Skip to main content
simoneformichella
Associate
September 10, 2016
Question

STM32F4 Discovery and accelerometer

  • September 10, 2016
  • 18 replies
  • 6951 views
Posted on September 10, 2016 at 20:32

Hi everybody,

I'm writing this post to ask if someone, please, can help me resolving a problem with the datas-reading from the accelerometer integrated in the STM32F4 Evaluation Board.

What I want to do it's simply to read the accelerations datas from the sensor, using the SPI interface. I've had a look at a lot of examples but I still don't know why my code doesn't work.

Here the main functions:

void WriteAccelerometer(SPI_HandleTypeDef SPI, uint8_t address, uint8_t data)

{

       HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); //CS --> Low

           HAL_SPI_Transmit(&SPI,&address,1,50);

           HAL_SPI_Transmit(&SPI,&data,1,50);

           HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET); //CS --> High

uint8_t ReadAccelerometer(SPI_HandleTypeDef SPI, uint8_t address, uint8_t data)

{

 address = address | 0x80;

 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); //CS --> Low

          HAL_SPI_Transmit(&SPI,&address,1,50);

 HAL_SPI_Receive(&SPI,&data,1,50);

          HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET); //CS --> High

 return data;

}

Here the SPI configuration function:

static void MX_SPI1_Init(void)

{

  

hspi1.Instance = SPI1;

  hspi1.Init.Mode = SPI_MODE_MASTER;

  hspi1.Init.Direction = SPI_DIRECTION_2LINES;

  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

  hspi1.Init.NSS = SPI_NSS_SOFT;

  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;

  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

  hspi1.Init.CRCPolynomial = 10;

  if (HAL_SPI_Init(&hspi1) != HAL_OK)

  {

    Error_Handler();

  }

//WriteAccelerometer(hspi1,0x23,0xC9); //reset

//WriteAccelerometer(hspi1,0x21,0x00);

}

I use the two functions reported in this post to configurate and read the accelerometer with this commands:

uint8_t config_address = 0x20;

uint8_t config_data = 0x27; 

uint8_t Address_ACCX = 0x29;

uint8_t Address_ACCY = 0x2B;

uint8_t Address_ACCZ = 0x2D;

WriteAccelerometer(hspi1,config_address,config_data); //config

// read data:

AccX = ReadAccelerometer(hspi1,Address_ACCX,AccX);

AccY = ReadAccelerometer(hspi1,Address_ACCY,AccY);

AccZ = ReadAccelerometer(hspi1,Address_ACCZ,AccZ);

Thank you in advance for the help..

Simone

    This topic has been closed for replies.

    18 replies

    Zek_De
    Senior
    October 31, 2017
    Posted on October 31, 2017 at 22:15

    0690X00000604BCQAY.jpg
    Zek_De
    Senior
    October 31, 2017
    Posted on October 31, 2017 at 22:18

    I am using STM32f4 discovery board,actually .SPI is working I controlled with Osciloscope also NRF24L01 is working and I2C is working same ADXL345 I mean device is strong.Also my new codes are here.

    void ADXL345_Setup(SPI_HandleTypeDef *hspi)

    {

    if(HAL_SPI_GetState(hspi) == HAL_SPI_STATE_READY )

    {

    ADXL345_Write_Single(hspi,BW_RATE,0x0D);

    ADXL345_Write_Single(hspi,POWER_CTL,0x08);

    //ADXL345_Write_Single(hspi,DATA_FORMAT,0x04);

    }

    else

    {

    while(1)

    {

    }

    }

    }

    //##################################################################################

    void ADXL345_Raw_Values(SPI_HandleTypeDef *hspi,int16_t *AccRaw)

    {

    uint8_t out[6]={0},temp=0;

    do

    {

    temp = ADXL345_Read_Single(hspi,INT_SOURCE);

    }while((temp & 0x80) == 0);

    ADXL345_Read_Multi(hspi,DATAXL,out,6);

    AccRaw[x_eksen] = (int16_t)(((uint16_t)out[1]<<8) | out[0]);

    AccRaw[y_eksen] = (int16_t)(((uint16_t)out[3]<<8) | out[2]);

    AccRaw[z_eksen] = (int16_t)(((uint16_t)out[5]<<8) | out[4]);

    }

    //##################################################################################

    void ADXL345_Write_Single(SPI_HandleTypeDef *hspi, uint8_t reg, uint8_t data)

    {

    CS_LOW;

    HAL_SPI_Transmit(hspi,&reg,1,100);

    HAL_SPI_Transmit(hspi,&data,1,100);

    CS_HIGH;

    }

    //##################################################################################

    //##################################################################################

    uint8_t ADXL345_Read_Single(SPI_HandleTypeDef *hspi, uint8_t reg)

    {

    reg |= 0x80; // Okuma biti etkin //

    uint8_t out;

    CS_LOW;

    HAL_SPI_Transmit(hspi,&reg,1,100);

    HAL_SPI_Receive(hspi,&out,1,100);

    //HAL_SPI_TransmitReceive(hspi, &adr, &out, 1, 200);

    CS_HIGH;

    return out;

    }

    //##################################################################################

    //##################################################################################

    void ADXL345_Read_Multi(SPI_HandleTypeDef *hspi, uint8_t reg,uint8_t *out, uint8_t NumberOfByte)

    {

    reg |= 0xC0; // Okuma ve çoklu okuma bitleri etkin.0b1100-0000 //

    CS_LOW;

    HAL_SPI_TransmitReceive(hspi,&reg,out,NumberOfByte,100);

    CS_HIGH;

    }

    //##################################################################################

    //##################################################################################
    Zek_De
    Senior
    October 31, 2017
    Posted on October 31, 2017 at 22:19

    I am trying 2 days this.

    Luke Dempsey
    Visitor II
    October 31, 2017
    Posted on October 31, 2017 at 22:23

    Your ADXL CS pin will be different to the one required by the accelerometer on the board though, correct?

    Zek_De
    Senior
    October 31, 2017
    Posted on October 31, 2017 at 22:44

    I didnt manage to understand rhat you mean,sorry my late reply

    Luke Dempsey
    Visitor II
    October 31, 2017
    Posted on October 31, 2017 at 22:59

    Is 

    ADXL345_CS_Pin GPIO PE3 on the STM32F4?

    Zek_De
    Senior
    October 31, 2017
    Posted on October 31, 2017 at 23:26

    I see that is about adxl345 board companent arrays.Lis is working.

    Zek_De
    Senior
    October 31, 2017
    Posted on October 31, 2017 at 23:27

    No ,it was on different pin

    Zek_De
    Senior
    October 31, 2017
    Posted on October 31, 2017 at 23:30

    My opinion is pull-up resistor is making a trouble  or maybe R4 resistor(there is on the board)

    Luke Dempsey
    Visitor II
    November 2, 2017
    Posted on November 02, 2017 at 02:49

    Just confirming, you are using the STM32F4DISCOVERY board with the LIS3DSH MEMS (and you are trying to use the LIS3DSH) ?

    Datasheet:

    http://www.st.com/content/ccc/resource/technical/document/user_manual/70/fe/4a/3f/e7/e1/4f/7d/DM00039084.pdf/files/DM00039084.pdf/jcr:content/translations/en.DM00039084.pdf

     

    I don't see a pull-up resistor being used between the MEMS and STM32?

    Zek_De
    Senior
    November 12, 2017
    Posted on November 12, 2017 at 08:49

    Hi Luke,

    Sorry my late response,I only got a job and I was very busy.I used ADXL345(this is why failure).LIS3DSH is successful.