好久没有再碰STM32的程序,今天出于需求又把程序捡起来调试了一下,发现问题居多,以下就归纳总结以下:
1.我是用两块STM32F103C8T6进行SPI通讯,一块做主机,一块作为从机;
主机部分代码:
void SPI2_Config(void)//SPI2的传输配置
{
SPI_InitTypeDef SPI_InitStructure;
SPI_Cmd(SPI2, DISABLE);
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
void Spi2_Master_Fun(void)//SPI2端口配置
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//使能spi2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽复用
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15;//sck mosi
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//miso
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//cs
GPIO_Init(GPIOB,&GPIO_InitStructure);
SPI2_Config();
printf("MasterFunInitOver!\r\n");
}
//SPI读写数据:
unsigned char SPIWriteByte(unsigned char Byte)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //等待发送区空
SPI_I2S_SendData(SPI2, Byte); //发送一个byte
//delayms(100);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET); //等待接收完一个byte
return SPI_I2S_ReceiveData(SPI2); //返回收到的数据
}
void USART1_IRQHandler(void)//将串口的传过来的数据发送给从机
{
u8 k;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//检查指定的USART中断发生与否
{
// k=USART_ReceiveData(USART1);
// printf("Send_Data:%x\r\n",k);
GPIO_ResetBits(GPIOB,GPIO_Pin_12);
SPIWriteByte(USART_ReceiveData(USART1));
GPIO_SetBits(GPIOB,GPIO_Pin_12);
}
}
从机部分代码:
void SPI2_Slaver_Config(void)//从机配置
{
SPI_InitTypeDef SPI_InitStructure;
SPI_Cmd(SPI2, DISABLE);
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/*Enable SPI1.NSS as a GPIO*/
//SPI_SSOutputCmd(SPI1, ENABLE);
SPI_Cmd(SPI2, ENABLE);
SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE);
//spi1_cs_low;
}
/* 中断嵌套控制器配置 */
void NVIC_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 选择中断分组2 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
/* 设置SPI1中断 */
NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Spi2_Slaver_Fun(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//使能spi2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //推挽复用
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_15;//sck mosi
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;//miso
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;//cs
GPIO_Init(GPIOB,&GPIO_InitStructure);
NVIC_config();
SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
SPI2_Slaver_Config();
printf("SlaverFunInitOver!\r\n");
}
void SPI2_IRQHandler(void)//接受数据并且打印到串口
{
u8 i;
if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET)
{
SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
i = SPI_I2S_ReceiveData(SPI2);
printf("Receive_Data:%x\r\n",i);
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, i);
}
延迟函数:
void delayms(u32 i)
{
u32 temp;
SysTick->CTRL=0X01;
SysTick->LOAD=9000*i;
SysTick->VAL=0X00;
do
{
temp=SysTick->CTRL;
}
while((temp&0x01)&&(!(temp&(1<<16))));
SysTick->CTRL=0X00;
SysTick->VAL=0X00;
}
main函数:
#include"stm32f10x.h"
#include "../APP/SPI/SPI.h"
#include "../APP/Usart/Uart.h"
#define Spi_Master 1
#define Spi_Slaver 0
int main(void)
{
Uart1_Init_Config();
#if Spi_Master
Spi2_Master_Fun();
printf("Please Input:\r\n");
while(1)
{
delayms(100);
};
#endif
#if Spi_Slaver
Spi2_Slaver_Fun();
while(1);
#endif
}
接线图:
单片机1:NSS -> 单片机2:NSS
单片机1:SCK-> 单片机2:SCK
单片机1:MISO-> 单片机2:MISO
单片机1:MOSI-> 单片机2:MOSI
实验结果:
虽然进行慢速发没问题,但是将速度加到10ms后就会发生接收数据偏移错误,目前不知道是什么原因,希望有大侠可以告知根号的解决方案。