#51 MCU# Basic application of UART serial communication, module introduction and serial program

Publisher:科技奇才Latest update time:2021-10-20 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Basic Applications of UART Serial Communication

Three basic types of communication:


Simplex communication: allows one party to send information to the other party, but the other party cannot send information back. Example: TV remote control, radio broadcast


Half-duplex communication: Data can be transmitted between two parties, but only one party can transmit to the other party at the same time. Example: Walkie-talkie


Full-duplex communication: data can be sent and received simultaneously. Example: telephone


UART Module Introduction

Usually, we care about the result of communication rather than the process. There is a UART module inside the 51 microcontroller, which can automatically receive data and send a notification signal after receiving. To use this module, you need to configure the corresponding register with special functions.


The UART serial port structure of the 51 single-chip microcomputer consists of three parts: the serial port control register SCON, the sending and receiving circuits.


First, let's understand the serial port control register SCON


SCON - bit allocation of serial control register (address 0X98, bit addressable)

image.png

SCON - Serial Control Register Bit Description

image.png

Of the four modes of the serial port, mode 1 is the most commonly used, which is the 1 start bit, 8 data bits and 1 stop bit mentioned above. The following is a detailed introduction to the working details and usage of mode 1 (the other 3 modes are similar to mode 1, and you can refer to the information when you need to use them).


In the hardware module, there is a dedicated baud rate generator to control the speed of sending and receiving data. For the STC89C52 microcontroller, this baud rate can only be generated by timer T1 or timer T2. This is completely different from the simulated communication concept. Timer T1 is used by default (timer T2 requires additional configuration registers).


The following mainly explains the use of T1 as a baud rate generator. The baud rate generator in mode 1 must use mode 2 of T1, which is the automatic reload mode.


The timer reload value calculation formula is: TH1 = TL1 = 256-crystal value/12/2/16/baud rate


Here is another calculator related to baud rate: the power management register PCON, its highest bit can double the baud rate, that is, if you write PCON |= 0X80, the calculation formula becomes: TH1 = TL1 = 256-crystal value/12/16/baud rate


The meanings of the numbers in the formula are explained as follows:

256——8-bit timer overflow value, 1TL1 overflow value.

Crystal value - 11059200 on the development board

12——1 machine cycle equals 12 clock cycles

16——collect one bit of signal 16 times (when the IO port simulates serial communication to receive data, the middle position of the data is collected, and the actual serial port module is more complex and accurate than the simulation. It collects one bit of signal 16 times, and takes out 7, 8, and 9 times. If two of these three times are high level, the data is considered to be 1, and if two are low level, the data is considered to be 0. In this way, even if the data is read incorrectly due to interference, the correctness of the final data can still be guaranteed)


After understanding the serial port acquisition mode, we can think about a question. Is it possible that the value calculated by "crystal value/12/2/16/baud rate" is a decimal? Understanding this also explains why the crystal oscillator uses 11059200. Because 11059200 can be divided by the common baud rate.


The sending and receiving circuits of the serial communication have two SBUF registers with the same name physically, and their addresses are the same, both 0X99. One of these two registers is used for sending buffer and the other for receiving buffer, which realizes the full-duplex communication of UART without interfering with each other. When operating SBUF, the microcontroller automatically determines and decides whether to select receiving SBUF or sending SBUF.


UART serial port program

In general, the steps for writing a serial communication program are as follows:


1) Configure the serial port to mode 1


2) Configure timer T1 to mode 2 (auto-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 T1, that is, when using T1 as a baud rate generator, do not enable the T1 interrupt.

The following is a demonstration code


 #include


 void ConfigUART(unsigned int baud); //Serial port configuration function, baud——communication baud rate


 void main()

 {

  ConfigUART(9600); //Configure the baud rate to 9600


while(1)

{

while(!RI); //Wait for receiving to complete

RI = 0; // Clear the receive interrupt flag

SBUF = SBUF + 1; //Received data + 1, sent back

while(!TI); //Wait for sending to complete

TI = 0; // Clear the transmit interrupt flag

}

 }



 void ConfigUART(unsigned int baud)

 {

  SCON = 0X50; //Configure the serial port to mode 1

TMOD &= 0X0F; // Clear the control bit of T1

TMOD |= 0X20; //Configure T1 to mode 2

TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value

TL1 = TH1; //initial value equals reload value

ES = 0; //Disable T1 interrupt

TR1 = 1; //Start T1

 }


Note: In actual project development, this code is not written like this, because this code waits for the receive interrupt flag and the send interrupt flag in the main loop, which occupies a large amount of program memory. Serial port interrupts are often used when actually using the serial port.


The following is the demonstration code for serial port interrupt


 #include


 void ConfigUART(unsigned int baud); //Serial port configuration function, baud——communication baud rate


 void main()

 {

  EA = 1; // Enable general interrupt

  ConfigUART(9600); //Configure the baud rate to 9600

while(1);

}



 void ConfigUART(unsigned int baud)

 {

  SCON = 0X50; //Configure the serial port to mode 1

TMOD &= 0X0F; // Clear the control bit of T1

TMOD |= 0X20; //Configure T1 to mode 2

TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value

TL1 = TH1; //initial value equals reload value

ET1 = 1; //Disable T1 interrupt

ES = 1; // Enable serial port interrupt

TR1 = 1; //Start T1

 }


 //UART interrupt service function

 void InterruptUART() interrupt 4

 {

  if(RI) // Byte received    

{

RI = 0; //Manually clear the receive interrupt flag  

SBUF = SBUF + 1;    //The received data is +1 and then sent back. The left side is the sending SBUF and the right side is the receiving SBUF

}

if(TI) //Bytes sent  

{

TI = 0; //Manually clear the transmit interrupt flag    

}

 }

Reference address:#51 MCU# Basic application of UART serial communication, module introduction and serial program

Previous article:51 MCU serial communication uart notes
Next article:[Self-study 51 single-chip microcomputer] 11 -- UART serial communication

Recommended ReadingLatest update time:2024-11-17 00:04

STM32F4 UART DMA initialization
The internal DMA initialization of the F4 chip is very different from that of the F103. Data streams are introduced here. There are 16 data streams in total for 2 DMAs (8 for each controller). Each data stream can process data from up to 8 channels. Each channel has an arbitrator to handle the priority of DMA requests
[Microcontroller]
STM32F4 UART DMA initialization
Programming Tips: AVR UART I/O Function Module
This program implements the UART port functions of the AVR device, so that the PC terminal software can directly interact with the device. The program refers to the Samsung S3C2440 library file. The program consists of: test program , function module and its header file . /* ****************************
[Microcontroller]
S3C2410 -- UART
Automatic flow control mode S3C2410's UART0 and UART1 can both implement automatic flow control through their respective nRTS and nCTS signals. In automatic flow control (AFC) mode, nRTS depends on the state of the receiving end, while nCTS controls the operation of the send interrupt. Specifically: only when nCTS is
[Microcontroller]
S3C2410 -- UART
ARM-Linux S5PV210 UART driver (3) ----Serial port core layer, key structures, interface relationships
Although a specific UART device driver can be designed according to the design method of tty driver, that is, defining tty_driver and implementing the member functions of tty_operations, Linux has implemented a general tty driver layer for UART devices in the file serial_core.c, called the serial port core layer. In
[Microcontroller]
ARM-Linux S5PV210 UART driver (3) ----Serial port core layer, key structures, interface relationships
LPC17xx pin multiplexing Uart part
Pin multiplexing, that is, a pin can be used for multiple purposes, and the corresponding function is selected by setting the register. This is very common in microprocessors. I have used LPC2xxx series chips before, which have a lot of pin multiplexing, but the multiplexing functions of different pins will not be rep
[Microcontroller]
Use of STM32L0xx_HAL_Driver library——UART
MCU model: STM32L051C8T6 Development environment MDK5.12 Library version: STM32L0xx_HAL_Driver V1.1.0 Host environment: Windows XP I used to use the STM32F030C8T6 microcontroller for development. Due to changes in requirements, I replaced it with a new model STM32L051C8T6, mainly because of its low power consu
[Microcontroller]
S3C2440A UART
    UART (Universal Asynchronous Receiver/Transmitter) is the core device of RS-232C serial port, which is commonly used for development and debugging. You can find a detailed introduction to it on Baidu Encyclopedia ( http://baike.baidu.com/view/245027.htm ).     Although the UART serial port output function was us
[Microcontroller]
S3C2440A UART
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号