2023-11-14 02:24 AM - last edited on 2023-11-30 01:15 AM by Mike_ST
Hello,
I have been trying to make a accelerator project by using lis302dl accelorator on STM32f407 vg discovery card. There are three axis which are x, y and z. These have x, y and z variables according to position of lis302dl on the card. I have used SPI1 and E3 pin for CS. When ı observe the values on STM Studio, x and z variables changed everytime which is good. However y variables is always zero, I enabled x, y, z on lis302 dl register though. Can somebody give me idea about what I am doing wrong?
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
uint8_t x,y,z;
uint8_t x_address = 0x29, y_address = 0x2B, z_address = 0x2D, m_data = 0x00;
void GPIO_Config()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); // for CS pin
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStruct.GPIO_OType=GPIO_OType_PP; //
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; //
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOE, &GPIO_InitStruct);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); // SCK pin
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); // MOSI pin
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); // MISO pin
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; //
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
}
void SPI_Config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft|SPI_NSSInternalSoft_Set;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE);
GPIO_SetBits(GPIOE, GPIO_Pin_3); // CS pin locig 1
}
void SPI_Write(uint8_t address, uint8_t data)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_3);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1,address);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1,data);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);
GPIO_SetBits(GPIOE,GPIO_Pin_3);
}
uint8_t SPI_Read(uint8_t address, uint8_t data)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_3);
address = address | 0x80;
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1,address);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1,data);
while(!SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);
return SPI_I2S_ReceiveData(SPI1);
}
void delay(uint32_t time)
{
while(time--);
}
int main(void)
{
GPIO_Config();
SPI_Config();
SPI_Write(0x20,0x67);
while (1)
{
x = SPI_Read(x_address, 0x00);
y = SPI_Read(y_address, 0x00);
z = SPI_Read(z_address, 0x00);
delay(20);
}
}
void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size)
{
return;
}
uint16_t EVAL_AUDIO_GetSampleCallBack(void)
{
return -1;
}