我想创建固件stm32f4发现wich闪烁的灯,当董事会移动但是SPI_I2S_ReceiveData
总是返回0xff或0x00。我认为问题出在我的SPI初始化中,但我不知道具体在哪里。
这是我的密码。
#include <stm32f4xx.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_spi.h>
#include <stm32f4xx_i2c.h>
void InitLEDs(void);
void InitSPI(void);
void Delay(int iTicks);
void DiodeFlash(void);
unsigned short ReadACCELEROMETER(void);
unsigned char WriteSPI(unsigned char cbData);
int main(void)
{
unsigned short cbPrevRead = 0;
unsigned short cbCurrentRead = 0;
InitLEDs();
InitSPI();
while(1)
{
cbCurrentRead = ReadACCELEROMETER();
if (cbCurrentRead != cbPrevRead)
{
DiodeFlash();
cbPrevRead = cbCurrentRead;
}
}
}
void InitLEDs(void)
{
GPIO_InitTypeDef PortD;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
PortD.GPIO_Mode = GPIO_Mode_OUT;
PortD.GPIO_OType = GPIO_OType_PP;
PortD.GPIO_PuPd = GPIO_PuPd_NOPULL;
PortD.GPIO_Speed = GPIO_Speed_100MHz;
PortD.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_Init(GPIOD, &PortD);
}
void InitSPI(void)
{
GPIO_InitTypeDef PortA;
GPIO_InitTypeDef PortE;
SPI_InitTypeDef SPI1Conf;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
PortA.GPIO_Mode = GPIO_Mode_AF;
PortA.GPIO_OType = GPIO_OType_PP;
PortA.GPIO_Speed = GPIO_Speed_50MHz;
PortA.GPIO_PuPd = GPIO_PuPd_NOPULL;
PortA.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init(GPIOA, &PortA);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
PortE.GPIO_Mode = GPIO_Mode_OUT;
PortE.GPIO_OType = GPIO_OType_PP;
PortE.GPIO_Speed = GPIO_Speed_50MHz;
PortE.GPIO_PuPd = GPIO_PuPd_NOPULL;
PortE.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOE, &PortE);
GPIOE->ODR = 0x0008; // Disable CS
SPI1Conf.SPI_DataSize = SPI_DataSize_8b;
SPI1Conf.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI1Conf.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI1Conf.SPI_FirstBit = SPI_FirstBit_MSB; // ACCELEROMETER PROTOCOL
SPI1Conf.SPI_Mode = SPI_Mode_Master;
SPI1Conf.SPI_CPHA = SPI_CPHA_2Edge;
SPI1Conf.SPI_CPOL = SPI_CPOL_High;
SPI1Conf.SPI_CRCPolynomial = 7;
SPI1Conf.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI1, &SPI1Conf);
SPI_Cmd(SPI1, ENABLE);
WriteSPI(0x23); WriteSPI(0xc9);
WriteSPI(0x20); WriteSPI(0x97);
WriteSPI(0x24); WriteSPI(0x00);
WriteSPI(0x10); WriteSPI(0x00);
WriteSPI(0x11); WriteSPI(0x00);
WriteSPI(0x12); WriteSPI(0x00);
}
void Delay(int iTicks)
{
while ((iTicks--) > 0);
}
void DiodeFlash(void)
{
GPIO_Write(GPIOD, 1UL << 12);
Delay(100000);
GPIO_Write(GPIOD, 1UL << 13);
Delay(100000);
GPIO_Write(GPIOD, 1UL << 14);
Delay(100000);
GPIO_Write(GPIOD, 1UL << 15);
Delay(100000);
GPIO_Write(GPIOD, 0x0000);
}
unsigned short ReadACCELEROMETER(void)
{
unsigned short nAxisX = 0x0000;
unsigned short nAxisY = 0x0000;
unsigned short nAxisZ = 0x0000;
unsigned char cbAddress = 0x80;
//**********************************************************
// Forming X Value
WriteSPI(cbAddress | 0x0f);
nAxisX |= WriteSPI(0x00);
WriteSPI(cbAddress | 0x2A);
nAxisX |= WriteSPI(0x00) << 8;
//**********************************************************
// Forming Y Value
WriteSPI(cbAddress | 0x2B);
nAxisX |= WriteSPI(0x00);
WriteSPI(cbAddress | 0x2C);
nAxisX |= WriteSPI(0x00) << 8;
//**********************************************************
// Forming Z Value
WriteSPI(cbAddress | 0x2D);
nAxisX |= WriteSPI(0x00);
WriteSPI(cbAddress | 0x2E);
nAxisX |= WriteSPI(0x00) << 8;
return nAxisX ^ nAxisY ^ nAxisZ;
}
unsigned char WriteSPI(unsigned char cbData)
{
unsigned char cbResult = 0x00;
GPIOE->ODR = 0x0000; // Enable CS
// Wait for ready status
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
SPI_I2S_SendData(SPI1, cbData);
// Wait for ready status
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);
cbResult = SPI_I2S_ReceiveData(SPI1);
GPIOE->ODR = 0x0008; // Disable CS
return cbResult;
}
最佳答案
首先,你没有指定你的加速计我能猜到,那是圣·利斯克斯。
在这种情况下,有不正确的数据传输到加速计。
正确的写入顺序:
-激活芯片选择
-写入寄存器号
-写入寄存器值
-停用芯片选择
使用类似的顺序读取每个寄存器。
下一步,ST不推荐低层SPI传输的算法:
不要使用BSY标志来处理每个数据传输或接收最好改用TXE和RXNE标志
(见参考手册)简单使用
SPIx->DR = out;
while (!(SPIx->SR & SPI_SR_RXNE)) ;
return SPIx->DR;
另外,我正在使用LIS3DH,CPOL=0&CPHA=0spi设置(我不知道,它将如何处理CPOL=1,CPHA=1)。
提示:要检查SPI通信,请尝试读取我注册的用户-它启用了alawys并且始终具有已知值。