<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>nRF52832 + external sensor accelerometer</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/54561/nrf52832-external-sensor-accelerometer</link><description>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?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 20 Nov 2019 07:29:42 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/54561/nrf52832-external-sensor-accelerometer" /><item><title>RE: nRF52832 + external sensor accelerometer</title><link>https://devzone.nordicsemi.com/thread/220977?ContentTypeID=1</link><pubDate>Wed, 20 Nov 2019 07:29:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9a75ca01-e6ab-4282-90d7-b6e136e55a65</guid><dc:creator>ps_anu</dc:creator><description>&lt;p&gt;which line you are printing the value ..i don&amp;#39;t find&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52832 + external sensor accelerometer</title><link>https://devzone.nordicsemi.com/thread/220973?ContentTypeID=1</link><pubDate>Wed, 20 Nov 2019 07:22:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:71537ae1-880f-461c-b412-1e54f19a07c1</guid><dc:creator>ps_anu</dc:creator><description>&lt;p&gt;i am getting data 0 only what will be issue&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52832 + external sensor accelerometer</title><link>https://devzone.nordicsemi.com/thread/220776?ContentTypeID=1</link><pubDate>Tue, 19 Nov 2019 10:15:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:224c3987-15b6-43f4-b876-ab2178284651</guid><dc:creator>HuanVH</dc:creator><description>&lt;p&gt;I&amp;#39;m using LSM6DS33 sensor: accelerometer(0x28) and gyroscope(0x22)&lt;/p&gt;
&lt;p&gt;Datasheet:&amp;nbsp;&lt;a href="https://www.st.com/resource/en/datasheet/lsm6ds33.pdf"&gt;https://www.st.com/resource/en/datasheet/lsm6ds33.pdf&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52832 + external sensor accelerometer</title><link>https://devzone.nordicsemi.com/thread/220764?ContentTypeID=1</link><pubDate>Tue, 19 Nov 2019 09:47:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7ebb00cd-0d5f-45a6-9acd-fc41f536e9fa</guid><dc:creator>ps_anu</dc:creator><description>&lt;p&gt;hi ...Huan.. In this case ,&amp;nbsp;&amp;nbsp;from which register you are reading data . you have given more register address ,what are they ?&lt;/p&gt;
&lt;p&gt;.&lt;pre class="ui-code" data-mode="c_cpp"&gt;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);&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF52832 + external sensor accelerometer</title><link>https://devzone.nordicsemi.com/thread/220749?ContentTypeID=1</link><pubDate>Tue, 19 Nov 2019 09:14:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:925fe81e-70fb-40eb-80a7-8300c6ebb14a</guid><dc:creator>HuanVH</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#define LSM6_ADD      (0XD6U &amp;gt;&amp;gt; 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-&amp;gt;type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event-&amp;gt;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(&amp;amp;m_twi, &amp;amp;twi_vnet_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);
    nrf_drv_twi_enable(&amp;amp;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(&amp;amp;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(&amp;amp;m_twi, addr, &amp;amp;reg, sizeof(reg), false);
    APP_ERROR_CHECK(err_code);

    m_xfer_done = false;
    nrf_delay_us(delay);
    err_code = nrf_drv_twi_rx(&amp;amp;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(&amp;quot;\r\nTWI sensor example started.&amp;quot;);
    NRF_LOG_FLUSH();
    twi_init();
    LSM6_CFG();
    while (true)
    {
        NRF_LOG_FLUSH();
        LSM6_Read();
        nrf_delay_ms(1000);
    }
}




&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You can see the flow code&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>