When a corresponding interrupt occurs on serial port 1, it will jump to the function execution. A small receiving protocol is designed here (not defined by the system): through this function, an array USART_RX_BUF[ ] and a receiving status register USART_RX_STA are used to manage the receiving of serial port data. The maximum value of USART_RX_BUF[ ] is 64, which means that the maximum data received at one time cannot exceed 64 bytes. USART_RX_STA is a receiving status register, and the definition of each bit is shown in the table:
(Note: This is a protocol designed by the author. How to judge whether the serial port has received a group of data? Since the length of the data received each time is different, the least is 3 8-bit data, and the most is more than ten. The number of data is uncertain and irregular. Is there any good way to make it receive completely?
The design ideas of the protocol are as follows:
When receiving data sent from the computer, the received data is saved in USART_RX_BUF, and the number of valid data received is counted in the receive status register (USART_RX_STA). When the first byte 0X0D of the carriage return (0X0D, 0X0A) is received, the flag bit, that is, the sixth position, is 1, and the counter will no longer increase, waiting for the arrival of 0X0A. If 0X0A does not arrive, it is considered that this reception has failed and the next reception will be restarted. If 0X0A is successfully received, the seventh bit of USART_RX_STA is marked, so that one reception is completed, and the bit is cleared by other programs, so as to start the next reception. If 0X0D is not received for a long time, then when the received data exceeds 64, the previous data will be discarded and received again. Since this register is defined by the author, we may be easily confused when we understand it. In fact, this is similar to a normal register, except that the author defines an 8-bit number and stipulates the definition of each bit. The first two bits are two flag bits, and the last six bits are used for counting, which is easy to understand. The function code is as follows:
- void USART1_IRQHandler(void)
- {
- u8 res;
- if(USART1->SR&(1<<5))//Receive data USART1 should be a structure defined by the author, containing various registers
- {
- res=USART1->DR;
- if((USART_RX_STA&0x80)==0)//receiving is not completed
- {
- if(USART_RX_STA&0x40) //Received 0x0d
- {
- if(res!=0x0a)USART_RX_STA=0; //Receive error, restart
- else USART_RX_STA|=0x80; //Receiving completed
- }else //Has not received 0X0D
- {
- if(res==0x0d)USART_RX_STA|=0x40;
- else
- {
- USART_RX_BUF[USART_RX_STA&0X3F]=res;
- USART_RX_STA++;
- if(USART_RX_STA>63)USART_RX_STA=0; //Receive data error, restart receiving
- }
- }
- }
- }
- }
In fact, we can use the agreement method
Previous article:STM32-Serial port timeout judgment method receives unknown length data
Next article:STM32-Key Input Experiment Study Notes
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- 5G indoor base stations will be released soon, and it is expected that each household will have a small base station, which will subvert the existing home Internet access methods.
- How to consider the impact of audio analog signals after being transmitted through a 100m long cable?
- Angle sensor
- Transistor Selection
- Fancy PCB Exhibition (1)
- Why is it said that 90% of EMC is designed? (Long article)
- Chinese mobile phones continue to sell well in India, Vivo surpasses Samsung to become the second largest brand
- Share CC2541 Bluetooth learning about ADC
- Summary of frequently asked questions about oscilloscopes (Part 2)
- MSP430 LCD5110 driver programming example