|
stm32f107 USART3数据接收错误问题
[复制链接]
芯片用的是stm32f107vb,使用USART3,PB10、PB11,不需要重映射
一开始用的原来103的启动文件startup_stm32f10x_md.s,预定义为STM32F10X_MD,结果通信正常。
现在改为startup_stm32f10x_cl.s,预定义也改为STM32F10X_CL。其他没变,结果接收数据错误。
现在串口中断中自发自收。
在串口调试助手中发送01,接收E0,发送02,接收00,发送13接收00,发送0A,接收1C,发送的同一个数,对应接收的同一个数
就改了启动文件和预定义的那个,程序不变如下
void uart_init(u32 bound)
{
USART_InitTypeDef USART_InitStructure;
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound; //一般设置为115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
STM_EVAL_COMInit(COM3, &USART_InitStructure);
#if EN_USART3_RX //如果使能了接收
USART3_NVIC();
#endif
}
u16 USART_RX_STA=0; //接收状态标记
void USART3_NVIC()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(EVAL_COM3, USART_IT_RXNE, ENABLE);//开启中断
}
void STM_EVAL_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM], ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM];
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM];
GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure);
USART_Init(COM_USART[COM],USART_InitStruct); //初始化串口
//使能串口
USART_Cmd(COM_USART[COM], ENABLE);
}
void USART3_IRQHandler(void) //串口3中断服务程序
{
u8 Res;
if(USART_GetITStatus(EVAL_COM3, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(EVAL_COM3);//(USART3->DR); //读取接收到的数据
USART_SendData(EVAL_COM3,Res); //串口自发自收
// USART_SendData(EVAL_COM3,USART_ReceiveData(EVAL_COM3));
if((USART_RX_STA&0x8000)==0)//接收未完成
{
if(USART_RX_STA&0x4000)//接收到了0x0d
{
if(Res!=0x0a)
USART_RX_STA=0;//接收错误,重新开始
else
USART_RX_STA|=0x8000; //接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)
USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))
USART_RX_STA=0;//接收数据错误,重新开始接收
}
}
}
}
}
请路过的大神帮忙看看
|
|