STM32间SPI通信收到乱码
发送主机函数
int main(void)
{
int i=0;
unsigned char SPI_TxBuffer[]="MR.ROCK";
USART1_Configuration();
SPI_Configuration();
while(SPI_TxBuffer[i]!='\0')
{
SPI_SendByte(SPI_TxBuffer[i++]);
}
while(1);
}
SPI相关初始化
//SPI的GPIO和SPI初始化
void SPI_Configuration(void)
{
//定义GPIO,SPI结构体
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
//开PA口复用时钟,初始化系统时钟
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO|RCC_APB2Periph_SPI1,ENABLE);
//配置PA5,PA6,PA7即SPI1__SCK,SPI1_miso,SPI1_mosi
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//配置PC4即SPI1_NSS
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//PC4片选信号
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
//配置SPI1
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
// printf("SPI初始化成功!\r\n");
}
//送字函数
u8 SPI_SendByte(u16 byte)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)!=SET);
SPI_I2S_SendData(SPI1,byte);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
return SPI_I2S_ReceiveData(SPI1);
}
从机用中断接收
//SPI的GPIO和SPI初始化
void SPI_Configuration(void)
{
//定义GPIO,SPI结构体
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
NVIC_InitTypeDefNVIC_InitStructure;
//开PA口复用时钟,初始化系统时钟
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO|RCC_APB2Periph_SPI1,ENABLE);
//配置PA5,PA6,PA7即SPI1__SCK,SPI1_miso,SPI1_mosi
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//配置PC4即SPI1_NSS
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//PC4片选信号
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
//进行SPI1的中断配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //优先级为第0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//配置SPI1
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode=SPI_Mode_Slave;
SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_Init(SPI1,&SPI_InitStructure);
SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
SPI_Cmd(SPI1,ENABLE);
printf("SPI初始化成功!\r\n");
}
//送字函数
u8 SPI_SendByte(u16 byte)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)!=SET);
SPI_I2S_SendData(SPI1,byte);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
return SPI_I2S_ReceiveData(SPI1);
}
中断处理函数
void SPI1_IRQHandler(void)
{
int i=0;
flag=0;
if(SPI_I2S_GetITStatus(SPI1,SPI_I2S_IT_RXNE)==SET)
{
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==SET)
{
SPI_RxBuffer[i++]=SPI_SendByte(0xffff);
}
SPI_I2S_ClearITPendingBit(SPI1,SPI_I2S_IT_RXNE);
}
}
主函数
int main(void)
{
USART1_Configuration();
SPI_Configuration();
while(1)
{
while(SPI_RxBuffer[flag]!='\0')
printf("%c",SPI_RxBuffer[flag++]);
}
} STM32 乱码 SPI
[解决办法]
主从模式是否设置对,SPI配置是否正确,延时时间是否合适,检查这些问题。
[解决办法]
SPI模式是否配对了,时钟先不要调太高