当前位置:网站首页>Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
2022-04-23 04:00:00 【Chenxr32】
Real time is very important in embedded development , Optimize MCU Serial port transmission processing method can improve the real-time performance of embedded system . Study on the Internet and experiment in person ( be based on STM32 Single chip microcomputer ) after , I will introduce optimization in two parts MCU Method of sending and receiving data through serial port , Resources will be listed in the second blog . For the first time, the optimization of serial port sending data is introduced .
Sending method 1 :
Mode 1 is adopted “ Death etc. ” The way to send data , That is to say while Wait for the position bit of the word sending completion flag in the loop .
void usartsend(void)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); // Wait until the last byte is sent
USART1->DR=txbuf[cnt]; // The bytes to be sent are stored in the serial port data register
}
void sendcmd(void)
{
for(cnt=0;cnt<100;cnt++)// send out 100 Bytes
{
usartsend();// Call the byte sending function
}
}
In this way while A lot of time is consumed in the loop , and sendcmd Call... Over and over again in the function usartsend Functions are also time-consuming . The baud rate I used in the experiment is 115200, In theory, send 100 Byte data takes about 8.68ms, The time spent in hardware simulation is about 9.45ms. Here 9.54ms Inside ,MCU In addition to sending bytes and while There are no other tasks to deal with besides waiting , Seriously affect the real-time performance of the system .
Sending mode 2 :
Mode 2: send data by interrupt . To send data , Enable the transmission buffer of the serial port to be disconnected in the air , stay ISR Determine whether there is data to send , If there is , Then the bytes to be sent are stored in the serial port data register . When all data is sent, the transmission buffer of the serial port is forbidden to be disconnected in the air .
void usartsend(void)
{
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);// Enable the transmission buffer of the serial port to be disconnected in the air
}
void USART1_IRQHandler(void) // A serial port 1 Of ISR
{
if(USART_GetITStatus(USART1,USART_IT_TXE)==SET)
{
USART1->DR=txbuf[cnt];
cnt++;
if(cnt>=100)
{
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);// Data sent , It is forbidden to interrupt the sending buffer of the serial port in the air
}
}
}
This way is mainly ISR Elapsed time . use 115200 Baud rate experiment , send out 100 Byte data takes about 8.51ms. It takes less time than the theoretical time (8.68ms) The reason may be that the program reached the breakpoint I set before the last byte was sent .
The second method takes less time than the first method . What is more noteworthy is that the second method costs 8.51ms It's piecemeal , The program won't wait 8.51ms, in the meantime MCU Can handle other tasks , The real-time performance of the system is high . The first method is to wait for the program to die 9.54ms,MCU No other tasks can be processed during this period , Real time performance is seriously affected .
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230356325420.html
边栏推荐
- Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
- Using VBA interval to extract one column from another in Excel
- C语言常用字符串处理函数
- Abstract classes, interfaces and common keywords
- Qt程序集成EasyPlayer-RTSP流媒体播放器出现画面闪烁是什么原因?
- [Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)
- [mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
- Set经典小题目
- What if win10 doesn't have a local group policy?
- 减治思想——二分查找详细总结
猜你喜欢
【BIM+GIS】ArcGIS Pro2.8如何打开Revit模型,BIM和GIS融合?
Thought of reducing Governance -- detailed summary of binary search
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 robot papers today, issue 30] Thu, 14 APR 2022
Express middleware ① (use of Middleware)
Express middleware ② (classification of Middleware)
(valid for personal testing) compilation guide of paddedetection on Jetson
Difference between LabVIEW small end sequence and large end sequence
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
为什么推荐你学嵌入式
随机推荐
Nel ASA: her ø Ya facility in Norway officially opened
The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
[Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)
Qt程序集成EasyPlayer-RTSP流媒体播放器出现画面闪烁是什么原因?
【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
VS Studio 修改C語言scanf等報錯
A sword is a sword. There is no difference between a wooden sword and a copper sword
C language character constant
VSCode配置之Matlab极简配置
Identifier, keyword, data type
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
Solve the technical problems in seq2seq + attention machine translation
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022
ROS series (4): ROS communication mechanism series (4): topic communication practice
Alibaba cloud IOT transfer to PostgreSQL database scheme
【测绘程序设计】坐标方位角推算神器(C#版)
Variables, constants, operators
作为一名码农,女友比自己更能码是一种什么体验?
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism