2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
The blog posts of other bloggers on CSDN are very good. If you want to know more about the UART serial port of 51 microcontroller, you can click in and have a look:
Asynchronous communicationIt means that the time interval between two characters (8 bits) in communication is not fixed, but the time interval between each bit in a character isstable。
UART serial communication is the most commonly used communication technology for microcontrollers, and is usually used for communication between microcontrollers and computers and between microcontrollers.
Communication can be divided into basic typesParallel communicationandSerial communication,In parallel communication, each bit of data is transmitted at the same time, which can achieve communication in bytes, but the communication lines occupy more resources and are costly. For example, P0 = 0xFF used earlier; the 8 IO ports of P0 are assigned values at a time, and the signals are output at the same time, which is similar to having 8 lanes that can pass 8 cars at the same time. This form is parallel, and P0, P1, P2 and P3 are usually called the 4 parallel buses of the 51 microcontroller.
Serial communication is like a lane that can only pass one car at a time. If a byte of data such as 0xFF is to be transmitted, assuming that one car is carrying 1 bit of data, then it will take 8 cars to pass.Same time intervalCross the same lane one by one.
The difference between synchronous communication and asynchronous communication:
1. Synchronous communication requires that the clock frequency of the receiving end is consistent with the clock frequency of the sending end, and the sending end sends a continuous bit stream; asynchronous communication does not require the clock of the receiving end to be synchronized with the clock of the sending end. After the sending end sends a byte, it can send the next byte after any time interval.
2. Synchronous communication is efficient, while asynchronous communication is less efficient.
3. Synchronous communication is more complicated, and the allowable error between the clocks of both parties is smaller; asynchronous communication is simple, and a certain error can be allowed between the clocks of both parties.
4. Synchronous communication is only applicable to point-to-multipoint, while asynchronous communication can be used for point-to-point.
The STC89C52 microcontroller has two pins that are specifically used for UART serial communication, one is P3^0 and the other is P3^1. They are also called RXD (Receive Data) and TXD (Transmit Data). The communication interface composed of them is called a serial interface. As shown in the figure:
Communication between MCU and MCU:
In the figure, GND indicates the reference ground of the microcontroller system power supply. TXD is called the serial transmit pin, and RXD is the serial receive pin.
To communicate between two MCUs, first the power supply reference must be the same, so the GND of the two MCUs must be connected to each other. Then the TXD pin of MCU 1 is connected to the RXD pin of MCU 2. The function is that MCU 1 sends information to MCU 2. (This process is divided into two parts. MCU 1Sending a signalProcess and MCU 2receive signalProcess) Similarly, the TXD pin of MCU 2 is connected to the RXD pin of MCU 1.
This schematic diagram shows the process of two microcontrollers sending and receiving information to each other.
When MCU 1 wants to send data to MCU 2, for example, to send 0xE4, the binary form is
0b1110 0100, before the transmission starts, MCU 1 faces several problems:
Assuming that I transmit the lowest bit 0 first, then I need to set the TXD port of microcontroller 1 to a low level.
The microcontroller is generally TTL or CMOS level. For the TTL/CMOS level standard:
A voltage greater than 2.0V is a high level, and a voltage less than 0.0V is a low level. The voltage in between is neither high nor low. The logic in between is confusing. It may be judged as a high level or a low level by the device. Therefore, try not to operate the circuit in this area. Operating in this area will make your product work sometimes well and sometimes badly. Therefore, when developing a product, you should check the device data sheet and the voltage during the circuit operation process to make it at a suitable voltage.
1. TTL level:
Output high level>2.4V, output low level<0.4V. At room temperature, the general output high level is 3.5V, and the output low level is 0.2V. Minimum input high level and low level: input high level>=2.0V, input low level<=0.8V, noise margin is 0.4V.
2. CMOS level:
1Logic LevelThe voltage is close to the power supply voltage, and the 0 logic level is close to 0 V. And it has a wide noise tolerance.
So here comes the question:
When not transmitting or receiving information, the transmission port (TXD) of MCU 1 will be at a high level or a low level or the voltage of the port is in between. The receiving port voltage (RXD) of MCU 2 follows the voltage of the transmission port of MCU 1. How to set the signal that MCU 1 is about to start transmitting information, that is, how does MCU 2 know that MCU 1 is transmitting information to it? Therefore, it is necessary to standardize the communication standards.
Step 1: When no communication occurs, the voltage of the MCU TXD and RXD ports remain at a high level.
Step 2: Standardize the data transmission method as shown below
You can see that it defines 1 start bit, 1 stop bit, plus 1 byte of data to be transmitted, which is 8 bits in total.Ten-digit data。
The above transmission of 0xE4 data is actually the transmission of 0 1110 0100 1. When MCU 1 transmits 1 byte of data to MCU 2, it actually transmits 10 bits of data. The whole of 10 bits is called a complete serial data frame.
With the start bit and stop bit, the RXD port of MCU 2 starts to prepare to receive data when it detects a low level. Similarly, if MCU 1 wants to send 1 byte of data, it needs to send a start bit 0 first (to tell MCU 2 to prepare to receive), and then send a stop bit 1 after the data is sent (to tell MCU 2 that a byte of data transmission is completed). And when transmitting data,First low then highOrder
Step 3: You need to uniformly set the time required to transmit 1 bit of data. The speed of this time is expressed by baud rate (baud). 1 baud rate means 1 bit of data is transmitted in 1 second, and 9600 baud rate means 9600 bits of data are transmitted in 1 second.
Then we can get the transmission of 1 bitDuration = 1/baud. Then the time to transmit a complete data frame can be calculated as10/baud,The time interval between data frames is arbitrary.
When MCU 1 and MCU 2 transmit signals, their baud rates must be set to be consistent in order to achieve correct communication.
In the early years of desktop computers, there was generally a 9-pin serial port, this serial port is called RS-232 port, it is related to UART communication, but nowadays laptops do not have this 9-pin serial port, so the communication between microcontrollers tends to use USB virtual serial port more and more.
The level logic used by RS232 is negative logic, which is different from TTL/CMOS logic level
Therefore, the 9-pin RS232 serial port of the computer cannot be directly connected to the microcontroller. A level conversion chip MAX232 is required as shown below:
RS232 pin description:
1. Carrier detection DCD (Data Carrier detection) 2. Receive data RXD 3. Send data TXD
4. Data terminal is ready for DTR (Data Terminal Ready) 5. Signal ground 6. Data ready DSR (Data Set ready)
7. Request to send RTS (Request to Send) 8. Clear to send CTS (Clear to Send) 9. Ringing prompt RI (Ringing)
As shown in the figure above, in the serial port wiring, RS232 can complete communication as long as pins 2, 3 and 5 are connected to the corresponding pins of MAX232.
With the development of technology, RS-232 serial communication is still widely used in industry, but in the application of commercial technology, USB to UART technology has gradually replaced RS232 serial port.
So how to realize the communication between the microcontroller and the computer as shown in the figure
This is the development board in this case that uses the USB to serial port chip CH340T to achieve this function.
The MCU port TXD is connected to USB-TX with a jumper cap and is connected to the RXD port on the 4th pin of CH340.
The MCU port RXD is connected to USB-RX using a jumper cap and is connected to the third pin TXD of CH340.
You can see that there is a 4148 diode in series between USB-RX and the third pin TXD of CH340, because the STC89C52 microcontroller needs a cold start when downloading the program. That is, click download first and then power on.
So-calledCold startIt refers to the process of starting the microcontroller from power off to power on; while hot start means that the microcontroller is always powered on. The difference between cold start and hot start is that the values in the internal RAM of the microcontroller are some random values during cold start, while the values in the internal RAM of the microcontroller will not be changed during hot start and remain the same as before startup.
At the moment of power-on, the MCU will first detect whether it needs to download the program. Although the VCC of the MCU is controlled by the switch, since the 3rd pin of CH340T is an output pin, if there is no diode, when the MCU behind the switch is powered off, the 3rd pin of CH340T and the P3.0 (RXD) pin of the MCU are connected together, and current will flow through this pin into the subsequent circuit and charge the capacitor of the subsequent circuit, causing a certain voltage in the subsequent circuit. Although this voltage value is only about 2~3V, it may affect the normal cold start. After adding the diode, on the one hand, it does not affect the communication, and on the other hand, it can eliminate this adverse effect.
From the microcontroller manual, we know that P3.0 is a quasi-bidirectional IO port.
The red frame part is the quasi-bidirectional IO portWhen the internal output is low, the transistor base is high after passing through the NOT gate, and the transistor is turned on (the voltages at both ends of CE are almost the same when saturated), and the IO port of the microcontroller outputs a low level. No matter whether the external key switch is pressed or released, the IO port always maintains a low level. That is, it is not controlled by external signals.
When the internal output is high, after the NOT gate, the base of the transistor is low, and the transistor is not turned on (the resistance at both ends of CE is very large, and the resistor R can be ignored compared to it). At this time, the IO port outputs a high level. When the button is pressed, the IO port is grounded, and the IO port outputs a low level.
It can be seen from this that only whenIO port output is high levelwhen,The output of the IO port is controlled by the external circuit。
Therefore, we know that when P3.0 is used as the RXD port, the internal output is always high.Internal output levelCan be changed up or down as needed.
Moreover: for TTL level
Output terminal: high level>=2.4V, low level<=0.4V;
Receiving end: high level>=2.0V, low level<=0.8V.
For COMS level
Output terminal: high level = Vcc, low level = GND (Vcc is the power supply voltage);
Receiving end: high level>=0.7Vcc, low level<=0.2Vcc.
There is still some voltage difference between the logic level at the receiving end and the logic level at the output end. This may be due to the voltage drop interference generated during signal transmission or the existence of a protection circuit. Therefore, adding a diode will not cause the voltage after transmission to be in a chaotic logic area.
Use IO port to simulate UART serial communication
On the program:
- # include<reg52.h>
-
- sbit PIN_RXD = P3^0; //接受引脚定义
- sbit PIN_TXD = P3^1; //发送引脚定义
-
- bit RxdorTxd = 0; //指示当前状态为接受还是发送
- bit RxdEnd = 0; //接受结束标志
- bit TxdEnd = 0; //发送结束标志
- unsigned char RxdBuf = 0; //接收缓冲区
- unsigned char TxdBuf = 0; //发送缓冲器
-
- void ConfigUART(unsigned int baud);
- void StartTXD(unsigned char dat);
- void StartRXD();
-
-
- void main()
- {
- EA = 1; //打开总中断
- ConfigUART(9600); //配置波特率为9600
-
- while(1)
- {
- while(PIN_RXD); //等待接收引脚出现低电平,即起始位
- StartRXD(); //启动接收
- while(!RxdEnd); //等待接受完成
- StartTXD(RxdBuf +1);//接受到的整数+1后,发送回去
- while(!TxdEnd); //等待发送结束
- }
- }
-
- /* 串口配置函数,baud-通信波特率 */
- void ConfigUART(unsigned int baud)
- {
- TMOD &= 0xF0; //清零T0的控制位
- TMOD |= 0x02; //配置T0为模式2
- TH0 = 256 - (11059200/12)/baud; //计数T0的重载值
- }
-
- /* 启动串行接受 */
- void StartRXD()
- {
- TL0 = 256 - ((256-TH0) >> 1);//接受启动时T0定时为半个波特率周期
- ET0 = 1; //使能T0中断
- TR0 = 1; //启动T0
- RxdEnd = 0; //清零接受结束标志
- RxdorTxd = 0; //设置当前状态为接受 1位发送
-
- }
-
- /* 启动串行发送,dat-待发送字节数据 */
- void StartTXD(unsigned char dat)
- {
- TxdBuf = dat; //待发送数据保存到发送缓冲器
- TL0 = TH0; //T0计算初值为重载值
- ET0 = 1; //使能T0中断
- TR0 = 1; //启动T0
- PIN_TXD = 0; //发送起始位
- TxdEnd = 0; //清零发送结束标志
- RxdorTxd = 1; //设置当前状态为发送
- }
-
- /*T0中断服务函数,处理串行发送和接收 */
- void interruptTimer0() interrupt 1
- {
- static unsigned char cnt = 0;
-
- if(RxdorTxd)
- {
- cnt++;
- if(cnt <= 8) //低位在先一次发送8bit数据位
- {
- PIN_TXD = TxdBuf & 0x01;
- TxdBuf >>= 1;
- }
- else if(cnt == 9) //发送停止位
- {
- PIN_TXD = 1;
- }
- else //发送结束
- {
- cnt = 0; //复位bit计数器
- TR0 = 0; //关闭T0
- TxdEnd = 1; //置发送结束标志
- }
- }
-
- else //串行接收处理
- {
- if(cnt == 0) // 处理起始位
- {
- if(!PIN_RXD) //起始位为0时,清零接收缓冲器,准备接受数据位
- {
- RxdBuf = 0;
- cnt++;
- }
- else //起始位为1(不为0)时,中止接收
- {
- TR0 = 0; //关闭T0
- }
- }
- else if(cnt <= 8) //处理8位数据位
- {
- RxdBuf >>= 1; //低位在先,所以将之前接收的位向右移
- if(PIN_RXD) //接收脚为1时,缓冲器最高位置1
- { //而为0时不处理即仍保持位移后的0
- RxdBuf |= 0x80;
- }
- cnt++;
- }
- else //停止位处理
- {
- cnt = 0; //复位bit计数器
- TR0 = 0; //关闭T0
- if(PIN_RXD) //停止位为1时,方认为数据有效
- {
- RxdEnd = 1; //置接收结束标志
- }
- }
- }
-
- }
Work Logic Map
This program uses timer 0 to simulate UART serial communication.Simulation program.This program realizes the communication between the computer and the single-chip microcomputer. Its communication result is that the data transmitted by the computer is transmitted to the single-chip microcomputer, and the single-chip microcomputer adds 1 to the data and sends it back to the computer. Use the serial port debugging assistant that comes with STC-ISP to demonstrate this result.
First, make sure the ports are the same. The above pictures are all COM3, the baud rate is 9600, the check bit is NO, the data bit is 8, and the stop bit is 1. (Here are the computer's serial port parameters)
Look at the result: normal communication is possibleTimer 0 simulates serial communication
Briefly explain the working logic of the program:Sending moduleFor example, send 0xAA =1010 1010 and start from the lowest bit.
That is, TxdBuf = 1010 1010. See the program below.
- if(RxdorTxd)
- {
- cnt++;
- if(cnt <= 8) //低位在先一次发送8bit数据位
- {
- PIN_TXD = TxdBuf & 0x01;
- TxdBuf >>= 1;
- }
- else if(cnt == 9) //发送停止位
- {
- PIN_TXD = 1;
- }
- else //发送结束
- {
- cnt = 0; //复位bit计数器
- TR0 = 0; //关闭T0
- TxdEnd = 1; //置发送结束标志
- }
- }
firstEnter interrupt cnt = 1
PIN_TXD = TxdBuf & 0x01, that is, the result of the AND operation of 1010 1010 0000 0001 is to assign the lowest bit of 0xAA to the transmit port to enable the corresponding level. It can be seen that the voltage of the TXD port is low when it enters the interrupt for the first time, and this process will continue until the second interrupt.
Then TxdBuff is shifted right by one bit, that is, TxdBuf = 0101 0101
Thenthe second timeInterrupt
cnt = 2
PIN_TXD = 1 This time the level of the sending port is high
TxdBuf = 0010 1010
Then the third time, the fourth time, and so on.8th
cnt = 8
PIN_TXD = 1 sets the level to a high level, which is the highest bit 1 of the transmitted data
At this time, TxdBuf = 0x00
After that9thEntering the interrupt, the 9th interrupt means that the data bit has just been sent and it is time to prepare to send the stop bit
Therefore, PIN_TXD = 1, the transmit port is directly assigned a high level
The last tenth interrupt means that the stop bit has been sent. Therefore, cnt is reset to 0, TR0 is set to 0 to turn off timer 0, TXDEnd = 1, and the send end flag is set to 1. At this point, a complete data frame is sent.
After thatReceiver Module:In the previous text, we sent 0xAA. For this function, its value comes from the result of adding 1 to the received data, so the received data is: 0xA9 =1010 1001
- void StartRXD()
- {
- TL0 = 256 - ((256-TH0) >> 1);//接受启动时T0定时为半个波特率周期
- ET0 = 1; //使能T0中断
- TR0 = 1; //启动T0
- RxdEnd = 0; //清零接受结束标志
- RxdorTxd = 0; //设置当前状态为0接收 1为发送
-
- }
- else //串行接收处理
- {
- if(cnt == 0) // 处理起始位
- {
- if(!PIN_RXD) //起始位为0时,清零接收缓冲器,准备接受数据位
- {
- RxdBuf = 0;
- cnt++;
- }
- else //起始位为1(不为0)时,中止接收
- {
- TR0 = 0; //关闭T0
- }
- }
- else if(cnt <= 8) //处理8位数据位
- {
- RxdBuf >>= 1; //低位在先,所以将之前接收的位向右移
- if(PIN_RXD) //接收脚为1时,缓冲器最高位置1
- { //而为0时不处理即仍保持位移后的0
- RxdBuf |= 0x80;
- }
- cnt++;
- }
- else //停止位处理
- {
- cnt = 0; //复位bit计数器
- TR0 = 0; //关闭T0
- if(PIN_RXD) //停止位为1时,方认为数据有效
- {
- RxdEnd = 1; //置接收结束标志
- }
- }
- }
It can be seen that the sending and receiving processes are actually similar. For receiving data, I marked the first interrupt as starting from the 0th interrupt mainly to facilitate understanding with cnt.
As shown in the figure, the time it takes to enter the interrupt for the 0th time is half of the preset interrupt time. This is because when confirming whether a level signal is 0 or 1, if the level at the beginning of the sampling time is likely to produce errors or interference, the sampling point is generally set at the midpoint of the signal transmission time, and the level signal at this point is considered to be the signal transmitted during this time period. Therefore, for the receiving module, it only needs to set its interrupt time to half of the original when receiving the start bit for the first time, so that all subsequent signal collection points can be guaranteed to be at the center point.
See how the program is set: TL0 = 256 - ((256-TH0) >> 1); //When receiving the start, T0 timing is half the baud rate period
256-TH0 is the preset interrupt time. In the previous article, we mentioned that we hope the interrupt time will be half of the previous one. The program operation is to shift right by 1 bit. For example, the binary number of 8 is 0000 1000. The right shift of 1 bit is 0000 0100 = 4. It can be seen that the right shift of 1 bit is to change the number to half of the original one. Similarly, the result of left shift of 1 bit is 0001 0000 = 16, which is to multiply the original number by 2.
Start program process analysis
When RxdorTxd is 0, enter the else function, that is,No. 0Entering the interrupt, the starting point of the receiving module in the time domain is the midpoint of the start bit. First, determine whether the port voltage is really low level, if not, turn off the interrupt. Why do we do this?
Because this is a receiving module, the voltage of RXD is not controlled by the microcontroller. In this case, it is controlled by the computer to control the voltage of the TXD terminal of CH340T. The voltage of the microcontroller RXD follows the TXD terminal of CH340. For the receiving end, the received information may be interfered and needs to be confirmed again.
If yes, cnt++, then cnt = 1. RxdBuf = 0; receive buffer cleared
After thatNo. 1(Actually, it’s the second time)Second-rateEnter the interrupt cnt = 1 and enter the else if() function to transfer the data to the buffer step by step. The subsequent process will not be repeated. If you are interested, you can follow the program logic to see if you can really store the data in the buffer variable in the way of low bits first and high bits later. Because in the previous blog post, the function uses the left shift method to store the high bits of the data first. This method is quite complementary to another working method.
So far the program has used timer 0 to simulate UART serial port communication.
Three Basic Types of Communication
Simplex communication: only one party is allowed to send information to the other party, and the other party cannot send back information, such as TV remote control, radio broadcast, etc.
Half-duplex communication: data can be transmitted between two parties, but only one party can send data to the other party at the same time. For example, a walkie-talkie is a typical half-duplex communication. The serial port simulation program above can also be understood as half-duplex communication.
Full-duplex communication: data can be sent and received at the same time, and the two are carried out simultaneously, such as our telephone communication.
IO port simulated serial communication basically shows the essence of serial communication, but the MCU program needs to constantly detect and scan the data received by the MCU IO port, which takes up a lot of MCU running time. Therefore, there is a UART module inside the 51 MCU, which can automatically receive data and notify after receiving it. To use it correctly, you need to correctly configure the special function registers.
SCON Serial Control Register
This case only introduces mode 1, that is, setting SM1 = 1 and SM0 = 0 is mode 1. This mode is the data frame format used in the previous simulated serial communication: 1 start bit, 8 data bits, and 1 stop bit.
Analog serial communication uses timer 0 to represent the baud rate. The baud rate transmitter of the STC89C52 UART module can only be generated by timer T1 or timer T2, but not by timer T0. This is a completely different concept from analog communication.
When using timer T2, additional configuration registers are required. By default, timer T1 is used.
The program code:
- # include<reg52.h>
-
- void ConfigUART(unsigned int baud);
-
- void main()
- {
- EA = 1;
- ConfigUART(9600); //配置波特率为9600
- while(1);
-
- }
- /* 串口配置函数,baud-通信波特率 */
- void ConfigUART(unsigned int baud)
- {
- SCON = 0x50; //0x50= 0101 0000 配置串口为模式1
- TMOD &= 0x0F; //清零T1的控制位
- TMOD |= 0x20; //0x20 = 0010 0000 配置T1的为模式2自动重载模式
- TH1 = 256 - (11059200/12/32)/baud; //计算T1的重载值
- TL1 = TH1; //设置初值
- ET1 = 0; //禁止T1中断
- ES = 1; //启动串口中断
- TR1 = 1; //启动T1定时器
- }
-
- /*UART中断服务函数 */
- void interruptUART() interrupt 4
- {
- if(RI) //接收到字节
- {
- RI = 0; //软件清0接收中断标志
- SBUF = SBUF+1;//接收的数据+1,左边是发送SBUF,右边是接收SBUF
-
- }
- if(TI)
- {
- TI = 0; //软件清0发送中断标志位
- }
-
- }
Take a look at the resulting video:Built-in module serial communication_bilibili_bilibili
You can see that there is no problem with normal communication, and then briefly explain the relevant content in the program:
The calculation formula for the reload value of timer T1 is:
TH1 = TL1 = 256 - Crystal value/12/2/16/Baud rate then its
Interrupt interval time = crystal value/12/2/16/baud rate = crystal value/12*(1/baud rate)*(1/32)
The crystal value/12 means the number of machine cycles in 1 second, and (1/baud rate) means the time required to transmit 1 bit of data.
Crystal value/12*(1/baud rate) means how many machine cycles are needed to transmit 1 bit of data.
Similarly, crystal value/12*(1/baud rate)*(1/32) the interrupt time = 1/32 the time to transmit 1 bit of data
That is, the transmission of 1 bit of data is divided into 32 time intervals.
The result of the calculation is 3, that is, the interrupt interval time = 3 machine cycles, which is a very short time.
Because the signal sampling method of the serial port module is to collect one signal 16 times, and take out the signal levels of the 7th, 8th and 9th times. If two of these three times are high level, the data is considered to be 1. If the level is 0 twice, the bit is considered to be 0. In this way, once the data is read incorrectly once due to unexpected interference, the correctness of the final data can still be guaranteed.
The following picture is from the recommended blog post. It is mainly for the convenience of explanation.:Baud rate calculation formula
SMOD is controlled by the power register PCON. By default, SMOD is 0. Substituting it into the above formula gives the initial value solution formula.
TH1 = TL1 = 256 - Crystal value/12/2/16/Baud rate
When the register is set to 1 PCON |= 0x80; that is, SMOD is set to 1, the baud rate can be increased by 1 times, as shown in the formula above.
At this time, the initial value of T1 becomes: TH1 = TL1 = 256 - crystal value/12/16/baud rate
One thing that must be noted here is: If you want to use the PCON register to double the wave rate, its initial value formula should still be written as
TH1 = TL1 = 256 - Crystal value/12/2/16/Baud rate
The current reload value is TH1=256 - (11059200/12/32)/baud =256-(11059200/12/16)/(2*buad)baud*2=4800*2=9600, 9600 is the baud rate of the current program
If you write the initial value reload company as TH1 = TL1 = 256 - crystal value/12/16/baud rate, then its current baud rate is still 4800 instead of 9600. You use the serial communication software to set the communication baud rate to 9600.Result is wrong。
Watch the video:Baud rate related_Bilibili_bilibili
We can see that the baud rate of 9600 has a time interval of only 3 machine cycles, and the time interval of 14400 is 2 machine cycles. It can be seen that if the baud rate is higher, its time interval may be less than 1 machine cycle. Therefore, there is another working mode, that is, after the highest position of PCON is set to 1, ConfigUART(9600); its current baud rate is 19200, but its time interval is the time interval of the original 9600 baud rate, that is, 3 machine cycles.
It must be reminded that there are still many differences between the work of the timer 0 analog serial communication and the serial communication module. The serial communication module completes one input and output, and it only enters the serial communication interrupt twice. Once RI is set to 1 in response, and once TI is set to 1 in response, and then the software clears it. It no longer pays attention to the transmission process but only pays attention to whether the transmission is completed. A signal is sent when the transmission is completed.
The sending and receiving circuits of serial communication have two identical SBUF registers physically, and their addresses are both 0x99, but one is used for sending buffer and the other is used for receiving buffer. This means that there are two rooms with the same house number, one of which is allowed to enter but not leave, and the other is allowed to leave but not enter. In this way, full-duplex communication of UART can be achieved without interference between them. However, logically, only SBUF is operated each time, and the microcontroller automatically chooses whether to receive SBUF or send SBUF according to whether it is a "read" or "write" operation.
The basic steps of the serial communication program:
1. Configure the serial port to mode 1.
2. Configure timer T1 to mode 2, which is the automatic reload mode.
3. Calculate the initial values of TH1 and TL1 according to the baud rate. If necessary, use PCON to double the baud rate.
4. Open the timer control register TR1 to start the timer running.Note: When using the serial port interrupt, the Timer 1 interrupt cannot be enabled, unless you are using the Timer 2 serial port interrupt.