cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery Accelerometer SPI Problem

totally
Associate II
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
17 REPLIES 17
Posted on December 09, 2012 at 17:18

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

Not going to work, PinSource is an index, not a bit vector. Break into multiple lines.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
totally
Associate II
Posted on December 09, 2012 at 17:38

Cool, thanks a lot! Works fine now

amirpatel7625
Associate
Posted on July 17, 2013 at 15:11

Hi

does this code work? I've been struggling for 2 days to read the accelerometer data and I keep just getting 0xFF in my out rexeived buffer.

 

Am I doing something wrong?

Posted on July 17, 2013 at 16:40

Am I doing something wrong?

Apparently.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ahsankhushe
Associate II
Posted on August 28, 2013 at 19:50

Hi

Can any one tell me how to get the acceleration value ? I want to calculate the distance by using it.

Thanks.

crt2
Associate II
Posted on August 29, 2013 at 10:03

Above code with small correction gets you a number out. Distance through acceleration? This is something we can discuss about nicely. You do know that acceleration is 0 when speed is constant? This might get you into problem - just a thought.

ahsankhushe
Associate II
Posted on August 29, 2013 at 10:22

Yes, i know this fact but what about if we store the previous acceleration and when speed becomes constant we can use that previously stored value of acceleration ?

crt2
Associate II
Posted on August 29, 2013 at 12:58

Honestly by asking this question my only response can be: go check the basic physics book again. What you could do is calculate speed or something and go with that...

frankmeyer9
Associate II
Posted on August 29, 2013 at 14:37

Reducing the requirements from ''odometer'' to ''step counter'' would make it sound more realistic ...