当前位置:网站首页>STM32 serial port based on Hal library accepts interrupts and idle interrupts
STM32 serial port based on Hal library accepts interrupts and idle interrupts
2022-07-27 04:29:00 【Haoyuehua】
Preface
In communications .UART Due to full duplex communication , It can accept data and send data at the same time and is widely used .
There are many ways to accept data
such as :
<1> Judge according to the terminator , Data is in string form , So generally, the receiver accepted by the serial port is "\r\n" Switch to 16 Base number ascil Code display is 0X0D 0X0A ( Corresponding \r\n)
<2> Timer interrupt , Design " Dog feed semaphore " That is, the idle time of the serial port , The timer after serial port acceptance is continuously decreasing , If it is reduced to zero, it is judged that the acceptance is complete , When receiving the data, feed the dog to refresh the decreasing time .( This is also the common serial port acceptance idea of MCU )
Refer to me 51 The engineering of :
<3>STM32 There are many ways , The first two can be used in some cases .
But the first one only applies to the judgment terminator . I recently made RF module (RF) Data sent by serial port ( A kind of 16 The format of hexadecimal , No, \r\n The terminator ) It's Limited , In addition, the second type needs to be matched with a timer . And I am satisfied with the serial port accepting interrupt and idle interrupt introduced in this writing YYDS!!!
understand
Serial port accepts interrupt :
Enable serial port to accept interrupt RXEN after , The serial port accepts a packet , One frame ( Usually 8 Digit bit , One end bit ) Then enter interrupt .
With a timer 2 For example
Can make
__HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);The actual processing function
USART2_IRQHandler(void)stay HAL The library function cannot be redefined , stay stm32f1xx_it.c in Add __weak Indicates that the weak definition can be rewritten
__weak void USART2_IRQHandler(void)Serial port idle interrupt :
In enabling IDLE , After receiving a frame of data through the serial port , If the serial port remains idle within a byte, the serial port idle interrupt will be triggered
Once again into the USART2_IRQHandler(void)
Can make
__HAL_UART_ENABLE_IT(&huart2,UART_IT_IDLE);It is the same as the interrupt processing function of serial port receiving data . Like the improvement of the second method above , Because the second method doesn't know how long it takes , Because the size of the data transmission interval is unknown , Only one value can be estimated to pass the attenuation “ Dog feed semaphore ” To judge . Again , If the data is too large and keeps sending, it will not be idle .
It is worth noting that , After the serial port is accepted, the idle flag bit must be cleared . Through reading serial port DR Register to clear IDLE Sign a , Otherwise, the program will always trigger idle interrupt .
For example, serial port interrupt processing code ( Take serial port 3 as an example ) Be careful ( use __weak modification hal library stm32f1x_it.c In this rewrite )
void USART3_IRQHandler(void){
uint8_t temp;
if(huart3.Instance->SR & UART_FLAG_RXNE){
Rx_uart3_signal=2;
temp=huart3.Instance->DR;
Rx_uart3_buf[Rx_uart3_cnt++]=temp;
}
else if(huart3.Instance->SR & UART_FLAG_IDLE){
temp=huart3.Instance->DR;
Rx_uart3_signal=0;
}
}adopt Rx_uart3_signal=0 Judge that one acceptance is over , stay while Print data in . The effect is very good for me , The second method is in the form of hardware (stm32 Built in idle interrupt ) Instead of a timer, it realizes packet acceptance .
Reference resources
STM32 Idle interrupt of serial port _csdn_dx The blog of -CSDN Blog _ Serial port idle interrupt
边栏推荐
- Echart柱状图中数据显示在图上方
- sed输出指定行
- Okaleido tiger will log in to binance NFT in the second round, or continue to create sales achievements
- JMeter学习笔记004-CSV文件行数控制循环次数
- Post analysis of Data Analyst
- How CentOS installs mysqldump
- js三种遍历数组的方法:map、forEach、filter
- 452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition
- SkyWalking分布式系统应用程序性能监控工具-中
- 【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
猜你喜欢

Learning route from junior programmer to architect + complete version of supporting learning resources

Is the e-commerce billing system important? How should the platform choose billing service providers?

Influxdb basic understanding

人很话不多,工程师不耍嘴皮子

你了解微信商户分账吗?

Elastic开源社区:开发者招募

【day02】数据类型转换、运算符、方法入门

【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns

Session&Cookie&token
随机推荐
P1438 boring sequence line segment tree + difference
Detailed explanation of TCP protocol knowledge
从零开始C语言精讲篇4:数组
[C language] recursively explain the tower of Hanoi problem
CloudCompare&PCL 匹配点距离抑制
From scratch, C language intensive Lecture 4: array
The external symbol parsed by the method "public: virtual _ucdecl nvinfer1:: yololayerplugin:: ~yololayerplugin (void)" "public: virtual
卷积神经网络——灰度图像的卷积
Brightcove appoints Dan Freund as chief revenue Officer
微服务的feign调用header头被丢弃两种解决方案(附源码)
Preliminary understanding of NiO
Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
Ribbon load balancing strategy and configuration, lazy loading and hungry loading of ribbon
微信input组件添加清除图标,点击清空不生效
Okaleido生态核心权益OKA,尽在聚变Mining模式
Echart柱状图中数据显示在图上方
使用kubesphere图形界面dashboard开启devops功能
JMeter download and installation
【day02】数据类型转换、运算符、方法入门
标准C语言11