PDA

View Full Version : Bits to Word?



Qacer
- 10th March 2006, 20:39
Hello again all,

Thanks very much to those who have helped me in the past! I appreciate it a lot.

I wanted to double check here. I am reading a couple of bytes from an external device. Each bit in the byte represents a configuration code. I wanted to get bit 5 to bit 0 of one byte, also get all 7 bits of another byte, and then combine them all into a 14 bit word.

I had the following in mind:



databyte1 VAR byte 'This variable contains bit 5 to bit 0
databyte2 VAR byte 'This variable contains all 7 bits of the config
wordtoyou VAR word

[..assuming that databyte1 and databyte2 already contain the read values..]

databyte1 = databyte1 & %00111111
wordtoyou.byte1 = databyte1
wordtoyou.byte0 = databyte2

write 0, wordtoyou.byte0
wrte 1, wordtoyou.byte1



Does this look okay?

Thanks!

BigWumpus
- 10th March 2006, 20:47
In the past I used .lowbyte and .highbyte instead of .byte0 and .byte1.
What does the compiler do ?

Qacer
- 10th March 2006, 21:13
It actually compiled without any problems. But now I think I have another question. If I had a byte that represents different config bits, how can I set each bit individually?

If I had variable set as testbit VAR databyte1.0, will bit0 of databyte1 change if I set testbit to another value?

Errr.... how would I also set multiple bits in a byte? Say, I want to set bit5 to bit0 to a certain value.

Thanks!

mister_e
- 10th March 2006, 21:25
I'M BACK!!!

Sorry to be impolite and a pest but why don't simply test it and send results to a LCD or via Serial communication to your PC to see what's happen with?

Qacer
- 11th March 2006, 15:16
Hi mister_e,

:-) Sorry, I was feeling a bit lazy. Plus, I don't have the most efficient computer setup in the world. Anyway, this actually the question that I can't figure out: how would I set multiple bits in a byte simultaneously? Say, I want to set bit5 to bit0 to a certain value.

I was hoping there was something like this:

databyte1.[5 to 0] = %101011

Ruben Pena
- 11th March 2006, 18:53
Hi:
You can create aliases for each configuration bit. Ex:
name1 var byte1.0
name2 var byte1.2 etc.
then, you can clear or set each one by

name1 = 0, or name1 = 1

Or you can test them:
if name1 = 1 then do_something
I hope this helps.
Greetings...
Ruben de la Pena

picster
- 12th March 2006, 20:13
To set bit 4 on and leave the rest unchanged:

mybyte=mybyte | %00010000 'OR function with selected bit masked ON

To turn bit 4 off and leave the rest unchanged:

mybyte=mybyte & %11101111 'AND function with selected bit masked OFF

OR you can set multiple bits on or off by either ADDING or SUBTRACTING the bit weight to/from either 0 or 255 respectively.

------------------Picster-----------------