当前位置:网站首页>单片机串口数据处理(2)——uCOSIII+循环队列接收数据
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
2022-04-23 03:56:00 【Chenxr32】
上一篇文章介绍了串口发送数据的优化方法,使用中断的方式发送数据可以提高系统实时性。这次介绍串口接收数据的方法。新的数据接收方法结合了uCOS-III和循环队列,有较好的实时性。同时,使用STM32的总线空闲中断判断数据包接收完毕,使用状态机检查数据包正误。
配置USART时要使能总线空闲中断,当MCU检测到串口总线上有一个字节的时间没有接收数据时便触发中断。在中断处理函数中必须软件清除中断标志位才能避免反复进入空闲中断,具体方法为先读USART_SR,然后读USART_DR。示例代码如下:
void USARTInit(void)
{
/*
...
省略部分串口配置代码
*/
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);//开启数据寄存器非空中断
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); //开启总线空闲中断
USART_Cmd(USART1,ENABLE);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_IDLE)==SET)
{
ch=USART1->SR;
ch=USART1->DR;//清除总线空闲标志
}
}
程序因串口数据寄存器非空进入中断时,读取串口接收的字节并存入循环队列,开始记录数据包字节数。当程序因串口总线空闲进入中断时,发布(OSTaskQPost())数据包首地址及数据包大小到数据处理任务(状态机)的消息队列中,记录当前循环队列写指针(RXqueue.pwrite)的值作为下一个数据包的首地址,清零数据包字节计数变量。
void USART_IRQHandler(void)
{
unsigned char ch;
OS_ERR err;
static unsigned char* p_write=RXqueue.pwrite;
static OS_MSG_SIZE rxcnt=0;
OSIntEnter();
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
ch=USART1->DR;
if(RXqueue.FullCheck()==0)
{
*RXqueue.pwrite=ch;
if(RXqueue.pwrite==&RXqueue.arr[QUEUE_SIZE-1])
RXqueue.pwrite=RXqueue.arr;
else
RXqueue.pwrite++;
rxcnt++;
}
}
if(USART_GetITStatus(USART1,USART_IT_IDLE)==SET)
{
ch=USART1->SR;
ch=USART1->DR;
OSTaskQPost(&FiniteStatesTaskTCB,p_write,rxcnt,OS_OPT_POST_FIFO,&err);
rxcnt=0;
p_write=RXqueue.pwrite;
}
if(USART_GetITStatus(USART1,USART_IT_TXE)==SET)
{
if(TXqueue.EmptyCheck()==0)
{
USART1->DR=*TXqueue.pread;
if(TXqueue.pread==&TXqueue.arr[QUEUE_SIZE-1])
TXqueue.pread=TXqueue.arr;
else
TXqueue.pread++;
}
else
USART_ITConfig(USART1,USART_IT_TXE,DISABLE);
}
OSIntExit();
}
数据处理任务任务中,程序阻塞等待获得消息,一则消息被发布后数据处理任务获得消息并开始处理数据。任务阻塞等待消息时,CPU可以处理其他任务,当消息发布后,数据处理任务可以立即响应,有较好的实时性。
void FiniteStatesTask(void *arg)
{
OS_ERR err;
unsigned char *delta;//接收数据包首地址
OS_MSG_SIZE size;//接收数据包大小
CPU_TS ts;
unsigned char flag=0;
while(DEF_ON)
{
delta=(unsigned char*)OSTaskQPend(0,OS_OPT_PEND_BLOCKING,&size,&ts,&err);//任务阻塞等待消息
while(size--)
{
TXqueue.PutData(RXqueue.GetData());//将接收的数据存入发送缓冲队列
flag=receiveFiniteStates(*delta);//状态机处理数据
if(delta==&RXqueue.arr[QUEUE_SIZE-1])
delta=RXqueue.arr;
else
delta++;
}
if(flag==1)
{
flag=0;
USART1_SendChar();//如果数据处理无误,则把接收到的数据传回去
}
}
}
经过试验,波特率115200,数据包发送间隔1ms情况下没有丢包。当我把波特率设为921600时,无论发送间隔多长,数据丢包都很严重,经过硬件仿真发现有时候一包数据都接不全,我初步判断可能是波特率过高造成TTL电平不稳定。
如果有更好的串口数据接收防丢包的方法,欢迎留言。
工程代码下载:
https://download.csdn.net/download/qdchenxr/10958685
参考资料:
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://blog.csdn.net/QDchenxr/article/details/87166286
边栏推荐
- Express中间件①(中间件的使用)
- Digital image processing third edition Gonzalez notes Chapter 2
- vscode删除卸载残余
- 51 single chip microcomputer: D / a digital to analog conversion experiment
- Vs studio modifies C language scanf and other errors
- [AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
- 秒杀所有区间相关问题
- ROS series (IV): ROS communication mechanism series (3): parameter server
- What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
- Identificateur, mot - clé, type de données
猜你喜欢
Alibaba cloud IOT transfer to PostgreSQL database scheme
Let matlab2018b support the mex configuration of vs2019
Second kill all interval related problems
Express中间件②(中间件的分类)
阿里云IoT流转到postgresql数据库方案
[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
网络原理 | TCP/IP中的连接管理机制 重要协议与核心机制
Opencv4 QR code recognition test
ROS series (4): ROS communication mechanism series (4): topic communication practice
随机推荐
QtSpim手册-中文翻译
硬核拆芯片
基于PHP的代步工具购物商城
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
Basic introduction to spot gold
ROS series (IV): ROS communication mechanism series (1): topic communication
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
[AI vision · quick review of NLP natural language processing papers today, issue 28] wed, 1 Dec 2021
Why is it necessary to divide the variance by 255^2 when adding Gaussian noise using the imnoise function of MATLAB
Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
【BIM入门实战】Revit中的墙体层次以及常见问题解答
Seekbar custom style details
PolarMask is not in the models registry
QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
Let matlab2018b support the mex configuration of vs2019
UDP协议与TCP协议
ERROR: Could not find a version that satisfies the requirement win32gui
ROS series (III): introduction to ROS architecture
Key point detection of human hand based on mediapipe