cancel
Showing results for 
Search instead for 
Did you mean: 

stm32F4 acclerometer lisd302dl issue (using firmware)

d4ng3r09
Associate II
Posted on September 10, 2014 at 23:16

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];
}

#stm32f4 #discovery #lis302dl
1 REPLY 1
Posted on September 11, 2014 at 01:38

Perhaps because the device defaults into [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/spi%20sending%20data%20adress%20issues&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx&currentviews=26]Power Down mode?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..