Chapter 6 Serial Communication Interface

Publisher:清新自然Latest update time:2024-08-09 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Basic principles of serial data communication

Computer data transmission has two modes: parallel and serial. The characteristics of parallel transmission are: each data bit is transmitted at the same time, with fast transmission speed and high efficiency. However, it requires more data lines, so the transmission cost is high. The distance of parallel data transmission is usually less than 30 meters. The characteristics of serial transmission are: data transmission is carried out in bit order, and at least one transmission line is required to complete it. The cost is low, but the speed is slow. The distance of serial data transmission can range from a few meters to thousands of kilometers.


1. Character format of asynchronous serial communication

Asynchronous serial data communication uses characters as units, that is, one character is transmitted at a time. The data format of asynchronous communication is:


One frame of information = 1 start bit (low level) + 5-8 data bits (following the start bit), indicating the valid data to be transmitted (low bit first) + 1 parity bit (optional) + 1, 1.5 or 2 stop bits (high level).


The time interval from the start bit to the stop bit is called a frame. It is the complete communication format of a character. Therefore, the character format of serial communication is also called the frame format.


2. Data path form of serial communication

Serial data communication has the following data path forms:


Simplex mode: A sends, B receives.

Half-duplex mode: A sends or receives, B receives or sends.

Full-duplex mode: A sends and receives, B receives and sends.

3. Serial communication transmission rate

The transmission rate is used to describe the speed of data transmission. In serial communication, data is transmitted bit by bit. Therefore, the transmission speed is expressed by the number of data bits transmitted per second, which is called the baud rate (Baud). One data bit transmitted per second is one baud. That is:


1 baud = 1bps (bit/second)


Commonly used baud rates are: 75, 110, 300, 600, 1200, 2400, 4800, 9600, 19200.


4. Serial communication interface circuit

There are two main technical issues in serial data communication:


Data transmission: Data transmission mainly solves problems such as standards, formats and working methods in transmission.

Data conversion: Data conversion refers to the serial-to-parallel conversion of data.

The basic logic block diagram of the serial interface circuit is as follows:


Serial input -> parallel output

Parallel input -> serial output

Clock, reset, control signal <-> status, control information

Universal Asynchronous Receiver Transmitter (UART) is a common interface for implementing asynchronous serial communication. Other common asynchronous serial communication interfaces include:


TTL level direct connection

RS-232C

RS-422, RS-485

20mA current loop

2. MCS-51 MCU serial port

The MCS-51 microcontroller has a full-duplex serial port that can be used for asynchronous serial communication and as a synchronous shift register. Timer T1 can be used as a serial communication baud rate generator.


1. Serial port register structure

The basic structure of the serial port of the MCS-51 microcontroller is as follows:


TXD serial output <- shift <- SBUF transmit register

RXD serial input -> shift -> SBUF receive register -> RI receive interrupt

Clock->Shift

SBUF is the buffer register of the serial port. It is a byte-addressable special register, which includes a send register (write only) and a receive register (read only), so that communication can be carried out in full-duplex mode. These two registers share the same address (99H).


For example:

Start sending: MOV SBUF, A; SBUF←(A)

Start receiving: MOV A, SBUF; A←(SBUF)


2. Registers related to serial communication

The special registers related to serial communication control are:


SCON: Serial control, used to set the working mode of the serial port

PCON: Power control, can control the baud rate.

IE: Set serial interrupt enable.

The format of the serial control register SCON (98H) is as follows:


SM0 SM1 SM2 REN TB8 RB8 TI RI

Function description:


SM0, SM1: Serial port working mode selection bits

00 Mode 0: Synchronous shift register, fixed baud rate.

01 Mode 1: 8-bit asynchronous, variable baud rate.

10 Mode 2: 9-bit asynchronous, fixed baud rate.

11 Mode 3: 9-bit asynchronous, variable baud rate.

SM2: Multi-machine communication control bit, mainly used in methods 2 and 3.

REN: Allow serial reception, set or cleared by software.

TB8/RB8:

In working modes 2 and 3, TB8 is the ninth bit of data sent, which is set (SETB TB8) or reset (CLR TB8) by software. In two-machine communication, this bit is used as a parity bit; in multi-machine communication, it is used to indicate that D7~D0 is an address frame or a data frame. That is: D8=0: indicates a data frame; D8=1: indicates an address frame.

RB8: In working modes 2 and 3, this bit is the 9th data bit received.

TI: Transmit interrupt flag. Before sending the stop bit, this bit is set by hardware to request an interrupt. After the CPU responds to the interrupt, the next frame of data can be sent. This bit must be cleared by software.

RI: Receive interrupt flag. When half of the stop bit is received, this bit is set by hardware (SM2 setting must also be considered), and an interrupt is requested. After the CPU responds to the interrupt, the next frame of data can be received. This bit must also be cleared by software.

The format of the power control register PCON (87H) is as follows:


SMOD - - - GF1 GF0 PD IDL

It is mainly used for power control of CHMOS type microcontrollers. For HMOS type microcontrollers, except for the highest bit (SMOD), all other bits are dummy. SMOD is the multiplication bit of the serial port baud rate.


When: SMOD = 1, the serial port baud rate is doubled. When the system is reset, SMOD = 0. It can be set by the following instructions:


MOV PCON, #80H; SMOD←1

MOV PCON, #00H; SMOD←0

3. How the serial port works

(1) Method 0

Mode 0 is the synchronous shift register mode. The RXD terminal is used as the entry or exit of data shift, and the TXD terminal provides the shift clock pulse. The transmission and reception of shifted data are based on 8 bits as a frame, without start bit and stop bit, the low bit is in front and the high bit is in the back, and the baud rate is fixed, which is fOSC/12. That is, one machine cycle shifts once.


Function: Used as I/O port expansion.


(2) Method 1

The frame format of mode 1 is: 1 start bit + 8 data bits + 1 stop bit.


Data transmission is started by a write transmit register (MOV SBUF, A) instruction. After that, the hardware automatically adds the start bit and stop bit to the serial port to form a complete frame format. Then, under the action of the shift pulse, it is serially output from the TXD terminal. After a character frame is sent, the TXD terminal output line is maintained at a high level, and TI is set to notify the CPU that the next character can be sent.


When receiving data, REN=1. Under this premise, the serial port samples the RXD terminal. When the sample jumps from 1 to 0, it is determined that the start bit is received. Then, under the control of the shift pulse, the received data is shifted into the receiving register. After the stop bit arrives, it is sent to RB8 and RI is set to notify the CPU to take a received character from SBUF.


The baud rate of mode 1 is variable and is determined by the following formula:


Baud rate = (2^SMOD)/32 * (T1 overflow rate)

= (2^SMOD)/32 * fOSC/(12*[256-X])

Among them: smod is the value of the highest bit of the PCON register, fOSC is the crystal frequency, and X is the initial count value when T1 works in mode 2 (8-bit automatic loading). In actual use, always determine the baud rate first, and then calculate the initial count value of T1. That is:


X = 256 - (2^SMOD) * fOSC/(384*baud rate)

(3) Method 2 and Method 3

When the serial port works in mode 2 or 3, it is a 9-bit asynchronous communication port. The information sent and received in one frame consists of 11 bits, namely:


1 start bit (0) + 8 data bits (D0 to D7) + 1 programmable bit (D8) + 1 stop bit (1)

The D8 bit can be used as a parity bit or a control bit. Its function is determined by the user. When transmitting, the programmable bit D8 (TB8) can be set to 0 (CLR TB8) or 1 (SETB TB8). When receiving, TB8 is sent to the RB8 bit in the SCON register.


The difference between methods 2 and 3 is the baud rate settings:


The baud rate of mode 2 is fixed. That is: baud rate = fosc/32 or fosc/64

The baud rate of mode 3 is variable. The calculation formula is the same as that of mode 1.

The following table lists some commonly used baud rates and corresponding time constant settings:

Baud rate fosc smod Timer 1 mode Time constant
1200 1.0592 0 2 E8H
2400 1.0592 0 2 F4H
4800 1.0592 0 2 FAH
9600 1.0592 0 2 FDH
19200 1.0592 1 2 FDH


The I/O port can be easily expanded by using serial port working mode 0.

[1] [2]
Reference address:Chapter 6 Serial Communication Interface

Previous article:Chapter 3 MCS-51 Instruction System
Next article:Chapter 7 Interrupt System

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号