Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The basic use of UART Serial Communication

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

From the transmission bias, the commonly used communication can be divided into three categories: simplex communication, half-duplex communication and full-duplex communication.

Simplex communication means that only one party is allowed to transmit information in favor of the other party, while the other party cannot send the message back. For example, TV remote control, radio transmission and so on, are mostly simplex communication skills.

Half-duplex communication means that data can be transmitted from one party to another, but only one party can send it to the other at a time of unification. For example, our walkie-talkie is a typical half-duplex.

Full-duplex communication can also receive data while sending data, and the two stop at the same time, just like our phone calls, and we can hear each other's voices as we speak.

Introduction to UART module

IO port imitates serial communication, which makes everyone understand the essence of serial communication, but our single-chip microcomputer program needs to detect and scan the data received by single-chip microcomputer IO port all the time, which takes up a small amount of running time of single-chip microcomputer. At this time, smart people will think, in fact, we are not very concerned about the process of communication, we only need the consequences of a communication, and finally lose the accepted data. In this way, we can do a hardware module outside the single-chip microcomputer, let it take the initiative to accept the data, after it is accepted, just tell us that there is such a UART module outside our 51 single-chip microcomputer. To use it accurately, of course, we must first set up the corresponding special function memory equipment.

The construction of the UART serial port of 51 single chip microcomputer is composed of three parts: the serial port master memory SCON, the sending and receiving circuit. Let's first understand the serial port master memory SCON. As shown in Table 11-1, Table 11-2.

Table 11-1 SCON-- serial master memory location assignment (address 0x98, bit addressable)

Bit 76543210 symbol SM0SM1SM2RENTB8RB8TIRI reset value 00000000

Table 11-2 bit description of SCON-- serial master memory

The bit symbol depicts that the two 7SM0 cooperate to determine the form 0 ~ form 3 of serial communication, a total of four forms. What we use most often

That is, form 1, that is, SM0=0,SM1=1, let's focus on form 1, other modules.

A brief introduction to the style. 6SM15SM2 multi-computer communication master bit (rarely used), form 1 direct zero. 4REN enables serial admission. Software setting enables acceptance, while software zeroing prevents acceptance. The 9th bit of data to be sent in 3TB8 forms 2 and 3 (rarely available). The 9th bit data (rarely used) accepted in 2RB8 forms 2 and 3, and form 1 is used to accept abort bits. The marked bit is marked in the 1TI transmission, and when the transmitting circuit is sent to the position of both ends of the stop bit, the TI is set by the hardware.

Must be cleared by software. The marked bit is marked in the 0RI acceptance. When the receiving circuit accepts both ends of the dead bit, the RI is set by the hardware to 1.

Must be cleared by software.

I have learned so much about the setting and equipment of the memory before. I believe that the SCON center has not been difficult for most of the students. I should be able to understand and set up the equipment myself. With regard to the four forms of serial port, form 1 is the most commonly used, which is the 1-bit end bit, 8-bit data bit and 1-bit stop bit mentioned earlier. Below we will introduce the task details and application methods of form 1, as for the other three forms are more or less the same, when you really need to use it, everyone will consult the relevant materials again.

When we use IO port to imitate serial communication, the baud rate of serial port is shown by using the suffix of quasi-timer T0. In the hardware serial port module, there is a special baud rate transmitter to control the speed of sending and receiving data. As far as STC89C52 single chip microcomputer is concerned, this baud rate generator can only be generated by quasi-timer T1 or quasi-timer T2, but not by quasi-timer T0, which is a completely different concept from the communication we imitate.

If you use timer 2, which is required to set up a rated storage device, and acquiesce in using timer 1, we mainly use timer T1 as a baud rate transmitter in this chapter. The baud rate generator under method 1 must use the form 2 of the timer T1, that is, the active reloading form, and the calculation formula of the reload value of the timer is as follows:

TH1 = TL1 = 256-Crystal oscillator value / 12 / 2 ram 16 / baud rate

There is also a memory related to the baud rate, which is a power governance memory PCON, whose highest bit can double the baud rate, that is, if you write PCON | = 0x80, the calculation formula will become:

TH1 = TL1 = 256-Crystal oscillator value / 12 / 16 / baud rate

The meaning of the number in the formula is explained here. 256is the overflow value of the 8-bit timer, that is, the overflow value of TL1. The crystal vibration value on our opening board is 110592001.12 means that one mechanical period is equal to 12 clock cycles. What is worth paying attention to is this 16, which we will focus on. When IO imitates serial communication to accept data, it collects both ends of the data, but in practice, the serial module is more complex and accurate than what we imitate. His method is to collect a signal 16 times and take out the 7th, 8th and 9th of them. If two of these three times are high, then the data is determined to be 1, and if the two times are low, then this bit is determined to be 0, so that once you are disturbed and misread the data, you can still guarantee the accuracy of the final data.

Understand the serial port collection form, here to leave a question for everyone to consider. "Crystal oscillator value / 12thumb / 16 / baud rate", the time of the central calculation, it can not be divided, perhaps how to do the decimal, how much bias does the license show? If we understand this part, we will understand why our crystal oscillator uses 11.0592m.

In the sending and receiving circuit of serial communication, there are two SBUF memories with opposite names, and most of their addresses are 0x99, but one is used as send buffer and the other is used as reception buffer. It means that there are two rooms, and the door logos of the two rooms are the same, one of which only leaves and does not enter, and the other only enters and does not leave. In this way, we can complete the full-duplex communication of UART without disturbing each other. However, logically, we only operate SBUF each time, and the timing of a single chip takes the initiative to choose whether to accept SBUF or send SBUF based on the "read" or "write" operation performed on it. After the procedure, we will fully understand this result.

UART serial port program

In general, the basic steps of writing serial communication programs are as follows:

Set the serial port of equipment equipment to form 1.

Set the equipment setting timer T1 to form 2, that is, the active reinstallation form.

The initial values of TH1 and TL1 are calculated according to the baud rate, and the baud rate can be doubled using PCON if there is a need.

Turn on the timer to master the memory TR1 and let the timer run.

Special attention should also be paid to the fact that when using T1 as a baud rate transmitter, never again enable the infix of T1.

Let's first take a look at the program code when the IO port imitates serial communication and directly changes to the hardware UART module to see if the program is much more complicated, because most of the task hardware modules have been done for us. The function of the program is the same as that imitated by the IO port.

# include void ConfigUART (unsigned int baud); void main () {ConfigUART (9600); / / set the baud rate of equipment equipment to 9600 while (1) {while (! RI); / / wait for acceptance to complete RI = 0; / / clear zero acceptance marked bit SBUF = SBUF + 1; / / send return while (! TI) after received data + 1; / / wait for transmission to be completed TI = 0 / / clear zero transmission marked bit}} / * Serial port setting equipment configuration function, baud- communication baud rate * / void ConfigUART (unsigned int baud) {SCON = 0x50; / / set equipment installation serial port in the form 1 TMOD & = 0x0F; / / clear zero T1 control bit TMOD | = 0x20; / / set equipment configuration T1 in the form 2 TH1 = 256-(11059200needle 1232) / baud; / calculate T1 reload value TL1 = TH1 / / initial value is equal to overload value ET1 = 0; / / suppress T1 infix TR1 = 1; / / start T1}

Of course, this program is still written by waiting for the tagged bits in the main wheel to be accepted and the tagged bits in the transmission, but in the development of practical engineering, of course, we cannot do so. We only want to use a direct comparison to inform the students that the hardware module can greatly simplify the program code, then the practical use of serial port time will be used in the serial port affix, let's take a look at the program completed with infix. Please note that because the receive and send trigger is a unified serial infix, it is necessary to identify which infix in the serial infix function, and then deal with the response.

# include void ConfigUART (unsigned int baud); void main () {EA = 1; / / enable total suffix ConfigUART (9600); / / set equipment configuration baud rate to 9600 while (1);} / * Serial port setting equipment configuration function, baud- communication baud rate * / void ConfigUART (unsigned int baud) {SCON = 0x50; / / set equipment configuration serial port to form 1 TMOD & = 0x0F; / / TMOD of clearing zero T1 TMOD | = 0x20 / / set the equipment configuration T1 in the form 2 TH1 = 256-(11059200Universe 12 TH1 32) / baud; / / calculate T1 overload value TL1 = TH1; / / initial value equals overload value ET1 = 0; / / suppress T1 infix ES = 1; / / enable serial port suffix TR1 = 1; / / start T1} / * UART prefix service function * / void InterruptUART () interrupt 4 {if (RI) {/ / accept to byte RI = 0 / / manually clear the suffix mark bit SBUF = SBUF + 1; / / the accepted data is returned after + 1, the right side is the sending SBUF, and the left side is the accept SBUF} if (TI) {/ / bytes end of transmission TI = 0; / / Manual zero send the suffix mark bit}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report