51 MCU Tutorial from Scratch—— 22 MCU Serial Port Introduction

Publisher:SereneWandererLatest update time:2012-02-16 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The serial port is a tool for the microcontroller to exchange information with the outside world.

There are two communication modes for 8051 microcontroller:

Parallel communication: data bits are sent or received at the same time. Serial communication: data bits are sent or received sequentially. See the figure below:

Click to browse the next page

Serial communication method:

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 first, then 8 data bits, with the low bit first and the high bit last, followed by the parity bit (which can be omitted), and finally the stop bit 1. Using this format to represent characters, characters can be transmitted one by one.

In asynchronous communication, there must be two regulations between the CPU and the peripherals, namely, the character format and the baud rate. The character format is stipulated so that both parties can understand the same string of 0s and 1s as 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 speed at which data is transmitted, and its definition is the number of bits of binary numbers 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 a mark of the beginning and end of the character, which takes time; so when transferring data blocks, in order to increase the speed, these marks are often removed and synchronous transmission is used. Since the start of data block transfer must be indicated by a synchronization character, and a clock is required to achieve synchronization between the sender and the receiver, the hardware is more 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 two machines A and B is called duplex transmission. Duplex transmission is divided into half-duplex transmission and full-duplex transmission. Half-duplex transmission means that the two machines cannot send and receive at the same time, and can only send or receive information at any time.

2. 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 external devices that transmit information serially, or for full-duplex 8051 multi-machine systems using standard asynchronous communication protocols. It can also be used in a synchronous manner to expand I/O ports using TTL or CMOS shift registers.

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

Serial port control and status registers

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 bit definitions are as follows:

D7
D6
D5
D4
D3
D2
D1
D0
SM0
SM1
SM2
REN
TB8
RB8
TI
RI

SM0, SM1: Serial port working mode selection bits, which are defined as follows:

SM0, SM1
How it works
Functional Description
Baud rate
0 0
Method 0
8-bit shift register
Fosc/12
0 1
Method 1
10-bit UART
variable
1 0
Method 2
11-bit UART
Fosc/64 or fosc/32
1 1
Method 3
11-bit UART
variable

Where 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, when (SM2) = 1 and the received ninth bit of data RB8 = 0, RI is set to 1.

REN: Receive enable control bit. It is set by software to enable reception and cleared by software to disable reception.

TB8: is the 9th bit of the 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 to be used 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 mode 3, RB8 is the 9th bit of the received data.

TI: Transmit interrupt flag. In mode 0, it is set by hardware when the 8th bit is sent. In other modes, it is set by hardware before the send stop bit. TI setting indicates that a frame of information has been sent, and it also requests an interrupt. You can use software query to obtain 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. In mode 0, it is set by hardware after receiving the 8th bit of data. In other modes, it is set by hardware at the middle of receiving the stop bit (see the description of SM2 for exceptions). RI is set to indicate that a 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 realize power control on 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 four working modes, which are described as follows:

Mode 0 is shift register input/output mode. You can connect an external shift register to expand the I/O port, or connect an external synchronous input/output device. 8-bit serial data is input or output from RXD, and TXD is used to output synchronous pulses.

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

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

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

Click to browse the next page

Mode 1 is a 10-bit asynchronous communication interface with variable baud rate. Sending or receiving a frame of information includes 1 start bit 0, 8 data bits and 1 stop bit 1.

Output When the CPU executes an instruction to write data into the transmit buffer SBUF, transmission is started. Serial data is output from the TXD pin, and after a frame of data is sent, TI is set by hardware.

When the input (REN) = 1, the serial port samples the RXD pin. When the sampled jump from 1 to 0 is confirmed to be the start bit 0, it starts to receive a frame of data. Only when (RI) = 0 and the stop bit is 1 or (SM2) = 0, the stop bit enters RB8, and 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.

Method 2

Mode 1 is an 11-bit UART mode with a fixed baud rate. Compared with mode 1, it adds a 9th data bit which can be programmed to 1 or 0.

Output: The serial data sent is output from the TXD terminal. One frame of information is 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, the transmitter is started 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 sampled jump from 1 to 0 is confirmed to be the start bit 0, it starts to receive a frame of data. After receiving the additional 9th ​​bit of data, when (RI) = 0 or (SM2) = 0, the 9th bit of data enters RB8, and the 8 bits of data enter the receiving register, and the hardware sets the interrupt flag RI; otherwise, the information is 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 jump from 1 to 0 on RXD.

Working method 3

Mode 3 is an 11-bit UART mode with variable baud rate. Except for the baud rate, everything else is the same as Mode 2.

Baud rate selection

As mentioned above, in serial communication, the data transmission rate (baud rate) between the sender and the receiver must be agreed upon. 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 controlled by the overflow rate of timer T1.

Method 0

The baud rate of mode 0 is fixed at 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 as follows:

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

3. Method 1 and Method 3

Timer T1 is used as a baud rate generator, and its formula is as follows:

Baud rate = Timer T1 overflow rate Click to browse the next page

T1 overflow rate = T1 count rate / number of cycles required to generate overflow

The count rate of T1 depends on whether it works in the timer state or the counter state. When working in the timer state, the count rate of T1 is fosc/12; when working in the counter state, the count rate of T1 is the external input frequency, which should be less than fosc/24. The cycle required to generate overflow is related to the working mode of timer T1 and the preset value of T1.

Timer T1 works in mode 0: the number of cycles required for overflow = 8192-x

Timer T1 works in mode 1: the number of cycles required for overflow = 65536-x

Timer T1 works in mode 2: the number of cycles required for overflow = 256-x

Since mode 2 is an 8-bit timer/counter mode that automatically reloads the initial value, it is most appropriate to use it as a baud rate generator.

When the clock frequency is selected as 11.0592MHZ, it is easy to obtain the standard baud rate, so this is why many microcontroller systems choose this seemingly "strange" crystal oscillator.

The following table lists the commonly used baud rates and initial values ​​when timer T1 works in mode 2.

Common baud rates
Fosc(MHZ)
SMOD
TH1 initial value
19200
11.0592
1
FDH
9600
11.0592
0
FDH
4800
11.0592
0
FAH
2400
11.0592
0
F4H
1200
11.0592
0
E8

Keywords:MCU Reference address:51 MCU Tutorial from Scratch—— 22 MCU Serial Port Introduction

Previous article:51 MCU Tutorial from Scratch - MCU Timer/Counter Experiment
Next article:51 MCU Tutorial from Scratch—— 23 MCU Serial Port Communication Programming

Recommended ReadingLatest update time:2024-11-16 16:24

Introduction to PIC 16C84 microcontroller
  PIC16C84 is an 8-bit CMOS EEPROM microcontroller. It has high-performance RISC-like instructions, with a total of 35 single-byte instructions. All instructions require only one instruction cycle, except for program branch instructions, which require two instruction cycles. When the main oscillation frequency is 10MH
[Microcontroller]
51+PIC microcontroller compatible program 1602 LCD display welcome interface
//Mistake: In 51, RW (L command, H data) is directly soldered to ground //∴ lcd_enable() is missing; this function {lcdrs = 0; lcdrw = 0; lcden = 0; delay(5); lcden = 1;} //Without this function setting, it cannot be displayed #define PIC // Description, if it is 51, you can delete this sentence #define ww //Instruct
[Microcontroller]
51+PIC microcontroller compatible program 1602 LCD display welcome interface
Some Experiences in 51 Single Chip Microcomputer Development
1. The default level of the MCU IO port is high after power-on. (P1, P2, and P3 ports are internally pulled up. If the P0 port is not connected to a pull-up resistor, the default is high impedance.) 2. The operating frequency of the microcontroller = crystal oscillator frequency/12, that is, using a 12MHz crystal
[Microcontroller]
MCU Keil running water lamp experiment
Before doing the running light experiment, you must first figure out how each light-emitting diode is controlled. Secondly, after understanding Keil's internal left and right shift functions, you can write a relatively short running light experiment code. I encountered some problems during the writing process, such
[Microcontroller]
External memory expansion based on single chip microcomputer
1. Experimental purpose 1. Learn the method of expanding off-chip memory. 2. Learn different methods of reading and writing data memory. 3. Learn the method of reading off-chip program memory. 2. Experimental content 1. Experimental principle diagram: 2. Experimental content (1) Us
[Microcontroller]
External memory expansion based on single chip microcomputer
Introduction to MSP430 Series MCU
1. Development of MSP430 microcontroller   The MSP430 series is a 16-bit, ultra-low power hybrid microcontroller with a streamlined instruction set. It was launched in 1996. It has become a shining star among many microcontroller series due to its extremely low power consumption, rich on-chip peripherals and conveni
[Microcontroller]
I also play with single chip microcomputer: running water lamp
1. Light up a P1.0 LED 1. Source code 1 #include reg52.h 2 #define uint unsigned int //macro definition 3 #define uchar unsigned char //macro definition 4 sbit led=P1^0; /*define P1.0*/ 5 sbit led1=P1^1;/*define P1.1*/ 6 7 main() 8 {   9 uint a; 10 while(1) 11 { a=50000; 12 led=0; /*turn on th
[Microcontroller]
I also play with single chip microcomputer: running water lamp
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号