Andre said:
First, I am retired and doing all this just for fun ;-) and it's a
looong time since I used C language
I need to write 4 bits to PORTB without disturbing the others bit
RB1, RB2 are ( will be ) used by the UART.
If I Mask 4 bits like CC = CC & 0x0F the upper bits are set to 0 but
still there?
I think I could create a structure ( or a type ) with 4 bits such as
BCD { RB3
RB4
RB5
RB6
}
But I keep trying, no luck ( so far )
Can someone give a hand??
Many thanks in advances
André
From a digital logic point of view, you do things
like this with bitwise "AND" and "OR".
You "mask" fields with a bitmask and the "AND" function.
So the "& 0x0F" thing you did, is an example of selecting
the four lowest bits.
Now, imagine you kept a temporary variable, where the upper bits
were a valid copy of the register in question. Or, you were allowed
to do a read on the register right now. To "not disturb the bits",
you could mask with "& 0xF0" , which makes a copy of the upper
four bits of the byte. Next, "OR" together the desired value
for the lower bits, with the output of the "& 0xF0" step.
When written back, the upper bits will not be disturbed, in the
sense that when the register in the UART is written, you'll be
writing back the identical values for the upper bits.
http://cboard.cprogramming.com/c-programming/83723-logical-operation-functions-c;-xor-not-etc.html
"In C you can achieve this by using '&&' for AND, '||' for OR,
'!' for NOT, '^' for XOR, '&' bitwise AND,'|' Bitwise OR ."
http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/BitOp/bitwise.html
Typically, hardware doesn't allow writing to individual bits.
Usually, the hardware has "registers" or groups of bits, and
the registers are a common width. If you have an eight bit
wide register, and you need to write four bits of it,
you simply write identical values to what are already
stored in the other bits.
If the register contains 0x34 right now, and you want to
write a "5" into the lower bits, you mask and make a copy
of the upper bits (giving 0x30) and bitwise OR that with
the 0x05, giving 0x35 as the result. Then write that to
the UART. The "3" part is then undisturbed. It means,
somehow, you have to keep track of the value of the "undisturbed"
section of the register, in order to know it is 0x30 and
the upper part already holds a "3". That could be done with
a temporary variable, or if the hardware allows it, by doing
a read cycle on the register.
HTH,
Paul