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

I2C problem in nrf51822

hello there i am trying to interface adxl345 with nrf51822. my code is like

#define ADXL345_ADDR        0x53U
#define ADXL345_ADDR_WR     0xA6U
#define ADXL345_ADDR_RD     0xA7U
#define ADXL345_REG_MODE    0x2DU

/* Mode for MMA7660. */
#define ACTIVE_MODE 8u

void ADXL345_set_mode(void

)
{ret_code_t err_code;
 uint8_t reg[2] = {ADXL345_REG_MODE, ACTIVE_MODE};
  err_code = nrf_drv_twi_tx(&m_twi_adxl_345,ADXL345_ADDR_WR , reg, sizeof(reg), false);
	while(m_set_mode_done == false);
}
uint8_t read_data()
{ ret_code_t err_code;
	uint8_t rx_data[6];
	
while(1)
{    
    err_code = nrf_drv_twi_rx(&m_twi_adxl_345,ADXL345_ADDR_RD, rx_data, sizeof(rx_data), false);
    APP_ERROR_CHECK(err_code);
	
}
return rx_data[6];

}
void write_data()
{
   ret_code_t err_code;
	 uint8_t tx_data[1] = {0x32};
    err_code = nrf_drv_twi_tx(&m_twi_adxl_345,ADXL345_ADDR_WR, tx_data, sizeof(tx_data), false);
}


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_RX_DONE:
           // read_data(&m_sample);
				      read_data();
            m_xfer_done = true;
            break;
        case NRF_DRV_TWI_TX_DONE:
            if(m_set_mode_done != true)
            {
                m_set_mode_done  = true;
                return;
            }
            m_xfer_done = false;
            /* Read 4 bytes from the specified address. */
          //  err_code = nrf_drv_twi_rx(&m_twi_mma_7660, MMA7660_ADDR, (uint8_t*)&m_sample, sizeof(m_sample), false);
						 err_code = nrf_drv_twi_rx(&m_twi_adxl_345,ADXL345_ADDR_RD,(uint8_t*)&m_sample, sizeof(m_sample), false);
            APP_ERROR_CHECK(err_code);
            break;
        default:
      		const nrf_drv_twi_config_t twi_adxl_345_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH  // interrupt priority
    };
    void twi_init (void)
{
    ret_code_t err_code;
  const nrf_drv_twi_config_t twi_adxl_345_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH  // interrupt priority
    };
		err_code = nrf_drv_twi_init(&m_twi_adxl_345, &twi_adxl_345_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);
    
   // nrf_drv_twi_enable(&m_twi_mma_7660);
		nrf_drv_twi_enable(&m_twi_adxl_345);
}
int main(void)
{
    uart_config();
uint8_t	rx_data[6];
	int x,y,z;
   // int a = __GNUC__, c = __GNUC_PATCHLEVEL__;
    printf("\n\rTWI sensor example\r\n");
    twi_init();
   
    ADXL345_set_mode();
    uint8_t reg = {0x32};
    ret_code_t err_code;
    write_data();
  while(true)
    {
        nrf_delay_ms(100);
err_code = nrf_drv_twi_tx(&m_twi_adxl_345, ADXL345_ADDR_WR, &reg, sizeof(reg), true);
APP_ERROR_CHECK(err_code);
        //m_xfer_done = false;
			write_data();
				rx_data[6]=	read_data()
 x = (((int)rx_data[1]) << 8) | rx_data[0];   
 y = (((int)rx_data[3])<< 8) | rx_data[2];
 z = (((int)rx_data[5]) << 8) | rx_data[4];
	printf("\n\rTWI sensor example\r\n");
	printf("\n\r x is %d y is %d z is %d\r\n",x,y,z);
    }
}

can you tell me what is wrong with it? i am not getting data

Parents
  • if the rx function isn't calling you back then either the thing isn't sending data because it's not in a mode to send data, or it's not sending 6 bytes of it and the driver is waiting for the rest of it, or you're getting an error. You're likely to have to do a write to tell the device that

    1. you want data
    2. which register you want data from

    that write probably goes with the final flag set to true so you get a repeated start and not a new start. Then you send the read, perhaps waiting until the write completes first, perhaps not if the two commands can be buffered after each other.

    and that rx_data[] better not be a stack variable if you're passing it to a function because it'll get stomped on as soon as you return.

Reply
  • if the rx function isn't calling you back then either the thing isn't sending data because it's not in a mode to send data, or it's not sending 6 bytes of it and the driver is waiting for the rest of it, or you're getting an error. You're likely to have to do a write to tell the device that

    1. you want data
    2. which register you want data from

    that write probably goes with the final flag set to true so you get a repeated start and not a new start. Then you send the read, perhaps waiting until the write completes first, perhaps not if the two commands can be buffered after each other.

    and that rx_data[] better not be a stack variable if you're passing it to a function because it'll get stomped on as soon as you return.

Children
No Data
Related