cancel
Showing results for 
Search instead for 
Did you mean: 

LIS3DH problem - STM32F429-EVAL

arturdav87
Associate II
Posted on March 08, 2015 at 20:26

Hello,

I have a problem to get axes data-> all axes are 7 (in 2's complement). 

I cant find the mistake. is all process is well?

/* SPI2 init function */

void SPI2_Init (void)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  SPI_InitTypeDef SPI_InitTypeDefStruct;

  /* Peripheral clock enable */

  __SPI2_CLK_ENABLE();

  /**SPI2 GPIO Configuration

   PB13     ------> SPI2_SCK

   PC3     ------> SPI2_MOSI

   PG7     ------> SPI2_NSS

   PB14     ------> SPI2_MISO

   */

  GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;

  HAL_GPIO_Init (GPIOB, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_3;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;

  HAL_GPIO_Init (GPIOC, &GPIO_InitStruct);

    /*Configure GPIO pin : PG7 - ACCL_CS */

  GPIO_InitStruct.Pin = GPIO_PIN_7;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  GPIO_InitStruct.Speed = GPIO_SPEED_FAST;

  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;

  SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Master;

  SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft;

  SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_High; //clock phase is high when idle

  SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_1Edge;//data write on falling (first) edge

  SPI_InitTypeDefStruct.SPI_CRCPolynomial = 7;

  SPI_Init (SPI2, &SPI_InitTypeDefStruct);

  SPI_Cmd(SPI2,ENABLE);

}

void Init_Acclerometer(void)

{

//Inizialize MEMS Sensor

 //set ODR (turn ON device)

 LIS3DH_SetODR(LIS3DH_ODR_100Hz);

 //set PowerMode

 LIS3DH_SetMode(LIS3DH_NORMAL);

 //set Fullscale

 LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);

 //set axis Enable

 LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);

}

//Reg addresses are:  0x29-X, 0x2B-Y, 0X2D-Z

u8_t SPI_Mems_Read_Reg(u8_t Reg )

{

  u8_t axis_data;

HAL_GPIO_WritePin(GPIOG,GPIO_PIN_7,GPIO_PIN_RESET); //CS low

        Reg = 0x80 | Reg; //first '1' bit is for read,

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE));  //transmit buffer empty?

SPI_I2S_SendData(SPI2, Reg); //send data

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)); //data received?

axis_data=SPI_I2S_ReceiveData(SPI2); //Clear RXNE bit

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE));  //transmit buffer empty?

SPI_I2S_SendData(SPI2, 0x00); //Dummy byte to generate clock

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)); //data received?

HAL_GPIO_WritePin(GPIOG,GPIO_PIN_7,GPIO_PIN_SET); //CS high

axis_data=SPI_I2S_ReceiveData(SPI2); //return reveiced data

return axis_data;

}

void SPI_Mems_Write_Reg(uint8_t adress, uint8_t data){

        HAL_GPIO_WritePin(GPIOG,GPIO_PIN_7,GPIO_PIN_RESET);   //CS low

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE));  //transmit buffer empty?

SPI_I2S_SendData(SPI2, adress);

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)); //data received?

SPI_I2S_ReceiveData(SPI2);

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE));  //transmit buffer empty?

SPI_I2S_SendData(SPI2, data);

while(!SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE)); //data received?

SPI_I2S_ReceiveData(SPI2);

HAL_GPIO_WritePin(GPIOG,GPIO_PIN_7,GPIO_PIN_SET); //CS high

}

#lis3dh
0 REPLIES 0