我正在尝试将 SPI 接口与 SPI ADXL345 传感器和 STM32F072 Nucleo 板一起使用。我想做一个非常基本的读取操作。现在读取功能在等待 RXNE 信号时被阻塞。但 RXNE 从未设置。SPI 总是读取 0。我在这里使用 IAR 是我的代码,我也使用这些函数来写入和读取 SPI。
int main()
{
USART2_Init();
SPIx_Init();
while (1) {
adxl_baslat();
receiveData[0] = readData(0x32);
receiveData[1] = readData(0x33);
receiveData[2] = readData(0x34);
receiveData[3] = readData(0x35);
receiveData[4] = readData(0x36);
receiveData[5] = readData(0x37);
x = (receiveData[1] << 8) | (receiveData[0]);
y = (receiveData[3] << 8) | (receiveData[2]);
z = (receiveData[5] << 8) | (receiveData[4]);
xg = (float)x * 0.0078;
yg = (float)x * 0.0078;
zg = (float)x * 0.0078;
sprintf(buffer, "xg: %f yg: %f zg: %f \r\n", xg, yg, zg);
USART_PutString(buffer);
}
}
void SPIx_Init()
{
SPI_InitTypeDef SPI_InitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
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);
SPI_DataSizeConfig(SPI1, ENABLE);
SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);
// Step 2: Initialize GPIO
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
// GPIO pins for MOSI, MISO, and SCK
GPIO_InitStruct.GPIO_Pin = SPI_PIN_MOSI | SPI_PIN_MISO | SPI_PIN_SCK;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_1);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStruct.GPIO_Pin = SPI_PIN_SS;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
}
uint8_t sendByte(uint8_t byteToSend)
{
timeout = TIMEOUT_TIME;
while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) & (timeout != 0)) {
timeout--;
}
SPI_SendData8(SPI1, byteToSend);
timeout = TIMEOUT_TIME;
while ((SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) & (timeout != 0)) {
timeout--;
}
return (uint8_t)SPI_ReceiveData8(SPI1);
}
void writeData(uint8_t address, uint8_t dataToWrite)
{
DATA2 = address | 0x40;
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
sendByte(DATA2);
sendByte(dataToWrite);
GPIO_SetBits(GPIOB, GPIO_Pin_9);
}
uint8_t readData(uint8_t address)
{
address |= 0x40;
address |= 0x80;
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
sendByte(address);
tempByte = sendByte(address);
GPIO_SetBits(GPIOB, GPIO_Pin_9);
return tempByte;
}
void adxl_baslat(void)
{
writeData(0x31, 0x01);
writeData(0x2d, 0x00);
writeData(0x2d, 0x08);
}