|
各位技术大牛,我现在有一个问题想请教大家一下,我用的是STM32F407ZGT6作为MCU,用中显的SDW显示屏(触摸屏),显示屏与MCU之间是通过串口5进行通讯,但在通讯过程中,数据接收不稳定,还有丢帧的现象,已经搞了两天了,还没有搞好,心都急了,想请教如何处理丢帧的现象?我用的是中断处理方式,数据帧的格式为:数据帧格式为"AA 7C 05 <StrNUM> <Str> CC 33 C3 3C" 或“AA 78 <KEYCODE> CC 33 C3 3C ”, StrNUM 表示键盘输入的字符数,Str表示键盘输入的字符的ASCII码,帧的长度不定。
/*********************************************************************************************************
* 函 数 名: USARTx_IRQHandler
* 功能说明: 串口中断服务程序。
* 形 参: 无
* 返 回 值: 无
// **********************************************************************************************************/
void UART5_IRQHandler(void)
{
/* 处理接收中断 */
if (USART_GetITStatus(UART5, USART_IT_RXNE) == SET)
{
if(ucRXData[ubRxIndex - 1]== 0xAA) //帧头
{
BufferEx = ubRxIndex - 1;
}
if(ubRxIndex < 256)
{
/* 接收数据 */
ucRXData[ubRxIndex++] = USART_ReceiveData(UART5);
if((ucRXData[ubRxIndex - 4] == 0XCC)&&(ucRXData[ubRxIndex - 3]==0x33)&&(ucRXData[ubRxIndex - 2]==0xC3)
&&(ucRXData[ubRxIndex-1]==0x3c)) // 帧尾
{
KEYFLAG=1;
ubRxIndex = 0;
BufferEx=0;
Delay(1000000);
}
}
}
else
{
/* 禁止接收中断 */
USART_ITConfig(UART5, USART_IT_RXNE, DISABLE);
}
/* 处理发送缓冲区空中断 */
if (USART_GetITStatus(UART5, USART_IT_TXE) == SET)
{
if (ubTxIndex < 256)
{
/* 发送数据 */
//USART_SendData(UART5,ucTXData(Buffer++));
USART_Puts(ucTXData,UART5);
}
else
{
/* 禁止发送缓冲区空中断 */
USART_ITConfig(UART5, USART_IT_TXE, DISABLE);
}
}
}
串口5初始化如下:
void InitLCD(void)
{
GPIO_InitTypeDef GPIO_InitStructure; // IO口
USART_InitTypeDef USART_InitStructure; // 串口
NVIC_InitTypeDef NVIC_InitStructure; //优先级
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE); // 打开GPIOC、GPIOD时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); //打开UART5时钟
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); //TX5
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); //RX5
/*配置串口5*/
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* 输出类型为推挽 */
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; /* 内部上拉电阻使能 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; /* 复用模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200; /* 波特率 */
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
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;
USART_Init(UART5, &USART_InitStructure);
/*使能串口中断*/
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); //使能串口中断
USART_Cmd(UART5, ENABLE);
USART_ClearFlag(UART5, USART_FLAG_TC);
} |
|