Implementation of Universal Software UART Based on 51 Series MCU

Publisher:快乐之源Latest update time:2012-03-12 Source: 微计算机信息 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

In terms of hardware design, the general software UART only uses a general I/O port, a T/C count timer and an external interrupt INT of the 51 series microcontroller, which minimizes the system hardware overhead. The design idea of ​​the software UART data transmission is to use any I/O port of the microcontroller as the transmitter (TxD) of the general software UART, set the communication baud rate through the T/Cx count timer, and send out the start bit, data bit, parity bit and stop bit of the data frame in sequence; the design idea of ​​data reception is to use an external interrupt of the microcontroller as the data receiving end (RxD), and set the interrupt source to the falling edge trigger mode, so as to detect the space (logic 0) level of the start bit in time, and receive the data in the interrupt handler. This design is conducive to improving the response speed of UART and the utilization rate of the microcontroller.
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:


Among them, is the initial value of the constant, is the modulus value of the counter, and is the number of items to be counted [2]-[3].

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

Reference address:Implementation of Universal Software UART Based on 51 Series MCU

Previous article:Design of Temperature Control System with MCS-51 Single Chip Microcomputer
Next article:Anti-interference solution for single-chip microcomputer application system

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号