0 Introduction
Embedded system development has entered the 32-bit era, the 8-bit MCU market has stabilized, and the 32-bit MPU represents the development direction of embedded technology. However, as a representative of low-end applications of embedded systems, 8-bit microcontrollers are still widely used in home appliances, instruments and other fields; and with the continuous development of IC technology, the expansion capability of microcontrollers is getting stronger and stronger, and the development and application of 8-bit microcontrollers are still highly valued.
With the continuous development of network technology and communication technology, the communication capability of microcontrollers is required to be higher and higher. Asynchronous communication technology has a long communication distance, saves costs, and is reliable. In particular, it is widely used in hierarchical, layered and distributed control systems and remote communications due to its increasingly fast communication speed. It is especially suitable for the application direction of single-machine to multi-machine or networking. The currently widely used MCS-51 series and some other dedicated microcontrollers usually have only one UART asynchronous serial communication interface, while in actual application systems (such as in multi-machine communication systems, the host communicates with both the slave and the terminal) often requires multiple serial interfaces. For microcontrollers with three open buses, the usual method is to use Intel 8251 or 8250 Universal Synchronous/Asynchronous Receiver/Transmitter (USART) to expand the system, which increases the hardware overhead and cost of system development. In addition, the three buses of some special microcontrollers (such as ST62T32B) are not open, and the communication interface chip cannot be used to expand the interface, which brings difficulties to multi-machine communication. This article only uses the expansion module inside the 51 microcontroller (an I/O port, a timer T/C and an external interrupt INT) to implement a general software UART, which can receive and send data at baud rates of 4800, 9600, and 19200. No peripheral devices are used, the communication reliability is high, the development cost is saved, and the software is written in C language, which has good portability. The software UART has been used in multiple microcontroller systems, works stably and reliably, and is a communication solution that can be used as a reference.
1 Introduction to Asynchronous Communication
In asynchronous communication, data is usually transmitted in character frames composed of characters (bytes). The character frames are sent frame by frame by the sender and received frame by frame at the receiver through the transmission line. The sender and the receiver can have their own clocks to control the sending and receiving of data. The two clocks are independent of each other and asynchronous. In addition, the sender and the receiver coordinate the sending and receiving of data through the format and baud rate specified by the character frame. The character frame and baud rate are two important indicators, which are selected by the user according to the actual situation.
The character frame consists of four parts: the start bit, the data bit, the parity bit and the stop bit, as shown in Figure 1. The function and structure of each part are as follows:
The start bit is located at the beginning of the character frame, occupies one bit, and puts the data line in the space (logic 0) state, which is used to indicate to the receiving device that the sender starts to send data.
The data bit follows the start bit and can be 5, 6, 7 or 8 bits depending on the character encoding method. In the data transmission process, the low bit is in front and the high bit is in the back. If the transmitted data is ASCII code, 7 bits are often used.
Parity bit is located after the data bit, occupies one bit, and is used to check the correctness of data transmission. There are three options for parity bit, namely odd, even and no parity, which are set by the user according to needs.
Stop bit is located at the end of the character frame, and can occupy 1, 1.5 or 2 bits. In actual application, it is determined by the user according to needs. It corresponds to the mark (logical 1) state and is used to indicate to the receiving end that a frame of information has been sent [1].
2 Design Concept of Universal Software UART
The baud rate and sampling clock of UART are realized by using the T/Cx count timer. For different baud rates, different initial values are assigned to the T/Cx counting timer, and then at the specified baud rate, the software is used to implement the conversion of UART's parallel data to serial data, the generation, sending and receiving of each frame data format. [page]
In this design, the hardware system uses a main crystal oscillator with a legitimate frequency of 12M, and T/Cx records the number of pulses after the oscillation frequency is divided by 12 (that is, the number of machine cycles), that is, each machine cycle increases the T/Cx counter by 1 until it is full and returns to zero, and an overflow interrupt request is automatically generated. Therefore, the formula for the timer initial value to achieve the specified baud rate is:
Table 1 Timer initial values corresponding to different baud rates
Since the universal software UART uses the same timer in the sending and receiving process, it is a half-duplex software UART. Macro definition and conditional compilation are used in the software to set the format and baud rate of the data frame according to user needs, thereby improving the portability of the software.
3 Implementation of Universal Software UART
Sending
The sending process uses the microcontroller to actively control the I/O port, and sends the data according to the set data frame format. In the sending process, other irrelevant interrupts are turned off to ensure that the sending clock of each sending bit is the same. First, set the timer to the initial value of the timing, start the timer, turn off other irrelevant interrupts, and then start sending the data frame; after sending the start bit space, the data in the shift register is sent out in order from low to high through the loop structure, and the parity bit of the data is calculated at the same time, and sent out after sending the data bit, and finally the stop bit mark is sent; turn off the timer, open the interrupt, and the sending of a frame of data is completed. The sending flow chart is shown in Figure 2.
Receiving
The receiving process is implemented using the external interrupt of the microcontroller. Set the trigger mode of the interrupt source to falling edge trigger. When an interrupt occurs, receive the data in the interrupt handler, and put the data into the buffer set in advance, waiting for the microcontroller to read the data. In the process of data reading, in order to ensure the correctness of data reading, data sampling must be performed in the middle of each data bit. In the main program, set the trigger mode of the external interrupt, and set the interrupt to a high priority to ensure continuous data reception, and then wait for the external interrupt to occur; in the interrupt handler, first set the timer initial value, start the timer, and turn off other irrelevant interrupts; then wait for a half-bit clock so that the sampling point falls in the middle of the first data bit, then read the data into the shift register through the loop structure, and calculate the parity bit at the same time. After receiving, if the parity check is correct, put the data into the data buffer unit, otherwise it will be discarded; finally turn off the timer and restore the interrupt. The receiving flow chart is shown in Figure 3.
The issues that need to be paid attention to in the process of receiving and sending are the interrupt response time calibration, the overflow calibration of the counting timer T/C, and the real-time reading. In order to be able to send and receive data frames in a timely, continuous and accurate manner, the timer initial value needs to be adjusted accordingly in the actual application process, and irrelevant interrupts need to be turned off during the sending and receiving process.
4 Conclusion
The universal software UART implemented in this paper has been applied in many engineering projects, and after many improvements, it has better portability, which speeds up the development of the system. It can be applied to various 51 series microcontrollers, and achieves the purpose of reducing the hardware overhead of the application system and improving the utilization rate of microcontroller resources. It is a system design idea worth promoting.
References
[1] Zhao Xiao. Principles and Applications of MCS-51 Single Chip Microcomputer [M]. Tianjin: Tianjin University Press, 2001. 187-200
[2] He Limin. Selected Articles on Single Chip Microcomputer Application Technology 6 [C]. Beijing: Beijing University of Aeronautics and Astronautics Press, 1999
[3] Li Wei. Software Implementation of Asynchronous Serial Port Based on ST62T32B Single Chip Microcomputer I/O Port [J]. Journal of Zhejiang Engineering Institute, Vol. 20, No. 3: 193-196
[4] Yan Yude, Yu Hong. Principles and Applications of MCS-51 Single Chip Microcomputer (C Language Edition) [M]. Beijing: Machinery Industry Press, 2003
[5] Xu Aijun, Peng Xiuhua. Single Chip Microcomputer High-Level Language C51 Windows Environment Programming and Application [M]. Beijing: Electronic Industry Press, 2003
Previous article:Design of Temperature Control System with MCS-51 Single Chip Microcomputer
Next article:Anti-interference solution for single-chip microcomputer application system
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Tencent employees angrily criticized management for high-intensity overtime work, netizens: role models of our generation
- When using STM32 debugging, a hardware error occurs
- What problems have you encountered recently when using the forum? Feel free to comment!
- Intelligent LED lighting solution based on sensor and MCU
- What is the difference between embedded system development and directly developing projects in MCU using assembly? Does the former have to be developed on OS?
- Gods, what's going on?
- What are the uses and characteristics of common pulse circuits?
- 【mpy】The SD/MMC bus can be configured to 1 or 4 bits
- Introducing several circuit simulation software that can run on mobile phones
- The microcontroller uses a dip switch to set the address