AnsweredAssumed Answered

stm32F4 acclerometer lisd302dl issue (using firmware)

Question asked by ali.med on Sep 10, 2014
Latest reply on Sep 11, 2014 by Clive One
Hi guys,
I am using lisd302dl on stm32f4 discovery. I read it s datasheet then went through the firmware to get it to work.
I came out with this is code which doesn't seem to work.  All i get is 0 for x ,y and z when i debugg the programm.Can you please enlighten me?
Thank you for your interest.

#include "stm32f4_discovery_lis302dl.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include <stdio.h>
 
uint8_t buffer[6];
int x = 22;
int y = 22;
int z = 22;
 
void InitMems() {
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;
 
    /* Enable the SPI periph */
    RCC_APB2PeriphClockCmd(LIS302DL_SPI_CLK, ENABLE);
 
    /* Enable SCK, MOSI and MISO GPIO clocks */
    RCC_AHB1PeriphClockCmd(
            LIS302DL_SPI_SCK_GPIO_CLK | LIS302DL_SPI_MISO_GPIO_CLK
                    | LIS302DL_SPI_MOSI_GPIO_CLK, ENABLE);
 
    /* Enable CS  GPIO clock */
    RCC_AHB1PeriphClockCmd(LIS302DL_SPI_CS_GPIO_CLK, ENABLE);
 
    /* Enable INT1 GPIO clock */
    RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT1_GPIO_CLK, ENABLE);
 
    /* Enable INT2 GPIO clock */
    RCC_AHB1PeriphClockCmd(LIS302DL_SPI_INT2_GPIO_CLK, ENABLE);
 
    GPIO_PinAFConfig(LIS302DL_SPI_SCK_GPIO_PORT, LIS302DL_SPI_SCK_SOURCE,
            LIS302DL_SPI_SCK_AF);
    GPIO_PinAFConfig(LIS302DL_SPI_MISO_GPIO_PORT, LIS302DL_SPI_MISO_SOURCE,
            LIS302DL_SPI_MISO_AF);
    GPIO_PinAFConfig(LIS302DL_SPI_MOSI_GPIO_PORT, LIS302DL_SPI_MOSI_SOURCE,
            LIS302DL_SPI_MOSI_AF);
 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 
    /* SPI SCK pin configuration */
    GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_SCK_PIN;
    GPIO_Init(LIS302DL_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
 
    /* SPI  MOSI pin configuration */
    GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MOSI_PIN;
    GPIO_Init(LIS302DL_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
 
    /* SPI MISO pin configuration */
    GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_MISO_PIN;
    GPIO_Init(LIS302DL_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
 
    /* SPI configuration -------------------------------------------------------*/
    SPI_I2S_DeInit(LIS302DL_SPI);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_Init(LIS302DL_SPI, &SPI_InitStructure);
 
    /* Enable SPI1  */
    SPI_Cmd(LIS302DL_SPI, ENABLE);
 
    /* Configure GPIO PIN for Lis Chip select */
    GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_CS_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LIS302DL_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
 
    /* Deselect : Chip Select high */
    GPIO_SetBits(LIS302DL_SPI_CS_GPIO_PORT, LIS302DL_SPI_CS_PIN);
 
    /* Configure GPIO PINs to detect Interrupts */
    GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT1_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(LIS302DL_SPI_INT1_GPIO_PORT, &GPIO_InitStructure);
 
    GPIO_InitStructure.GPIO_Pin = LIS302DL_SPI_INT2_PIN;
    GPIO_Init(LIS302DL_SPI_INT2_GPIO_PORT, &GPIO_InitStructure);
 
}
 
int main(void) {
    InitMems();
    while (1) {
        LIS302DL_Read(buffer, LIS302DL_OUT_X_ADDR, 6);
 
        x = buffer[0];
        y = buffer[2];
        z = buffer[4];
         
}

Outcomes