Skip to main content
Visitor II
December 9, 2012
Question

STM32F4 Discovery Accelerometer SPI Problem

  • December 9, 2012
  • 17 replies
  • 3181 views
Posted on December 09, 2012 at 14:41

Hello,

I'm trying to get the MEMS Accelerometer on my STM32F4 Discovery Board to run with a simple code, but I don't make it.

Heres my code:

int main(void)

{

        SystemInit();

        GPIO_InitTypeDef GPIO_InitStructure;

        SPI_InitTypeDef  SPI_InitStructure;

        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE , ENABLE);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

          //Init SPI-Pins

          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

          GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

          GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

          GPIO_Init(GPIOA, &GPIO_InitStructure);

          //Init CS-Pin

          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

          GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

          GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

          GPIO_Init(GPIOE, &GPIO_InitStructure);

          GPIO_SetBits(GPIOE, GPIO_Pin_3);  //Set CS high

          GPIO_PinAFConfig (GPIOA, GPIO_PinSource5 |GPIO_PinSource6 |GPIO_PinSource7 , GPIO_AF_SPI1);

          SPI_I2S_DeInit(SPI1);

          SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

          SPI_InitStructure.SPI_Mode = SPI_Mode_Master ;

          SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b ;

          SPI_InitStructure.SPI_CPOL = SPI_CPOL_High    ;

          SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

          SPI_InitStructure.SPI_NSS = SPI_NSS_Soft ;

          SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;

          SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

          SPI_InitStructure.SPI_CRCPolynomial = 0;

          SPI_Init(SPI1, &SPI_InitStructure);

          SPI_Cmd(SPI1, ENABLE);

          GPIO_ResetBits(GPIOE, GPIO_Pin_3);

          SPI_I2S_SendData(SPI1, 0x20C7);    //Sensor Config

    while(1){

        //Read Y-Axis

        SPI_I2S_SendData(SPI1, 0xAB00);

        SPI_I2S_ReceiveData(SPI1);

    }

I'm running this in Debug-mode only (step by step), so there should not be any timing problems due to missing flag-status requests.

After sending the data, RXNE bit in SPI SR Register is set but the SPI Data Register always contains 0x0000. I have also tried different modes changing CPOL and CPHA, no results.

I am using CooCox IDE

I hope somebody can help!

Thanks,

Alex.

#accelerometer #stm32f4 #spi #metoo
    This topic has been closed for replies.

    17 replies

    Visitor II
    September 14, 2014
    Posted on September 14, 2014 at 09:46

    Hi,

        I am trying to build a project using MEMS accelerometer. I have created a project using your code.It was built successfuly without any error. But i didn't get a clear idea of the functionality. What value it returns in the while loop.

    Visitor II
    September 15, 2014
    Posted on September 15, 2014 at 11:00

    But i didn't get a clear idea of the functionality. What value it returns in the while loop.

     

    I'd suggest to download the datasheet of the MEMS accelerometer chip. Besides of documenting the returned data type (I think it was signed short), it might prove useful in understanding the initialization sequence, and for changes/improvements of the project.

    Graduate II
    September 15, 2014
    Visitor II
    September 18, 2014
    Posted on September 18, 2014 at 12:48

    Sir,

         I am using SM32F4 MB997C Board, the accelerometer  in this board is LIS3DSH,  the firmware STM32F4-Discovery_FW_V1.1.0 contains example only for LIS320DL. Where can I find the firmware version supporting  STM32F4 MB997C (and LIS3DSH MEMS).

    Graduate II
    September 18, 2014
    Posted on September 18, 2014 at 16:20

    I don't know, trying reviewing available materials for the code/driver/functionality you need.

    http://www.st.com/web/en/catalog/tools/PF252688

    http://www.st.com/web/en/catalog/tools/PF255640

    Visitor II
    September 18, 2014
    Posted on September 18, 2014 at 18:01

    Hi Avinash.Punnadiyil,

    Please don't [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4Discovery%20LIS302DL%20or%20LIS3DSH&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=612]Cross-Post.

    Regards. 
    Visitor II
    July 14, 2016
    Posted on July 14, 2016 at 20:50

    Looking for someone to tell me how I can use / access the accelerometer for Virtual pinball machine, nudging the ball.