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
  • thanks i understand nrf_drv_twi_tx function.but i can't understand nrf_drv_twi_rx function. if i want to read 6 byte data from 0x32 address??

    my code is like

    ret_code_t err_code;
    
    uint8_t rx_data[6];
    err_code = nrf_drv_twi_rx(&m_twi_adxl_345,ADXL345_ADDR_RD, rx_data, sizeof(rx_data), false);
    APP_ERROR_CHECK(err_code);
    

    how to store this 6 byte data and how to start reading data from perticular address like 0x32?

Reply
  • thanks i understand nrf_drv_twi_tx function.but i can't understand nrf_drv_twi_rx function. if i want to read 6 byte data from 0x32 address??

    my code is like

    ret_code_t err_code;
    
    uint8_t rx_data[6];
    err_code = nrf_drv_twi_rx(&m_twi_adxl_345,ADXL345_ADDR_RD, rx_data, sizeof(rx_data), false);
    APP_ERROR_CHECK(err_code);
    

    how to store this 6 byte data and how to start reading data from perticular address like 0x32?

Children
No Data
Related