当前位置:网站首页>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
边栏推荐
- 列表、元组、字典和集合的区别
- [mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
- matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
- Process seven state transition diagram
- ROS series (IV): ROS communication mechanism series (3): parameter server
- Raspberry pie 3B logs into the wired end of Ruijie campus network through mentohust, creates WiFi (open hotspot) for other devices, and realizes self startup at the same time
- 减治思想——二分查找详细总结
- 作为一名码农,女友比自己更能码是一种什么体验?
- What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
- As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
猜你喜欢
Process seven state transition diagram
As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
Add the compiled and installed Mysql to the path environment variable
Writing latex with vscode - the latest tutorial 2022 / 4 / 17
The great gods in acmer like mathematics very much
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
Let matlab2018b support the mex configuration of vs2019
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
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
随机推荐
CRF based medical entity recognition baseline
[Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)
(valid for personal testing) compilation guide of paddedetection on Jetson
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
Using VBA interval to extract one column from another in Excel
Solve the technical problems in seq2seq + attention machine translation
Overview of knowledge map (II)
Opencv4 QR code recognition test
STM32单片机ADC规则组多通道转换-DMA模式
What if win10 doesn't have a local group policy?
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
LabVIEW 小端序和大端序区别
Who will answer the question?
Numpy's broadcasting mechanism (with examples)
MySQL 2013 lost connection to MySQL server during query
Thought of reducing Governance -- detailed summary of binary search
Hard core chip removal
小红书被曝整体裁员20%,大厂之间内卷也很严重
Summary of knowledge map (I)