Bit Manipulating in Registers
I broke my LaunchPad, and now I’m waiting for a new LaunchPad to deliver. In the mean time, I would like to take time to provide a small tip. I’ll take Run Mode Clock Gating Control register (RCGCGPIO) in the Tiva C-Series as an example for this post.
According to the data sheet, this register looks like:
I choose clock gating control because we must unlock the clock before making any change in the corresponding port. Bit field [31:6] is reserved, which means we would be happier not to touch them. We can only manipulate [5:0] corresponding to [PortF:PortA]. So, in order to “unlock” the clock of a port (i.e. port B) and not to mess with others:
Convert binary to hex:
B000010 = 0x02
SYSCTL_RCGC2_R |= 0x02;
There are two ways to manipulate a bit in this course:
“Set” a bit: To set a bit, we use bit-wise
According to logic,
0 | b = band
1 | b = 1.
Basically, because the Reset state of the register is ALWAYS 0, we only take the value we want it to be, in this case,
0x02, and perform an
ORoperation, and then we will get
0 | b = b).
“Clear” a bit: To clear a bit, we use bit-wise
Similar to settinga bit, according to logic,
0 & b = 0and
1 & b = b.
If we want to clear all the bit no matter what is inside it, perform an
0and we will get all zeros.
Using this strategy, we can unlock more clock since “1″ stands for “enable” and “0″ stands for “disable”.
That’s all I got. I hope this helped.