I am confused with the registers present in the ADXL345 digital accelerometer.
The first thing which confuses me is where I have to write the data to set resolution for +/-2g. I didn't find any mention of this register in the datasheet.
Secondly, there are two registers in which the measurement value for the X axis is stored. How do I read that data from both registers? Do I need to send the address of the register at the same time, or what?
The first thing which confuses me is where I have to write the data to set resolution for +/-2g. I didn't find any mention of this register in the datasheet.
You'll find this information on page 26 of the data sheet (at least, in Rev. E of the data sheet). The range is controlled by bits 0 and 1 in register 0x31 (DATA_FORMAT
).
Register 0x31—DATA_FORMAT (Read/Write)
The DATA_FORMAT register controls the presentation of data to Register 0x32 through Register 0x37. All data, except that for the ±16 g range, must be clipped to avoid rollover.
SELF_TEST Bit
A setting of 1 in the SELF_TEST bit applies a self-test force to the sensor, causing a shift in the output data. A value of 0 disables the self-test force.
SPI Bit
A value of 1 in the SPI bit sets the device to 3-wire SPI mode, and a value of 0 sets the device to 4-wire SPI mode.
INT_INVERT Bit
A value of 0 in the INT_INVERT bit sets the interrupts to active high, and a value of 1 sets the interrupts to active low. FULL_RES Bit
When this bit is set to a value of 1, the device is in full resolution mode, where the output resolution increases with the g range set by the range bits to maintain a 4 mg/LSB scale factor. When the FULL_RES bit is set to 0, the device is in 10-bit mode, and the range bits determine the maximum g range and scale factor.
Justify Bit
A setting of 1 in the justify bit selects left-justified (MSB) mode, and a setting of 0 selects right-justified mode with sign extension.
Range Bits
These bits set the g range as described in Table 21.
Table 21. g Range Setting
╔═════════╦══════════╗ ║ Setting ║ ║ ╠════╦════╣ g Range ║ ║ D1 ║ D0 ║ ║ ╠════╬════╬══════════╣ ║ 0 ║ 0 ║ +/- 2 g ║ ╠════╬════╬══════════╣ ║ 0 ║ 1 ║ +/- 4 g ║ ╠════╬════╬══════════╣ ║ 1 ║ 0 ║ +/- 8 g ║ ╠════╬════╬══════════╣ ║ 1 ║ 1 ║ +/- 16 g ║ ╚════╩════╩══════════╝
So, what you'll want to do is read the current value of register 0x31, mask off bits 0 and 1, set the value you want (as per Table 21), and then write the new value to register 0x31.
Secondly, there are two registers in which the measurement value for the X axis is stored. How do I read that data from both registers? Do I need to send the address of the register at the same time, or what?
No, you read each register sequentially.
Register 0x32 holds the least-significant bits of the x-axis value, and register 0x33 holds the most-significant bits of the x-axis value. Together, they combine to an x-axis reading with 13 (maximum) bits of precision, in two's-complement format. If you only needed 8 bits of precision, you could read only the MSB from register 0x33, which would be slightly faster than reading both registers.
The data sheet does make one additional recommendation that you should pay attention to:
It is recommended that a multiple-byte read of all registers be performed to prevent a change in data between reads of sequential registers.
How exactly you do a multiple-byte read varies, depending on whether you're using the SPI or I2C bus, but either way, it is described in the data sheet. For SPI:
To read or write multiple bytes in a single transmission, the multiple-byte bit, located after the R/W bit in the first byte transfer (MB in Figure 37 to Figure 39), must be set. After the register addressing and the first byte of data, each subsequent set of clock pulses (eight clock pulses) causes the ADXL345 to point to the next register for a read or write. This shifting continues until the clock pulses cease and CS is deasserted. To perform reads or writes on different, nonsequential registers, CS must be deasserted between transmissions and the new register must be addressed separately.