This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

accelerometer_result_error

Hello ,I wrote a program that allows to control the LIS2DE12 accelerometer but I am reviewing a fixed result which is always X = 72, Y = 33, Z = 32, I find out where the problem , thanks you in advance.

`int main(void)	
{
	int8_t X,Y,Z;
    
    uart_config();
    
    //accel_I2C_datawrite(&twi_instance);
    twi_init();
     m_device_address = accel_addr;
 
    uint8_t rx_buffer[6];
   rx_buffer[0] = 0x00;
//int threshold;
//accel_set_threshold(threshold);
    
        




            while(true)
        {
            accel_xyz_read(&X,&Y,&Z);
         printf("Groupeer accelero DATA2:  X: %d  Y: %d  Z: %d  \n", X ,Y,Z);
					nrf_delay_ms(1000);
        }      
    
}
`
  • I think we would need to see the contents of the accel_I2C_datawrite, accel_set_threshold and accel_xyz_read functions you are using in order to debug this.

  • Here is the code I used

    ret_code_t accel_I2C_datawrite(const nrf_drv_twi_t * p_twi_instance)
    

    { nrf_drv_twi_enable(p_twi_instance);
    ret_code_t ret_code
    uint8_t tx_data1[2] = {CTRL_REG1, 0x00}; ret_code = accel_I2C_register_write(accel_addr, tx_data1, sizeof(tx_data1) ); nrf_delay_ms(10);

        return ret_code;
    

    }

    ret_code_t accel_I2C_register_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t * pdata, uint32_t bytes) {
    ret_code_t ret_code; ret_code = nrf_drv_twi_tx(p_twi_master,slave_addr, &reg_addr,1,false);

    if(ret_code != NRF_SUCCESS)
    {
     
            return ret_code;
    }
    
    ret_code = nrf_drv_twi_rx(p_twi_master,slave_addr, pdata, bytes);
    
    return ret_code;
    

    }

    static void accel_xyz_read(uint16_t * x_value, uint16_t * y_value, uint16_t * z_value) { m_device_address = accel_addr; ret_code_t ret_code;

    uint8_t rx_buffer[6];
    rx_buffer[0] = 0x00;
    
    ret_code = accel_I2C_register_read(m_device_address,access_data, rx_buffer, sizeof(rx_buffer));
    

    *x_value = (rx_buffer[1]<<8)|rx_buffer[0]; *y_value = (rx_buffer[3]<<8)|rx_buffer[2]; *z_value = (rx_buffer[5]<<8)|rx_buffer[4]; //return ret_code; }

    int accel_set_threshold(int threshold) { int max_threshold = 127* THS_2G ; uint8_t ths = threshold/THS_2G; uint8_t accel_reg[2]; printf( "hors threshold \n"); if (threshold > max_threshold) { printf( "Threshold out of range\n"); return -1; }

    accel_reg[0] = INT2_THS;
    

    accel_reg[1] = ths;

    accel_I2C_register_write(accel_addr,accel_reg, sizeof(accel_reg) );
    accel_I2C_register_read(m_device_address,INT2_THS, &ths ,sizeof(ths));
    
    
    
    printf ("Threshold @%x(%d)\n",ths, ths);
    
    return 0;
    

    } int main(void) { uint16_t X,Y,Z; nrf_gpio_cfg_output(LED_1); nrf_gpio_pin_set(LED_1); uart_config(); printf("Start"); //accel_I2C_datawrite(&twi_instance); twi_init(); // m_device_address = accel_addr;

    uint8_t rx_buffer[6]; rx_buffer[0] = 0x00;
    accel_xyz_read(&X,&Y,&Z); printf("Groupeer accelero DATA: X: %d Y: %d Z: %d \n", X ,Y,Z);

            while(true)
        {
    	int threshold;
    

    accel_set_threshold(threshold); // accel_xyz_read(&X,&Y,&Z); // printf("Groupeer accelero DATA2: X: %d Y: %d Z: %d \n", X ,Y,Z); nrf_delay_ms(100); }

    }

  • I'm sorry but could you please delete all unused lines of code and repost? There are so many lines that are seemingly commented away and stacked after each other, I am not sure what is supposed to be here or not. Also could you please post the contents of the accel_I2C_register_write function?

    Secondly, have you made sure you are using the correct IC address for the chip, and do you follow the protocol as described in chapter 6.1.1 of the LIS2DE12 product specification?

  • here is the code that use for accel_I2C_register_write ret_code_t accel_I2C_register_write(uint8_t slave_addr,uint8_t * pdata, uint32_t bytes) { ret_code_t ret_code;

    ret_code = nrf_drv_twi_tx(p_twi_master, slave_addr, pdata, bytes , false);
    
    return ret_code;
    

    }

  • I want to know how to detect the movement from the accelerometer (without reading the axes)

Related