Hardware Encyclopedia: Introduction to Microcontroller Serial Port

Publisher:HeavenlySunsetLatest update time:2024-02-27 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The serial port is a tool for information exchange between the microcontroller and the outside world.

■ There are two communication methods for 8051 microcontroller:

Parallel communication: Each bit of data is sent or received at the same time.

Serial communication: Data is sent or received bit by bit.

See the picture below:


Hardware Encyclopedia: Introduction to Microcontroller Serial Port


Serial communication methods include asynchronous communication and synchronous communication.

Asynchronous communication

It uses a start bit to indicate the beginning of a character and a stop bit to indicate the end of a character. The format of each frame is as follows:

In a frame format, there is a start bit 0, then 8 data bits, with the low bit first and the high bit last, followed by the parity bit (can be omitted), and finally the stop bit 1. Using this format to represent characters, characters can be transmitted one after another.

In asynchronous communication, there must be two regulations between the CPU and peripherals, namely character format and baud rate. The stipulation of the character format is that both parties can understand the same string of 0s and 1s into the same meaning. In principle, the character format can be freely determined by both parties in communication, but from the perspective of universality and convenience, it is generally better to use some standards, such as the ASCII standard.

Baud rate is the rate of data transmission, which is defined as the number of binary bits transmitted per second. For example, if the data transmission rate is 120 characters/s, and each character contains 10 digits as specified above, the transmission baud rate is 1200 baud.

Synchronous communication

In synchronous communication, each character uses a start bit and a stop bit as the start and end marks of the character, which takes up time; therefore, in order to increase the speed when transmitting data blocks, these marks are often removed and synchronous transmission is used. Since the start of the data block transfer is indicated by a synchronization character, and a clock is required to achieve synchronization between the sending end and the receiving end, the hardware is relatively complex.

communication direction

In serial communication, the one-way transmission method in which the communication interface can only send or receive is called simplex transmission; and the two-way transmission of data between machines A and B is called duplex transmission. The duplex transmission method is divided into half-duplex transmission and full-duplex transmission. Half-duplex transmission means that two machines cannot send and receive at the same time. At any time, they can only send or receive information.

Serial interface structure of 8051 microcontroller

The 8051 microcontroller serial interface is a programmable full-duplex serial communication interface. It can be used as an asynchronous communication method (UART) to connect to an external device that transmits information serially, or for full-duplex 8051 multi-machine systems using standard asynchronous communication protocols. It can also be used synchronously using TTL or CMOS bit register to expand the I/O port.

The 8051 microcontroller communicates with the outside world through pin RXD (P3.0, serial data receiving end) and pin TXD (P3.1, serial data transmitting end). SBUF is the serial port buffer register, including transmit register and receive register. They have the same name and address space, but there will be no conflict, because one of them can only read data by the CPU, and one can only write data by the CPU.

▲Serial port control and status register

Serial port control register SCON

It is used to define the working mode of the serial port and implement receiving and sending control. The byte address is 98H, and its bits are defined in the following table:


Hardware Encyclopedia: Introduction to Microcontroller Serial Port


SM0, SM1: Serial port working mode selection bits, their definitions are as follows:


Hardware Encyclopedia: Introduction to Microcontroller Serial Port


(Note: fosc is the crystal oscillator frequency)

SM2: Multi-machine communication control bit. In mode 0, SM2 must be equal to 0. In mode 1, when (SM2) = 1, RI is set to 1 only when a valid stop bit is received. In mode 2 or mode 3, RI is set to 1 when (SM2) = 1 and the ninth bit of data received RB8 = 0.

REN: Receive enable control bit. Set by software to allow reception, and cleared by software to disable reception.

TB8: It is the 9th bit of data to be sent. In mode 2 or mode 3, the 9th bit of data to be sent is set to 1 or cleared to 0 by software as needed. For example, it can be agreed as a parity bit, or as a flag bit to distinguish address frames or data frames in multi-machine communication.

RB8: The 9th bit of the received data. RB8 is not used in mode 0. In mode 1, if (SM2) = 0, RB8 is the received stop bit. In mode 2 or 3, RB8 is the 9th bit of data received.

TI: Send interrupt flag. In mode 0, when the 8th bit is sent, it is set by hardware. Set by hardware before sending stop bits in other ways. The TI setting not only indicates the end of sending a frame of information, but also applies for an interrupt. According to needs, you can use software query to obtain the information that the data has been sent, or use an interrupt to send the next data. TI must be cleared to 0 by software.

RI: receive interrupt flag bit. In mode 0, after receiving the 8th bit of data, it is set by hardware. In other ways, it is set by hardware at an intermediate moment when the stop bit is received (exceptions are found in the description of SM2). RI is set to indicate that one frame of data has been received, which can be obtained by query or interrupt. RI must also be cleared to 0 by software.

▲Special function register PCON

PCON is added to implement power control on the CHMOS 80C51 microcontroller. The highest bit is SMOD.

How the serial port works

The full-duplex serial port of the 8051 microcontroller can be programmed into 4 working modes, which are described below:

Mode 0 Shift register input/output mode.

An external shift register can be connected to expand the I/O port, and an external synchronous input/output device can also be connected. 8-bit serial data is input or output from RXD, and TXD is used to output synchronization pulses.

The output serial data is output from the RXD pin, and the TXD pin outputs the shift pulse. When the CPU writes data to the sending register, it immediately starts sending and outputs 8-bit data from RXD at a fixed baud rate of fos/12, with the low bit in front and the high bit in the back. After sending one frame of data, the sending interrupt flag TI is set by hardware.

Input When the serial port receives in mode 0, the reception permission control bit REN is first set. At this time, RXD is the serial data input terminal, and TXD is still the synchronization pulse shift output terminal. When (RI)=0 and (REN)=1 are satisfied at the same time, reception starts. When the 8th bit of data is received, the data is moved into the receive register and RI is set by hardware.

The following two figures are the wiring diagrams of mode 0 extended output and input respectively.


Hardware Encyclopedia: Introduction to Microcontroller Serial Port


<MCU serial port wiring diagram>

Mode 1: 10-bit asynchronous communication interface mode with variable baud rate.

Send or receive a frame of information, including 1 start bit 0, 8 data bits and 1 stop bit 1.

Output When the CPU executes an instruction to write data to the send buffer SBUF, the send is started. Serial data is output from the TXD pin. After sending a frame of data, TI is set by hardware.

When the input is (REN) = 1, the serial port samples the RXD pin. When the transition from 1 to 0 is sampled, it is confirmed to be the start bit 0 and starts receiving a frame of data. Only when (RI) = 0 and the stop bit is 1 or (SM2) = 0, the stop bit enters RB8, the 8-bit data can enter the receiving register, and the interrupt flag RI is set by hardware; otherwise the information is lost. Therefore, when receiving in mode 1, the RI and SM2 flags should be cleared by software first.

Mode 2: 11-bit UART mode with fixed baud rate.

Compared with method 1, it adds a ninth bit of data that can be programmed to 1 or 0.

Output: The sent serial data is output from the TXD terminal as a frame of information as 11 bits. The additional 9th ​​bit comes from the TB8 bit of the SCON register, which is set or reset by software. It can be used as a flag bit for address/data information in multi-machine communication, and can also be used as a parity bit for data. When the CPU executes an instruction to write data to SUBF, it starts the transmitter to send. After sending a frame of information, the interrupt flag TI is set.

Input: When (REN) = 1, the serial port samples the RXD pin. When the transition from 1 to 0 is sampled, it is confirmed to be the start bit 0 and starts receiving a frame of data. After receiving the additional 9th ​​bit of data, when (RI) = 0 or (SM2) = 0, the 9th bit of data will enter RB8, and the 8-bit data will enter the receiving register, and the interrupt flag RI will be set by hardware; Otherwise the information will be lost. And RI is not set. After another bit of time, regardless of whether the above conditions are met, the receiving circuit is reset and re-detects the transition from 1 to 0 on RXD.

Mode 3: 11-bit UART mode with variable baud rate.

Except for the baud rate, the rest is the same as method 2.

About baud rate selection

As mentioned before, in serial communication, the data transmission rate (baud rate) of both the sending and receiving parties must have a certain agreement. Among the four working modes of the 8051 serial port, the baud rates of modes 0 and 2 are fixed, while the baud rates of modes 1 and 3 are variable and are controlled by the overflow rate of timer T1.

▲ Mode 0

The baud rate of mode 0 is fixed to 1/12 of the main oscillator frequency.

▲ Method 2

The baud rate of mode 2 is determined by the selection bit SMOD in PCON, and can be expressed by the following formula:

Baud rate = 2 to the power of SMOD divided by 64 and multiplied by fosc. That is, when SMOD = 1, the baud rate is 1/32 fosc. When SMOD = 0, the baud rate is 1/64 fosc.

[1] [2]
Reference address:Hardware Encyclopedia: Introduction to Microcontroller Serial Port

Previous article:Extension method analysis based on interface chip 8255
Next article:Design of USB2.0 data transmission system based on CY7C68013 and GPIF mode

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号