Parallax Forums
  HomeLog InRegisterCommunity CalendarSearch the ForumHelp
   
Parallax Forums > Public Forums > Propeller Chip > Best way to address bits in a byte?  Forum Quick Jump
 
New Topic Post Reply Printable Version
[ << Previous Thread | Next Thread >> ] | Show Newest Post First ]

RichardF
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Mar 2007
Total Posts : 154
 
   Posted 3/28/2008 4:47 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
I need to be able to read and write individual bits within a byte variable in order to keep track of eight switch positions (on/off). This is easy to do in pBasic using the .BITn form, but I don't see an easy way (or frankly any way) to do this in Spin. Would appreciate some suggestions.
Thanks,
Richard
Back to Top
 

Mike Green
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Oct 2004
Total Posts : 15579
 
   Posted 3/28/2008 6:25 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
byteVar &= !Mask ' To set a bit off
byteVar |= Mask ' To set a bit on
byteVar ^= Mask ' To toggle a bit
if byteVar & Mask ' To test if a bit is on
where
CON Mask = %10000000 ' or something similar
Back to Top
 

Rayman
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2007
Total Posts : 3127
 
   Posted 3/28/2008 6:27 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
Usually, I'd do something like

Mask:=1<<bit

to make a mask
Back to Top
 

tekochip
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Feb 2007
Total Posts : 56
 
   Posted 3/28/2008 6:33 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
Here's some examples with functions to do the same:
 
CON
  #0, Serve, Dispense, Flash 'Declare some bits
 
PUB Demo |coils
 
  OnBit(@coils, Serve)
  ToggleBit(@coils, Flash)
 
  if TestBit(@coils, Flash)
    OffBit(@coils, Serve)

PUB OnBit(x, bit)
{{Turn a bit on}}
  long[x]|=1<<bit
 
PUB OffBit(x, bit)
{{Turn a bit off}} 
  long[x]&= !(1<<bit)
 
PUB ToggleBit(x, bit)
{{Toggle a bit}}
  long[x]^=1<<bit
 
PUB TestBit(x, bit)
{{Return true if a bit is on}}
  return long[x] & (1<<bit)       
Back to Top
 

RichardF
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Mar 2007
Total Posts : 154
 
   Posted 3/28/2008 7:41 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
Thanks everyone. Makes me appreciate pBasic (smile)
Richard
Back to Top
 

hippy
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Feb 2007
Total Posts : 1951
 
   Posted 3/28/2008 8:02 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
For reading bits you could create a method which handles that, "var.bit4" becomes "bit4(var)", and option which is consistent and works for read and write is "GetBit4(@var)" and "SetBit4(@var,bitVal)", something like ...


PUB GetBit4( varPtr )
  result := GetBitX(4, varPtr )
PUB GetBitX( bitNum, varPtr )
  result := ( byte[ varPtr ] & |< bitNum ) <> 0

etc
Back to Top
 

Ariba
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Apr 2007
Total Posts : 712
 
   Posted 3/28/2008 8:57 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
Another idea is to use the outb and dirb registers as bitaddressable variables (Prop 1 has this registers but no PortB hardware):

1)
  outb[ 4] := 1
  if outb[ 5] == 1
    outb[ 4] := 0
2)
  outb[ 4]~~
  if outb[ 5]
    outb[ 4]~


1) is better readable and 2) is more optimized.
You get up to 64 bits with this methode. And if you don't use the timers, also frqa, frqb, phsa, phsb are good for bitaddressing.

Andy

Edit: the space after [ is only for the forum software ;-)

Post Edited (Ariba) : 3/28/2008 4:02:15 PM GMT

Back to Top
 

RichardF
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Mar 2007
Total Posts : 154
 
   Posted 3/28/2008 9:40 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
Two more great ideas, thank you.
Ariba, your use of the b registers is really cool. I have always assumed they were not addressable, since there are no associated pins.
Back to Top
 

Beau Schwabe (Parallax)
IC Layout Engineer



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Aug 2004
Total Posts : 3967
 
   Posted 3/28/2008 10:07 AM (GMT -7)    Quote This PostAlert An Admin About This Post.

Ariba,

 

outb is ok to use on the Current Propeller Chip, but it will not be backwards compatible on the next Propeller Chip

 

Instead, if you are not using any or one of the counters, you could use the frqa, frqb, phsa, and phsb registers to do basically the same thing that you are suggesting.

Stay away from the ctra, and ctrb registers as you could startup one of the counters without intending to if you write to them.

 

 


Beau Schwabe

IC Layout Engineer
Parallax, Inc.

Back to Top
 

Rayman
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2007
Total Posts : 3127
 
   Posted 3/28/2008 10:43 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
Ariba, Beau,

I like this idea! I rarely use frqa, frqb, phsa, or phsb in the main cog. So, this is a very nice way to access bits!
Back to Top
 

Javalin
Got a Propeller, need some SPIN?

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 768
 
   Posted 3/28/2008 12:09 PM (GMT -7)    Quote This PostAlert An Admin About This Post.
Chaps,

Surely:
 
if byteVar & Mask ' To test if a bit is on
where
  'CON Mask = %10000000 ' or something similar
 
should be
if byteVar & Mask == Mask ' to test if a bit is on
so - if byteVar is %00001111 & %00001000 would leave %00001000 so surely wouldn't equate to TRUE/FALSE or 1 or 0?

??

James

Post Edited (Javalin) : 3/28/2008 7:37:55 PM GMT

Back to Top
 

Rayman
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2007
Total Posts : 3127
 
   Posted 3/28/2008 1:10 PM (GMT -7)    Quote This PostAlert An Admin About This Post.
I think zero == false and non-zero == true...
Back to Top
 

Paul Baker
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 6323
 
   Posted 3/28/2008 7:45 PM (GMT -7)    Quote This PostAlert An Admin About This Post.
correct, just like C, false is 0 and anything else is true, so it is equivalent to "if (byteVar & Mask)<>0"


Paul Baker

Propeller Applications Engineer

Parallax, Inc.

Post Edited (Paul Baker (Parallax)) : 3/29/2008 2:52:31 AM GMT

Back to Top
 

Javalin
Got a Propeller, need some SPIN?

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 768
 
   Posted 3/29/2008 2:12 AM (GMT -7)    Quote This PostAlert An Admin About This Post.
ah - ok!

J
Back to Top
 
[ << Previous Thread | Next Thread >> ]
New Topic Post Reply Printable Version
 
Forum Information
Currently it is Thursday, July 29, 2010 5:17 PM (GMT -7)
There are a total of 462,440 posts in 62,066 threads.
In the last 3 days there were 90 new threads and 803 reply posts. View Active Threads
Who's Online
This forum has 20143 registered members. Please welcome our newest member, ME01.
55 Guest(s), 9 Registered Member(s) are currently online.  Details
John Abshier, Rayman, BradC, prof_braino, Sapieha, Gene Bonin, laser-vector, localroger, Nick McClick