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

nRF52832 + external sensor accelerometer

Hi...

I want to communicate the external sensor with nRF52 using I2C protocol , here i used twi sensor example and modified . constantly i am getting 0 value

Here i am sharing the code. Is this correct?

#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"


#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

/* TWI instance ID. */
#define TWI_INSTANCE_ID     0

/* Common addresses definition for temperature sensor. */


/* Mode for LM75B. */
#define NORMAL_MODE 0U

/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done = false;

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);


uint32_t device_addr    =0x4C ;

 uint32_t register_addr= 0x00;

 uint8_t *p_data;


void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config  = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
      .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

     err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
   
    APP_ERROR_CHECK(err_code);
    if (NRF_SUCCESS == err_code)
	{
		nrf_drv_twi_enable(&m_twi);
		NRF_LOG_INFO("TWI init success...");	
}
}


static void read_register()
{
  ret_code_t err_code;
  nrf_drv_twi_t twi_instance;
  uint32_t device_addr    =0x4C ;


 uint32_t register_addr=   0x00;
  uint8_t *p_data; uint8_t bytes;
  bool no_stop;

 err_code = nrf_drv_twi_tx(&twi_instance, device_addr, &register_addr, 1, no_stop);
  APP_ERROR_CHECK(err_code);

  if(err_code != NRF_SUCCESS) {
    return err_code;
  }
  err_code = nrf_drv_twi_rx(&twi_instance, device_addr, p_data, bytes);
return err_code;
}




int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("\r\nTWI sensor example started.");
    NRF_LOG_FLUSH();
    twi_init();
   read_register();

   
    while (1)
    {
        nrf_delay_ms(500);
           NRF_LOG_INFO("\r\nTWI .");
printf("p_data:%d",p_data);
NRF_LOG_INFO("P_data:%d",p_data);
NRF_LOG_FLUSH();
    
}
}

  • #include <stdio.h>
    #include "boards.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "nrf_drv_twi.h"
    #include "nrf_delay.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    #define LSM6_ADD      (0XD6U >> 1)
    
    #define G_EARTH 9.80665
    #define GAUSS_TO_TESLAS 100
    
    #define TWI_INSTANCE_ID     0
    
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    static volatile bool m_xfer_done = false;
    
    
    typedef union 
    {
        int16_t data[3];
        int8_t buff[6];
    } data_int_convert_t;
    
    typedef struct
    {
        float x;
        float y;
        float z;
        float gain;
    } axis_data_t;
    
    axis_data_t axis_data_lsm6_a;
    axis_data_t axis_data_lsm6_g;
    
    
    void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {
        switch (p_event->type)
        {
            case NRF_DRV_TWI_EVT_DONE:
                if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
                {
                    
                }
                m_xfer_done = true;
                break;
            default:
                break;
        }
    }
    
    void twi_init (void)
    {
        ret_code_t err_code;
        const nrf_drv_twi_config_t twi_vnet_config = {
           .scl                = 26,
           .sda                = 27,
           .frequency          = NRF_DRV_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
        err_code = nrf_drv_twi_init(&m_twi, &twi_vnet_config, twi_handler, NULL);
        APP_ERROR_CHECK(err_code);
        nrf_drv_twi_enable(&m_twi);	
    }
    
    void twi_reg_write(uint8_t addr, uint8_t *reg, uint16_t length)
    {
        m_xfer_done = false;
        uint32_t err_code;
        err_code = nrf_drv_twi_tx(&m_twi, addr, reg, 2, false);
        APP_ERROR_CHECK(err_code);
        while (m_xfer_done == false);
    }
    
    void twi_reg_read_t(uint8_t const addr, uint8_t reg, uint8_t *value, uint16_t length, uint32_t delay)
    {
        uint32_t err_code;
        uint8_t buff[2];
        m_xfer_done = false;
        err_code = nrf_drv_twi_tx(&m_twi, addr, &reg, sizeof(reg), false);
        APP_ERROR_CHECK(err_code);
    
        m_xfer_done = false;
        nrf_delay_us(delay);
        err_code = nrf_drv_twi_rx(&m_twi, addr, value, length);
        APP_ERROR_CHECK(err_code); 
    }
    
    void LSM6_CFG(void)
    {
        uint8_t reg[2] = {0x10, 0x80};
        m_xfer_done = false;
        uint32_t err_code;
        twi_reg_write(LSM6_ADD, reg, 2);
        reg[0] = 0x11; reg[1] = 0x80;
        twi_reg_write(LSM6_ADD, reg, 2);
        reg[0] = 0x12; reg[1] = 0x04;
        twi_reg_write(LSM6_ADD, reg, 2);
    }
    
    static void LSM6_Read(void)
    {
        uint8_t i = 0;
        data_int_convert_t data_cnv_lsm6_a;
        data_int_convert_t data_cnv_lsm6_g;
        uint8_t reg[2] = {0x28, 0x22};
        twi_reg_read_t(LSM6_ADD, reg[0], data_cnv_lsm6_a.buff, 6, 1300);
        nrf_delay_us(1000);
        twi_reg_read_t(LSM6_ADD, reg[1], data_cnv_lsm6_g.buff, 6, 1300);
        
        axis_data_lsm6_a.gain = 0.061 / 1000.0;
        axis_data_lsm6_a.x = (float)data_cnv_lsm6_a.data[0] * axis_data_lsm6_a.gain * G_EARTH;
        axis_data_lsm6_a.y = (float)data_cnv_lsm6_a.data[1] * axis_data_lsm6_a.gain * G_EARTH;
        axis_data_lsm6_a.z = (float)data_cnv_lsm6_a.data[2] * axis_data_lsm6_a.gain * G_EARTH;
        axis_data_lsm6_g.gain = 8.75 / 1000.0;
        axis_data_lsm6_g.x = (float)data_cnv_lsm6_g.data[0] * axis_data_lsm6_g.gain;
        axis_data_lsm6_g.y = (float)data_cnv_lsm6_g.data[1] * axis_data_lsm6_g.gain;
        axis_data_lsm6_g.z = (float)data_cnv_lsm6_g.data[2] * axis_data_lsm6_g.gain;
    }
    
    int main(void)
    {
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
        NRF_LOG_INFO("\r\nTWI sensor example started.");
        NRF_LOG_FLUSH();
        twi_init();
        LSM6_CFG();
        while (true)
        {
            NRF_LOG_FLUSH();
            LSM6_Read();
            nrf_delay_ms(1000);
        }
    }
    
    
    
    
    

    You can see the flow code

  • hi ...Huan.. In this case ,  from which register you are reading data . you have given more register address ,what are they ?

    .

    void LSM6_CFG(void)
    {
        uint8_t reg[2] = {0x10, 0x80};
        m_xfer_done = false;
        uint32_t err_code;
        twi_reg_write(LSM6_ADD, reg, 2);
        reg[0] = 0x11; reg[1] = 0x80;
        twi_reg_write(LSM6_ADD, reg, 2);
        reg[0] = 0x12; reg[1] = 0x04;
        twi_reg_write(LSM6_ADD, reg, 2);
    }
    
    static void LSM6_Read(void)
    {
        uint8_t i = 0;
        data_int_convert_t data_cnv_lsm6_a;
        data_int_convert_t data_cnv_lsm6_g;
        uint8_t reg[2] = {0x28, 0x22};
        twi_reg_read_t(LSM6_ADD, reg[0], data_cnv_lsm6_a.buff, 6, 1300);
        nrf_delay_us(1000);
        twi_reg_read_t(LSM6_ADD, reg[1], data_cnv_lsm6_g.buff, 6, 1300);

  • I'm using LSM6DS33 sensor: accelerometer(0x28) and gyroscope(0x22)

    Datasheet: https://www.st.com/resource/en/datasheet/lsm6ds33.pdf

  • i am getting data 0 only what will be issue 

  • which line you are printing the value ..i don't find 

Related