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

problem receiving data from LSM303DLHC accelerometer stm32f411ve discovery

Hi all, i am new in 32 bits microcontrollers,i am configuring the accelerometer LSM303DLHC using the I2C interface in the stm32f411ve discovery board, i write and receive the data from the accelerometer but the data is not steady

in resume i am writing three registers

//*****************************register 0x23****************************************
//the address is 0x32
i2cbuf[0]=0x23;//register 0x23
i2cbuf[1]=0x08;// set +/- 2g range and high resolution enable (datasheet accel)

HAL_I2C_Master_Transmit(&hi2c1,address,i2cbuf,2,10);//write  0x08 in the 0x23 register

//the write/read operation is correct because after this config i have checked it this way:

//reading the written register:
i2cbuf[0]=0x23;
HAL_I2C_Master_Transmit(&hi2c1,address,i2cbuf,1,10);//go to 0x23 register

i2cbuf[1]=0;
HAL_I2C_Master_Receive(&hi2c1,address,&i2cbuf[1],1,10);//read the 0x23 register value

//and in the debugging mode i get i2cbuf[1]=0x08 so the write is ok

//*****************************register 0x20****************************************

//test write register 0x20
i2cbuf[0]=0x20;
i2cbuf[1]=0x97;//normal data rate,normal mode power and z,x,y enable (i tried all the data rates but anything works)

HAL_I2C_Master_Transmit(&hi2c1,address,i2cbuf,2,10);//write 0x97 in the 0x20 register


//*****************************register 0x28****************************************

i2cbuf[0]=0x28;//register where is located the x accelerometer data
HAL_I2C_Master_Transmit(&hi2c1,address,i2cbuf,1,10);//go to 0x28 register

i2cbuf[1]=0;
HAL_I2C_Master_Receive(&hi2c1,address,&i2cbuf[1],6,10);//read 6 bytes from 0x28 register
//(x low,x high,y low,y high,z low,z high)

//any of this values are steady, all are constantly changing:
ax=(i2cbuf[2]<<8|i2cbuf[1]);//build the x value
ay=(i2cbuf[4]<<8|i2cbuf[3]);//build the y value
az=(i2cbuf[6]<<8|i2cbuf[5]);//build the z value

i think i am missing to config a register but i dont know what , at the beginning i didnt configure the 0x20 register and ax,ay and az lectures were 0 , but i realized that in the 0x20 register the default config was 0x00 (this is power down mode), then in started to set values in this register then ax,ay and az got some strange variables values.

please help and thanks.

  • Former Member
    0 Former Member

    Did you read the forum description ??

    The Keil Discussion Forum is an open forum where you may post questions and comments about Keil products. The articles you post here are visible immediately for everyone to read and respond.

  • Do you have any experience with any other microcontrollers?

    Do you have any experience with I2C in general?

    How have you proven basic I2C comms ?

    "the data is not steady"

    So what is that supposed to mean??

    What does any of this have to do with Keil ?