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

Unable to Read or write MMA8652 registers using TWI

[https://drive.google.com/open?id=0ByjgT_Zt3YyJS2tKQko3cW10SlU]

hi i am working on MMA8652 i am working on mma7660 twi example in sdk11 for nrf52. i have changed the registers as the same way the MMA8652 need. here is my read and write functions

to read data:-

#define MMA8652_I2C_ADDR				(0x1D)

uint8_t	W_reg = MMA8652_REG_WHO_AM_I;

	err_code=nrf_drv_twi_tx(&m_twi_mma_8652, MMA8652_I2C_ADDR, &W_reg, sizeof(W_reg), false);

	APP_ERROR_CHECK(err_code);

	nrf_delay_ms(5);

	err_code = nrf_drv_twi_rx(&m_twi_mma_8652, MMA8652_I2C_ADDR, &W_reg, sizeof(W_reg));

	APP_ERROR_CHECK(err_code);

when i am going to run this the application is going to stuck.

and also i have one doubt. in sdk twi example there is one function for reading, in that example in twi handler directly reading from device address. and the result is going to structure instance with x,y,x,tilt.

here is that function

typedef struct

{

    elem_t  x;

    elem_t  y;

    elem_t  z;

    uint8_t tilt;


} sample;

err_code = nrf_drv_twi_rx(&m_twi_mma_8652, MMA8652_I2C_ADDR, (uint8_t*)&sample, sizeof(sample));

how it is possible to read directly from MMA8652_I2C_ADDR(0X1D)

if i am using same procedure i am getting x value is always 0 and y, z values are varying. is this procedure is correct to read x,y,z data from the accelerometer.

EDIT:-------

void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context) { ret_code_t err_code; static sample_t m_sample;

switch(p_event->type)
{
case NRF_DRV_TWI_EVT_DONE:
	//string_send("1twirxsuc\r\n");
	if ((p_event->type == NRF_DRV_TWI_EVT_DONE) &&
			(p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX))
	{
		if(m_set_mode_done != true)
		{
			m_set_mode_done  = true;
			return;
		}
		m_xfer_done = false;
		err_code = nrf_drv_twi_rx(&m_twi_mma_8652, 0X1D, (uint8_t*)&m_sample, sizeof(m_sample));
		if(err_code == NRF_SUCCESS){
			string_send("rx Done\r\n");
		}
		else
			string_send("rxfail\r\n");

		APP_ERROR_CHECK(err_code);
		if(err_code == NRF_SUCCESS){
			//string_send("2twi rx suc\r\n");
		}
	}
	else
	{
		//string_send("elsetwirx\r\n");
		read_data(&m_sample);
		m_xfer_done = true;

	}
	break;
default:
	break;
}

}

Parents
  • In the first code snippet, where does the program get "stuck"? Does nrf_drv_twi_tx or nrf_drv_twi_rx return an error code? If you do not tell the MMA8652 from which register address you're going to read from by performing a nrf_drv_twi_tx() before nrf_drv_twi_rx, then it will just start reading from register 0x00. If you take a look in the MMA8652 Datasheet, found here, you'll see that the OUT_X_MSB data starts from 0x01 and not 0x00 like the MMA7660. Register 0x00 is default set to 0x00 in the MMA8652, so thats probably why the x value stays zero. Thus, the first method is the better choice.

Reply
  • In the first code snippet, where does the program get "stuck"? Does nrf_drv_twi_tx or nrf_drv_twi_rx return an error code? If you do not tell the MMA8652 from which register address you're going to read from by performing a nrf_drv_twi_tx() before nrf_drv_twi_rx, then it will just start reading from register 0x00. If you take a look in the MMA8652 Datasheet, found here, you'll see that the OUT_X_MSB data starts from 0x01 and not 0x00 like the MMA7660. Register 0x00 is default set to 0x00 in the MMA8652, so thats probably why the x value stays zero. Thus, the first method is the better choice.

Children
No Data
Related