当前位置:网站首页>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
边栏推荐
- Alibaba cloud IOT transfer to PostgreSQL database scheme
- Opencv4 QR code recognition test
- Vs studio modifies C language scanf and other errors
- Xiaomi, qui a établi le plus grand volume de ventes de téléphones portables domestiques sur le marché d'outre - mer, se concentre de nouveau sur le marché intérieur
- ROS series (IV): ROS communication mechanism series (6): parameter server operation
- Man's life
- Qtspim manual - Chinese Translation
- OpenCV----YOLACT实例分割模型推理
- Hard core chip removal
- Writing latex with vscode - the latest tutorial 2022 / 4 / 17
猜你喜欢
![[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022](/img/94/5fa8bf57dff45a1ba108edb46cf6c8.png)
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022

Key point detection of human hand based on mediapipe

ROS series (IV): ROS communication mechanism series (1): topic communication

Express middleware ① (use of Middleware)

What if win10 doesn't have a local group policy?

Identificateur, mot - clé, type de données

MATLAB lit plusieurs diagrammes fig et les combine en un seul diagramme (sous forme de sous - Diagramme)

AI CC 2019 installation tutorial under win10 (super detailed - small white version)
![[mapping program design] coordinate azimuth calculation artifact (version C)](/img/2b/e640f3e2702f80d003fa3dd5c4158d.png)
[mapping program design] coordinate azimuth calculation artifact (version C)

QtSpim手册-中文翻译
随机推荐
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
2021-09-03 crawler template (only static pages are supported)
VS Studio 修改C語言scanf等報錯
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
[string] ranking of country names ----- problem solving notes
As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
硬核拆芯片
Software testing process
VSCode配置之Matlab极简配置
UDP protocol and TCP protocol
Openvino only supports Intel CPUs of generation 6 and above
The great gods in acmer like mathematics very much
Machine translation baseline
VHDL语言实现32位二进制数转BCD码
现货黄金基本介绍
减治思想——二分查找详细总结
Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
STM32单片机ADC规则组多通道转换-DMA模式