51 MCU serial communication principle explanation

Publisher:theta18Latest update time:2024-08-22 Source: cnblogs Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Today I studied the serial port communication of 51 single-chip microcomputer. The single-chip microcomputer used is the Puzhong Technology Development Board, but in fact the hardware used is the same as that of other brands of single-chip microcomputers, there is no difference. Let me summarize my understanding and opinions.


Communication Principles

The communication principle can be roughly divided into two methods: serial and parallel. Each has its own advantages and disadvantages. I will not go into details here. The method used is serial communication. Let me briefly introduce the principle of serial communication. The figure above shows

two devices, one transmission line, and each group of data is transmitted. The total length is not necessarily 8 bits, which is determined by the internal setting of the 51 single-chip microcomputer. There are many interface methods for communication between devices. I use the serial interface on 51. The hanging chart:
insert image description here
SBUF: refers to the two buffer registers in the serial port, one is the sending register and the other is the receiving register. They are completely independent in physical structure, but the addresses are overlapping. They are both byte-addressed registers, and the byte addresses are all 99H,
TXD: Transmit (tx) Data; RXD: Receive (rx) Data;
Remember the two ports by abbreviations. I often can't remember (shame), so the upper SBUF is the sending, and the lower one is the receiving register. The data sent or received will be temporarily stored in it. Just assign values ​​directly when programming. TH1 and TL1 are the clock configuration system, which is mainly used to control the baud rate and the total number of bits sent per second. (When debugging, be sure to set the baud rate accordingly)

Control register SCON: The internal structure is shown in the figure below. It is mainly used to set the serial port working mode, receive and send control, and control the status bit
insert image description here
. SM0 and SM1 are the control working mode shown in the figure below, which control the number of bits of each group of total data (start and stop bits + data bits). Shift is to send input and output data one pulse at a time.
SM2 is the multi-machine communication control bit, in mode 2 and 3. SM2 controls whether RB8 will trigger the RI interrupt. When SM2=1, R8=1 activates the interrupt (the interrupt reads the data away), and R8=0 does not activate it; SM2=0 loses the control function. Regardless of whether RB is 0, RI can be activated. In mode 0, SM2 must be 0; in mode 1, SM2=0, when the stop bit is received, the R1 interrupt is turned on.
REN allows serial reception bit. REN=1 turns on reception, otherwise data cannot be received.
TB, used only in modes 2 and 3, is the parity check bit
RB. In modes 2 and 3, it is still used as a parity check bit. In mode 1, it is used to store the data stop bit, which is used to automatically set RI to 1 and start the interrupt.
insert image description here
PCON: used to control whether the baud rate is doubled, and SMOD=1, the baud rate is doubled. SMOD=0 at reset;
insert image description here
OK, the general communication principle is here, and the following is an explanation of its use!

Programming

Steps:
1. Determine the working mode of TMOD (counter)
2. Configure the initial values ​​of TH1 and TL1
3. Configure SCON and PCON
4. Enable the interrupt enable bit (general interrupt, serial port interrupt), configure the interrupt (mainly SBUF reads the send data in the interrupt), configure TCON to enable the interrupt, that is, set TR1 to 1.

The above code: acts as the serial communication input value and then returns

include

typedef unsigned char u8;


void ready(void)

{

TMOD = 0X20;

TH1=0XFF;

TL1=0XF9; //Baud rate 9600

SCON=0X50;//0101 0000

PCON=OX80; //1000 0000

EA=1; //Open the general interrupt

ES=1; //Open serial port interrupt, equivalent to ET1 ET0

TR1=1; //Turn on the counter. When it overflows, it will give SMOD a pulse to receive and read data to set the baud rate.

//When the stop bit is received, RI=1 triggers an interrupt

}


void main(void)

{

ready();

while(1);

}


void time1(void) interrupt 4

{

static u8 result;

result=SBUF; //Enter configuration interrupt and read data

RI=0;

SBUF=result; // Input data into SBUF and send

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

TI=0;

}

A simple serial communication is completed.


Reference address:51 MCU serial communication principle explanation

Previous article:51 MCU Learning Log-Instruction System
Next article:data,bdata,idata,pdata,xdata,code storage types and storage areas

Latest Microcontroller Articles
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号