Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading x,y,z values from LIS3DH on interrupt using nrf52, sdk 15

Status
Not open for further replies.

dipk11

Junior Member level 2
Junior Member level 2
Joined
Jul 27, 2017
Messages
23
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
245
I have interfaced Lis3dh on my nrf52 board using i2c interface. I am able to communicate with the accelerometer and get some x,y,z reading.This data is later sent to an app over BLE. I have tried 6.3.2 section from the application notes AN3308 of the LIS3DH sensor. I have tried to change the sensitivity and the threshold but still no change. I am using latched mode of the interrupt and reading the status register once the interrupt flag is set.

I constantly get interrupts even without shaking the device. Also i have noticed that the data makes no sense, it keeps on fluctuating even at rest.

I have noticed that, if i dont use latched interrupt, the interrupt doesnt occur.

Below is the main file code of my project. I do all the initialisations and accelerometer configuration, then go in an infinite loop for the interrupt yo occur. I set a flag interruptFlag in the ISR of INT1 pin. This flag is being polled in the main loop.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
int main(void)
{
    uint32_t err_code;
     int j=0;
     bool state =false;
     nrf_gpio_cfg_input(13, NRF_GPIO_PIN_PULLDOWN); //Pin 13 for interrupt pin
 
     uint8_t uiStatus=0;
    // Initialize.
    log_init();
 
    power_management_init();
    ble_stack_init();
    advertising_init();
 
     gpio_init(); //interrupt pin INT1 initialisation for change in state event
 
     twi_init(); //i2c initialisation
     data_Configuration(); //configure the LIS3dh
     nrf_delay_ms(500);
     read_Status_Reg();
     NRF_LOG_INFO("Waiting for interrupt");
 
    // Enter main loop.
   for (;;)
    {
 
      nrf_delay_ms(50);
     write_interrupt_cfg();
     nrf_delay_ms(50);
 
     if (interruptFlag) 
     {
 
        interruptFlag=false;
//        read_Ref_Reg(); //read reference register
//        nrf_delay_ms(500);
       read_Status_Reg(); //read status register
       nrf_delay_ms(500);
       read_sensor_data(); //read sensor data registers-all 6
 
       for (int i = 8; i < 14; i++) 
       {
         ble_data[i]=dataArray[j];       
         j++;
       }
 
        sd_ble_gap_adv_stop(m_adv_handle);
 
       //manuf_specific_data.company_identifier = APP_COMPANY_IDENTIFIER;
       manuf_specific_data.data.p_data = (uint8_t *)ble_data;
       manuf_specific_data.data.size = 14;
       advdata.name_type = BLE_ADVDATA_NO_NAME;
 
       advdata.p_manuf_specific_data = &manuf_specific_data;
 
       ble_advdata_encode(&advdata, m_adv_data.adv_data.p_data, &m_adv_data.adv_data.len);
 
       sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
 
 
       advertising_start();
          for (int i = 8; i < 14; i++) 
           {
 
          NRF_LOG_INFO("Data %d %d", i, ble_data[i]);
          NRF_LOG_PROCESS();
          }     
     }
    idle_state_handle();
   }
 
}



Below is the data configuration function for the Lis3dh


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
void data_Configuration(void)
{
  ret_code_t err_code;
 
    nrf_delay_ms(500);
   /* Writing to CTRL_REG1 to use all 3 axis----1Hz Normal Mode. */
    uint8_t reg1[2] = {CTRL_REG1, 0x57};
    err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg1, sizeof(reg1), false);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
 
   nrf_delay_ms(500);
 
   uint8_t reg2[2] = {CTRL_REG2, 0x00}; //no high pass filter
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg2, sizeof(reg2), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(500);
 
  uint8_t reg3[2] = {CTRL_REG3, 0x40}; //interrupt on int1
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg3, sizeof(reg3), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(500);
 
   uint8_t reg4[2] = {CTRL_REG4, 0x00}; // +/- 2g, LSB first,
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg4, sizeof(reg4), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(500);
 
 
  uint8_t reg_Ref[2] = {REFERENCE, 0x00}; //reference reg
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg_Ref, sizeof(reg_Ref), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(500);
 
  uint8_t reg5[2] = {CTRL_REG5, 0x08}; // interrupt request latched
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg5, sizeof(reg5), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(500);
 
  uint8_t reg6[2] = {CTRL_REG6, 0x00}; 
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg6, sizeof(reg6), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(1000);
 
  uint8_t reg_ths[2] = {INT1_THS,0x10}; // threshold 250mg
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg_ths, sizeof(reg_ths), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(1000);
 
  uint8_t reg_int[2] = {INT1_DURATION, 0x00}; // duration =0
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg_int, sizeof(reg_int), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
  nrf_delay_ms(1000);
 
  uint8_t reg_int_cfg[2] = {INT1_CFG, 0x3F}; // AOI=0 and x,y,z events
  err_code = nrf_drv_twi_tx(&m_twi, LIS3DH_ADDR, reg_int_cfg, sizeof(reg_int_cfg), false);
  APP_ERROR_CHECK(err_code);
  while (m_xfer_done == false);
 
}



Please let me know if i have done any wrong settings. Also the delay between the data configuration is not mentioned in the DS as well as Application notes.
 
Last edited by a moderator:

I constantly get interrupts even without shaking the device

You did not make any reference in the code above to which specific value for the acceleration range was selected (FS) nor to what sensitiity (operating mode) the accelerometer was configured; for the case of higher resolution, the device becomes very sensitive; in the experiments I did here with a wake-up feature in a motion-activated system, even when gently placing any stuff on the table where the board was on, the device would detect this imperceptible "seismic wave". In shorts, this is just a matter of settings.
 

You did not make any reference in the code above to which specific value for the acceleration range was selected (FS) nor to what sensitiity (operating mode) the accelerometer was configured; for the case of higher resolution, the device becomes very sensitive; in the experiments I did here with a wake-up feature in a motion-activated system, even when gently placing any stuff on the table where the board was on, the device would detect this imperceptible "seismic wave". In shorts, this is just a matter of settings.

Hi,
I have configured it for the following
->1Hz Normal Mode,no high pass filter
-> +/- 2g sensitivity

interrupt settings are as follows:
->interrupt on int1 and interrupt request latched so i read the status register to clear the interrupt.
->threshold 250mg
->interrupt event for AOI=0 and x,y,z events

All these settings are done in the data_Configuration function.
 

+/- 2g sensitivity
Perhaps very sensitive, try configuring to ±16g.

threshold 250mg
In low power mode and FS=11 it can operate at 192mg/digit, which sounds reasonable; try increasing THS register in steps of 2x (0x01 << n) in order to define the suited threshold.
 

Hi andre,
thanks for this suggestion.
I tried setting it for 16g but the threshold value was difficult to set. I dont know the shiffting by 2 logic which you tolf. I set the value same. tHS=0x10 and the interrupt stopped coming. Even when i shake the device.
Also the values that i get are a bit tricky, makes no sense of it.
the x and y values are close to 0g .The z-axis valus is also close to 0g. I guess we have to expect a value close to 1g for z-axis.
Please help me.
 

sorry, a small mistake in my previous reply. The interrupt comes even without shaking the device, same as that of 2g settings. Threshold register set to 0x80.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top