This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Reading and Writing to Registers using nrf52 TWI

I am building a project which requires accelerometer data from the LIS3DH accelerometer be read and then transmitted over bluetooth. I have successfully setup bluetooth transmission, and the read function for reading from the accelerometer registers. However, I seem unable to write to registers properly. Below is the write Register Function I have written. The issue I have is when I try to read after a write has performed, it returns the most recent register number I have tried to write to. So, for instance, if I write to register 0x30, and do read register 0x20, it will return 0x30. Or if I write to register 0x30, then to register 0x43, and do a read to register 0x13, it will return 0x43. I am struggling and have been working on this for a few days now. I have included my writeRegister() and readRegister() functions below.

bool writeRegister(uint8_t regNumber, uint8_t value){
uint8_t valueBuffer[2];
valueBuffer[0] = regNumber;
valueBuffer[1] = value;
int32_t toSend = (int32_t)value;
uint32_t err_code = nrf_drv_twi_tx(&m_twi_accelSensor, ACCELEROMETER_ADDRESS, valueBuffer, sizeof(valueBuffer), false);

}

uint8_t readRegister(uint8_t regNumber){
		uint8_t resultsWhoAmI;
		uint8_t whoAmIPointer = regNumber;
		nrf_drv_twi_tx(&m_twi_accelSensor, ACCELEROMETER_ADDRESS, &whoAmIPointer, 1, true);
		nrf_drv_twi_rx(&m_twi_accelSensor, ACCELEROMETER_ADDRESS, &resultsWhoAmI, 1);
		return resultsWhoAmI;

}

However, if I do not do any writes before reading, I am able to get proper values, such as the whoami register returns 0x33, which is the default value. I have run through all the xamples provided, to no avail.

Related