[MCU Learning] 51 MCU [Serial Port], detailed introduction

Publisher:创意探险Latest update time:2022-07-28 Source: csdnKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Know before you know

1. Basics of computing serial communication

  

2. Basic concepts of serial communication

(1) Asynchronous communication and synchronous communication

a. Asynchronous communication

Asynchronous communication refers to the process in which the sending and receiving devices use their own clocks to control the sending and receiving of data. In order to coordinate the sending and receiving of both parties, the clocks of the sending and receiving devices are required to be as consistent as possible.


Asynchronous communication is transmitted in units of characters (frames). The gaps (time intervals) between characters are arbitrary, but each bit in each character is transmitted at a fixed time. That is, the characters are not necessarily related by an integer multiple of the "bit interval", but the distances between each bit in the same character are all integer multiples of the "bit interval".

b. Synchronous communication

In synchronous communication, the sender's clock directly controls the receiver's clock so that both parties are completely synchronized. At this time, the distance between the bits of the transmitted data is an integer multiple of the "bit interval", and there is no gap between the transmitted characters, that is, the bit synchronization relationship is maintained, and the character synchronization relationship is also maintained. The synchronization of the sender to the receiver can be achieved in two ways.

(2) Transmission direction of serial communication

Simplex: Data transmission can only be in one direction and cannot be reversed;

Half-duplex: Data transmission can be in both directions, but it needs to be done in time division;

Full-duplex: Data can be transmitted in both directions simultaneously.

(3) Common error checking in serial communication

Parity


When sending data, the 1 bit following the data bit is the parity bit (1 or 0). When odd parity is used, the sum of the number of "1"s in the data and the number of check bits "1" should be an odd number; when even parity is used, the sum of the number of "1"s in the data and the number of check bits "1" should be an even number. When receiving characters, the number of "1"s is checked. If it is inconsistent, it means that an error has occurred during the data transmission process.


Code and verification


The code and checksum is that the sender sums the data block (or XORs each byte) to generate a one-byte checksum character (checksum) and appends it to the end of the data block. The receiver receives the data and sums the data block (except the check byte) (or XORs each byte), and compares the result with the sender's "checksum". If they match, there is no error. Otherwise, it is considered that an error occurred during the transmission process.


Cyclic Redundancy Check


This type of verification is a cyclic verification between valid information and check bits through some mathematical operations, which is often used for transmission of disk information, integrity verification of storage areas, etc. This verification method has strong error correction capability and is widely used in synchronous communications.


Parity check is usually used, and the others are understandable.


(4) Transmission rate

Bit rate (also known as baud rate), the number of bits of binary code transmitted per second, the unit is: bits/second (bps).


240 characters are transmitted per second, and each character format contains 10 bits (1 start bit, 1 stop bit, 8 data bits). The bit rate is: 10 bits × 240/second = 2400 bps


(5) Relationship between transmission distance and transmission rate

The maximum distance that a serial interface or terminal can directly transmit a serial information bit stream is related to the transmission rate and the electrical characteristics of the transmission line. When the transmission line uses an unbalanced shielded twisted pair with a 50PF capacitance per 0.3 meter (about 1 foot), the transmission distance decreases as the transmission rate increases. When the bit rate exceeds 1000bps, the maximum transmission distance drops rapidly.


At 9600bps, the maximum distance drops to only 76m (about 250 feet).


That is to say, [the greater the transmission speed, the smaller the transmission distance].


(6) Serial communication interface standards

a. RS-232C interface

RS-232C is the RS-232C standard revised by EIA (Electronic Industries Association) in 1969. RS-232C defines the physical interface standard between data terminal equipment (DTE) and data communication equipment (DCE).


<1> Mechanical properties

The RS-232C interface specifies the use of a 25-pin connector, with the connector size and the arrangement of each pin clearly defined. (Male)

<2>, Functional characteristics

<3>, Process characteristics

Process characteristics dictate the timing relationships between signals so that data can be received and sent correctly.

[External link image transfer failed. The source site may have an anti-hotlink mechanism. It is recommended to save the image and upload it directly (img-FlZQ5NIX-1644332739831)(assets/image-20220208223750368-164433107178262.png)]

Note: Send and receive are connected, don't connect them wrongly


b. RS-232C level and TTL level conversion drive circuit

The MC1488 chip converts the TTL level to the RS-232 level, that is, converts 5V to 12V for communication.


The MC1489 chip converts the RS-232 level into the TTL level, that is, converts 12V into 5V for communication.

c.RS422A interface

The RS-422A output driver is a double-ended balanced driver. If one line is in the logic "1" state, the other line is in the logic "0", which is twice as large as the voltage amplification of a single-ended unbalanced driver. The differential circuit can pick up valid signals from ground interference, and the differential receiver can distinguish potential differences of more than 200mV. If interference and noise are mixed in the transmission process, the interference and noise can be offset by each other due to the effect of the differential amplifier. Therefore, the influence of ground interference and electromagnetic interference can be avoided or greatly reduced. When the RS-422A transmission rate (90Kbps) is used, the transmission distance can reach 1200 meters.


d.RS-485 interface

RS-485 is a variation of RS-422A: RS-422A is used for full-duplex, while RS-485 is used for half-duplex. RS-485 is a multi-transmitter standard that can use up to 32 pairs of differential drivers/receivers on the communication line. If more than 32 devices are connected in a network, repeaters can also be used.

RS-485 uses voltage between two lines to represent logic 1 and logic 0. Since the sender needs two transmission lines, the receiver also needs two transmission lines. The transmission line uses a differential channel, so it has excellent interference suppression. Because of its low impedance and no grounding problem, the transmission distance can reach 1200 meters and the transmission rate can reach 1Mbps.


2. Serial port of 80C51

1. Structure of 80C51 serial port

There are two physically independent receive and transmit buffers SBUF, which occupy the same address 99H; the receiver is a double buffer structure; the transmit buffer, because the CPU is active during transmission, will not cause overlap errors.


2. Register SCON

SCON is a special function register used to set the serial port's operating mode and receive/send control.


It sets the status flags:

SM0 and SM1 are the working mode selection bits, and four working modes can be selected:

Common usage 1


SM2, multi-machine communication control bit, is mainly used in mode 2 and mode 3.


When the receiver's SM2=1, RB8 can be used to control whether to activate RI


RB8=0, RI is not activated, and the received information is discarded;

RB8=1, the received data enters SBUF, and RI is activated, and the data is read from SBUF in the interrupt service.

When SM2=0, no matter whether the received RB8 is 0 or 1, the received data can enter SBUF and activate RI. (That is, RB8 does not control the activation of RI at this time). [By controlling SM2, multi-machine communication can be achieved].


**Note:** In mode 0, SM2 must be 0; in mode 1, if SM2=1, RI is set to 1 only when a valid stop bit is received.


REN, serial receive enable bit.


When software sets REN=1, the serial port starts receiving data.


If the software sets REN=0, the binary reception is performed.


TB8, in mode 2 or mode 3, is the ninth bit of the transmitted data and its function can be defined by software.


It can be used as the parity bit of data or as the flag bit of address frame/data frame in multi-machine communication. (This bit is not used in mode 0 and mode 1)


RB8, in Mode 2 or Mode 3, is the ninth bit of the received data.


As parity bit or address frame/data frame flag bit. (In mode 1, if SM2=0, RB8 is the received stop bit.


TI, transmit interrupt flag.


In mode 0, when the serial transmission of the 8th bit of data ends, or in other modes, when the serial transmission of the stop bit begins, the internal hardware sets TI to 1 and sends an interrupt request to the CPU. In the interrupt service routine, it must be cleared by software to cancel the interrupt request.


RI, receive interrupt flag.


In mode 0, when the serial reception of the 8th bit of data is completed, or in other modes, when the serial reception is in the middle of the stop bit, the internal hardware sets RI=1 and sends an interrupt request to the CPU. It must also be cleared by software in the interrupt service routine to cancel this interrupt.


3. Register PCON

There is only one SMOD in PCON that is related to the serial port operation:

SMOD (PCON.7) Baud rate multiplication bit. In serial port mode 1, mode 2, mode 3, the baud rate is related to SMOD. When SMOD=1, the baud rate is doubled. When reset, SMOD=0.


3. How the serial port works

1. Method 0

a. Output

In mode 0, the serial port is in the input and output mode of synchronous shift register. It is mainly used to expand parallel input or output ports.


Data is input or output by the RXD (P3.0) pin, and the synchronous shift pulse is output by the TXD (P3.1) pin. Both sending and receiving are 8-bit data, with the low bit first and the high bit later. The baud rate is fixed at fosc/12.

1. Mode 0 output

[1] [2]
Keywords:MCU Reference address:[MCU Learning] 51 MCU [Serial Port], detailed introduction

Previous article:[MCU Learning] 51 MCU [Timer/Counter], detailed introduction
Next article:[MCU Learning] 51 MCU [Interrupt System], detailed introduction

Recommended ReadingLatest update time:2024-11-16 10:35

How to use the EEPROM in PIC microcontroller
/*============================================================================= Access the EEPROM on the PIC16F877A chip and run the program:         Begin Test - LED0,         EEPROM Test OK- LED7 is on,         Write&Read OK - LED0/2/4/6亮。 ============================================================================
[Microcontroller]
Design of information monitoring terminal using AVR single chip microcomputer and LCD module
At present, with the rapid development of the information industry, all kinds of ships are in urgent need of being equipped with more high-quality and low-cost small and medium-sized information monitoring terminals to monitor weather conditions and obtain navigation information to ensure navigation safety. 32-bit pro
[Microcontroller]
Design of information monitoring terminal using AVR single chip microcomputer and LCD module
ATMEGA8 microcontroller drives stepper motors
#include #include #defineucharunsignedchar #defineuintunsignedint ucharnp; //Stepper motor operation data table constucharmotortb[]={0x11,0x99,0x88,0xCC,0x44,0x66,0x22,0x33}; voiddelay_nms(uintms) //delay subroutine for each step { swimming; for(i=0;i_delay_loop_2(8*250); } void a_step (uchard, uchart) //stepper motor
[Microcontroller]
ATMEGA8 microcontroller drives stepper motors
MCU Growth Path (51 Basics) - 015 Multi-file compilation example about sdcc
This article is a continuation of the MCU Growth Path (51 Basics) - 009 Multi-file compilation example (I) about sdcc.  In actual work, it is impossible to put all the header files and function functions of the microcontroller in the same folder. We split the code of the MCU Growth Road (51 Basics) - 009 about the m
[Microcontroller]
MCS-51 microcontroller command system (3)
Instructions are divided into 7 addressing modes according to the addressing mode, direct addressing (direct), immediate addressing (#20H), register addressing (Rn), register indirect addressing (@Ri), relative addressing (rel) , register index addressing (@A+PC), bit addressing (ACC.1) There are five types of instr
[Microcontroller]
Analysis of NSS management of STM32 microcontroller SPI interface
I have read a lot of information about the configuration of NSS chip select signals, but I feel that it is not explained clearly. I have written some of my personal opinions and hope that it will be useful to everyone. First, let's take a look at the structure diagram of NSS, as follows. From the above figure we c
[Microcontroller]
Analysis of NSS management of STM32 microcontroller SPI interface
Temperature control program and circuit schematic based on 51 single chip microcomputer
/*------------------------------------------------------------------------------------------------------------------------------------------------------------ * * * This program is a complete temperature controller program * * Minimum system temperature control board (attached below) * * Functions to be implemente
[Microcontroller]
Temperature control program and circuit schematic based on 51 single chip microcomputer
PIC32MX MCU Peripheral Library Usage (I) - Basic Settings of System Clock and I/O Ports
Development environment: MAPLAB X IDE v1.85 Compiler: MPLAB XC 32 1. Instructions for use: The PIC32 Peripheral Library provides functions and macros for setting up and controlling 32-bit peripherals. An application that wishes to use the peripheral library can access any supported functions and macros simply by
[Microcontroller]
PIC32MX MCU Peripheral Library Usage (I) - Basic Settings of System Clock and I/O Ports
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号