本帖最后由 xiepengchenghn 于 2010-8-27 16:48 编辑
小弟不才,刚学STM32的SPI使用,用SPI1和SPI2进行通信,配置SPI1为主,SPI2为从,但是程序一直没有调通,用的是原厂的开发板STM3210C-EVAL。在查询SPI的RXNE时,总是为RESET,没有收到数据,哪位大哥能帮忙看一下,我对照了例程,没有发现有什么地方不同,百思不得其解,不知道哪个地方疏忽了。
谢谢了。
程序在查询是否有接收到数据的时候出错,
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);,查询接收非空一直都为RESET状态,说明没有收到数据,但是我找不到原因,参看了例程,参照例程来写的程序,只是把SPI3换成了SPI2
代码如下:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define BUFFERSIZE 5
typedef enum {False, True } Status;
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t SPI1_Tx_Buffer[BUFFERSIZE] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint8_t SPI2_Tx_Buffer[BUFFERSIZE] = {0x06, 0x07, 0x08, 0x09, 0x0A};
uint8_t SPI1_Rd_Buffer[BUFFERSIZE], SPI2_Rd_Buffer[BUFFERSIZE];
int TxIndex = 0, RdIndex = 0;
volatile Status TxStatus , RdStatus ;
/* Private function prototypes -----------------------------------------------*/
void RCC_Config(void);
void GPIO_Config(void);
//void SPI_Config(void);
/* Private functions ---------------------------------------------------------*/
SPI_InitTypeDef SPI_InitStruct;
int main(void)
{
RCC_DeInit();
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x00);
SystemInit();
RCC_Config();
GPIO_Config();
// SPI_Config();
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_LSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_InitStruct.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI2, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE);
SPI_Cmd(SPI2, ENABLE);
while (TxIndex < BUFFERSIZE)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, SPI2_Tx_Buffer[TxIndex]);
SPI_I2S_SendData(SPI1, SPI1_Tx_Buffer[TxIndex++]);
// while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
SPI2_Rd_Buffer[RdIndex] = SPI_I2S_ReceiveData(SPI2);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
SPI1_Rd_Buffer[RdIndex++] = SPI_I2S_ReceiveData(SPI1);
}
TxIndex = 0;
RdIndex = 0;
while (TxIndex < BUFFERSIZE)
{
if (SPI1_Tx_Buffer[TxIndex] != SPI2_Rd_Buffer[TxIndex])
{
TxStatus = False;
break;
}
else
{
TxStatus = True;
}
}
while (RdIndex < BUFFERSIZE)
{
if (SPI1_Rd_Buffer[RdIndex] != SPI2_Tx_Buffer[RdIndex])
{
RdStatus = False;
break;
}
else
{
RdStatus = True;
}
}
}
void RCC_Config(void)
{
SystemInit();
/*注意此处是否要对PCLK2分频处理因为PCLK1最大为36MHz,两个SPI的时钟频率是否要一致*/
// RCC_PCLK2Config(RCC_HCLK_Div2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOA, ENABLE);
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_Init( GPIOA, &GPIO_InitStruct);
}
| |
|