I am currently trying to work with the ADXL343 accelerometer, and only want to read the Z-axis for it being tapped, so i as thinking that the D4 and D0 bits of the 0x2B register needs to be set, but im unsure on how to do this

- 83
- 10
-
Does this answer your question? [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit) – phuclv Oct 08 '20 at 00:08
1 Answers
There will be some details about how you read from and write to this register. Probably it will be mapped into memory in some way and you an access it by reading from and writing to that memory address. Since you didn't give any details about how it's implemented, I can't help you with that. What I can do is tell you how you can modify specific bits in the value of the register after you read it.
Since this register contains 8 bits, I will use unsigned char type for it. And you can modify bits in the register by using bitwise operations, specifically bitwise and (&) and bitwise or (|).
Typically you would define a mask isolating the particular bit of interest. In this case, it will be a 8-bit value that sets a 1 in the bit of interest and 0 everywhere else. So for D0 that would be a binary value of 0000 0001 and for D4 it would be 0001 0000. These can be written equivalently in hex as 0x01 and 0x10 respectively, which is how masking of this type is typically done.
#define REGISTER_0x2B_D0_MASK 0x01
#define REGISTER_0x2B_D4_MASK 0x10
Now to read one of these bits, you can simply do bitwise and with the appropriate mask. To write (set) the bit, you can do bitwise or.
d0 = register_value & REGISTER_0x2B_D0_MASK; // read bit d0 value from register
register_value = register_value | REGISTER_0x2B_D0_MASK; // set bit d0 to 1
register_value = register_value & ~REGISTER_0x2B_D0_MASK; // set bit d0 to 0
The first line works by ANDing each bit with that of the mask. Since all bits of the mask are 0 except the last bit, it makes the results 0 in all bits except the last one (because 0&anything is 0, which 1&anything is that thing).
The second line works by ORing each bit with the mask. Since 0|anything is that thing and 1|anything is 1, it sets only the final bit to 1 while everything else stays as it was.
The third line is the trickiest because it uses the complement of the mask with ~. That inverts the mask, so it becomes all 1s except the last bit, which becomes 0. Then applying bitwise and means it ANDs every bit with 1 (making no change) except for the last bit, which is ANDed with 0 forcing it to be 0.
So the example code would look something like this:
#define REGISTER_0x2B_D0_MASK 0x01
#define REGISTER_0x2B_D4_MASK 0x10
void main()
{
unsigned char register_value, d0, d4;
// get the current value of the register somehow
register_value = read_register_0x2b();
// read the bits of interest
d0 = register_value & REGISTER_0x2B_D0_MASK;
d4 = register_value & REGISTER_0x2B_D4_MASK;
// set D0 to 1
register_value = register_value | REGISTER_0x2B_D0_MASK;
// set D4 to 0
register_value = register_value & ~REGISTER_0x2B_D4_MASK;
// apply changes to register
write_register_0x2b(register_value);
}
Note that in the example reads of the bits, you get the bits in the position that they occupy in the register. So if d0 is set to 1, then the d0 value will be 0x01. Likewise, if d4 is set to 1, then the d4 value will be 0x10. This is fine if you just want to use it to test whether the bit is set, so 0 is not set and anything else is set. If you want d4 to be exactly 1 if it's set, you would need to shift the bit down to the least significant position like this:
d4 = (register_value & REGISTER_0x2B_D4_MASK) >> 4;
- 532
- 3
- 9