cancel
Showing results for 
Search instead for 
Did you mean: 

LIS2DH12 ACC Reading Problem

ESims.2
Associate

Hello,

I am using LIS2DH12 with SPI connection.

I am able to read back Device ID which is 51. Also, I can write a to the reference register and read back successfully.

However, I cannot set registers. I am writing value on it and read back. The value on the register does not change.

uint8_t dev_id;
 
    ret = lis2dh12_device_id_get(&dev_id);
    printf("Device ID %d\n", dev_id);
 
    uint8_t data= 0xAA;
    uint8_t data2=0;
    lis2dh12_filter_reference_set(&data);
    TMR_Delay(MXC_TMR0, MSEC(10));
    lis2dh12_filter_reference_get(&data2);
 
    int result = 0;
 
    result = 0;
    lis2dh12_op_md_t op;
    lis2dh12_op_md_t op_set = LIS2DH12_NM_10bit;
    result = lis2dh12_operating_mode_get(&op);
    printf("GET| Func result %d Mode %d\n", result, op);
    result = lis2dh12_operating_mode_set(op_set);
    TMR_Delay(MXC_TMR0, MSEC(10));
    printf("SET| Func result %d Mode %d\n", result, op_set);
    result = lis2dh12_operating_mode_get(&op);
    printf("GET| Func result %d Mode %d\n", result, op);
 
    result = 0;
    lis2dh12_odr_t odr;
    lis2dh12_odr_t odr_set = LIS2DH12_ODR_400Hz;
    result = lis2dh12_data_rate_get(&odr);
    printf("GET| Func result %d ODR %d\n", result, odr);
    result = lis2dh12_data_rate_set(odr_set);
    TMR_Delay(MXC_TMR0, MSEC(10));
    printf("SET| Func result %d ODR %d\n", result, odr_set);
    result = lis2dh12_data_rate_get(&odr);
    printf("GET| Func result %d ODR %d\n", result, odr);
 
    lis2dh12_fs_t fs;
    lis2dh12_fs_t fs_set = LIS2DH12_2g;
    result = lis2dh12_full_scale_get(&fs);
    printf("GET| Func result %d Full Scale %d\n", result, fs);
    result = lis2dh12_full_scale_set(fs_set);
    TMR_Delay(MXC_TMR0, MSEC(10));
    printf("SET| Func result %d Full Scale %d\n", result, fs_set);
    result = lis2dh12_full_scale_get(&fs);
    printf("GET| Func result %d Full Scale %d\n", result, fs);
 
    uint8_t buff[6] = {0};
    int16_t acc_x, acc_y, acc_z;
    uint8_t new_data;
 
 
    uint8_t reg_val;
 
    result = lis2dh12_read_reg_st(0x20, &reg_val);
    printf("Result %d CTRL_REG1 %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x21, &reg_val);
    printf("Result %d CTRL_REG2 %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x23, &reg_val);
    printf("Result %d CTRL_REG4 %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x28, &reg_val);
    printf("Result %d XL %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x29, &reg_val);
    printf("Result %d XH %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x2A, &reg_val);
    printf("Result %d YL %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x2B, &reg_val);
    printf("Result %d YH %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x2C, &reg_val);
    printf("Result %d ZL %x \n", result, reg_val);
 
    result = lis2dh12_read_reg_st(0x2D, &reg_val);
    printf("Result %d ZH %x \n", result, reg_val);

The output of the program is like

0693W000001s8BsQAI.png

 0693W000001s8DtQAI.png

I am running SPI with the following configurations;

1MHz Clock speed,

SPI Clock polarity active low, Sampling rising edge.

Could you help please to figure out my problem ?

 Also , why I am receiving same ACC value ?

Best regards

1 REPLY 1
Eleon BORLINI
ST Employee

Hi @ESims.2​ , can you please explain me the initial part of your code, for my better understanding?

uint8_t data= 0xAA;
uint8_t data2=0;
lis2dh12_filter_reference_set(&data);
TMR_Delay(MXC_TMR0, MSEC(10));
lis2dh12_filter_reference_get(&data2);

Which LIS2DH12 register are you reading?

Regards