PDA

View Full Version : Binary assignment syntax - confused?



kevj
- 10th November 2007, 21:27
I'm not sure of how the binary bits are assigned to registeres when written different ways.

- I know I can change bit 4 of TRISA by writing: TRISA.4 = 1

- I know I can set all bits of an 8 bit register to 1 by writing: TRISC = %11111111

But I don't know what order those numbers corespond to the registers. For example:

TRISC = %01111111

Does that set bit 0 or bit 8 to "0"?


And what about the many registeres that don't have all their values - a register that has bits I can set for bits 0-2, but I can't set 3, but I can set bit 4 and 5, and I can't set bit 7 and 8.

Does that mean I just omit those out of the placement in my string? For example:

PCON = %11000

... where each position in the string automatically falls into the bits I can change, or do I still need to fill all the didgets with some value, even if that place in the string represents a bit I cannot change (it's a grey box with a dash in the data sheet).


My confusion on this has lead me to writing all my programs like:

PCON.0 = 1
PCON.1 = 0
PCON.4 = 0
PCON.5 = 1

... I'd like to save some space and say:

PCON = %1001 or...
PCON = %10000100 or....
PCON = %00100001 (the above reversed)


..... but I don't know how the various positions assign to the various bits.




And to expand on that .... how would I assign just a few bits without changing the others? What if I wanted to assign "1111" to bits 2,3,4,5 without effecting bits 0,1 and 6,7? I've seen stuff like "$0f" in this context which I assume refers to the hex assignement of something, but again, no luck finding a consise primer or tutorial on this stuff.

I get what hex and binary numbering shemese are - lots of refrences for those, but none that show how to actually syntax it in PBP.

Can someone provide a good reference on this? I'm sure it's spelled out someplace but searching for "bit" "bitwise" "assembly bit assignment", etc on google turns up nothing. :(

mister_e
- 11th November 2007, 02:19
Bit # 76543210
TRISIO = %01010101

if you want to set few bits without altering the other, you may need to set them manually one after the other or use bitwise OR.

let's say you want to change bit 0,2,4,6 of OPTION_REG which you already set to %00000000, then all you need to do is to use
OPTION_REG=OPTION_REG | %01010101

You can refer to section 4.17 and see the following
http://www.learn-c.com/boolean.htm

HTH