Best way to address bits in a byte?
RichardF
Posts: 168
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
Thanks,
Richard
Comments
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
Mask:=1<<bit
to make a mask
Richard
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 [noparse][[/noparse] is only for the forum software
Post Edited (Ariba) : 3/28/2008 4:02:15 PM GMT
Ariba, your use of the b registers is really cool. I have always assumed they were not addressable, since there are no associated pins.
·
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.
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!
Surely:
should be
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Post Edited (Paul Baker (Parallax)) : 3/29/2008 2:52:31 AM GMT
J