Detailed explanation and code example of serial port communication of 51 single-chip microcomputer

1. Serial port introduction

The serial port is a widely used communication interface. The serial port is low in cost, easy to use, and simple in communication lines, and can realize mutual communication between two devices.
The serial port of the single-chip microcomputer can make the single-chip microcomputer communicate with the single-chip microcomputer, the single-chip microcomputer and the computer, and the single-chip microcomputer and various modules, which greatly expands the application range of the single-chip microcomputer and enhances the hardware strength of the single-chip microcomputer system.
The 51 single-chip microcomputer has its own UART (Universal Asynchronous Receiver Transmitter, Universal Asynchronous Receiver Transmitter), which can realize the serial communication of the single-chip microcomputer.
insert image description here

2. Hardware circuit

Simple two-way serial communication has two communication lines (transmitter TXD and receiver RXD).
TXD and RXD need to be cross-connected.
When only one-way data transmission is required, a communication line can be used directly.
When the level standards are inconsistent, a level conversion chip needs to be added.

insert image description here

  • T - transmit (send);
  • X - exchange (exchange);
  • D - data (data);
  • R - receive (receive);

3. UART of 51 MCU

insert image description here
STC89C52 has 1 UART.
The UART of STC89C52 has four working modes:

  • Mode 0: synchronous shift register;
  • Mode 1: 8-bit UART, variable baud rate ( commonly used );
  • Mode 2: 9-bit UART with fixed baud rate;
  • Mode 3: 9-bit UART with variable baud rate;

4. Related registers

The relevant registers are as follows:
insert image description here

4.1 SCON: serial port control register (bit addressable)

The serial control register SCON is used to select the working mode of serial communication and some control functions. Its format is as follows:
insert image description here
SM0, SM1:
insert image description here

  • SM2: Allow mode 2 or mode 3 multi-machine communication control bit;
  • REN: Enable/disable traversal reception control bit.
    ○ REN is set by software, REN=1 is the state of allowing serial reception, and the serial receiver RxD can be started to start receiving information;
    ○ Software resets REN, that is, REN=0, then reception is prohibited;
  • TB8: In mode 2 or mode 3, it is the 9th data to be sent, set or cleared by software as required;
  • RB8: In mode 2 or mode 3, it is the 9th bit of data received;
  • TI: Transmit interrupt request flag. In mode 0, when the 8th bit of the serial transmission data ends, the internal hardware automatically sets the bit, that is, TI=1, and requests an interrupt from the host, and must be reset by software after responding to the interrupt, that is, TI=0. In other modes, it is set by internal hardware when the stop bit starts to be sent, and must be reset by software;
  • RI: Receive interrupt request flag. In mode 0, when the serial reception of the 8th bit ends, the internal hardware automatically sets RI=1, requests an interrupt from the host, and must be reset by software after responding to the interrupt, that is, RI=0. In other modes, the internal hardware sets RI=1 at the middle moment when the stop bit is received serially (see SM2 description for exceptions), and must be reset by software, RI=0.

0100 0000

4.2 PCON: Power Control Register (Not Bit Addressable)

PCON : Power Control Register (not bit addressable):
insert image description here

  • SMOD: Baud rate selection bit .
    ○ When SMOD is set by software, that is, SMOD=1, the baud rate of serial communication mode 1, 2, and 3 will be doubled;
    ○ SMOD=0, the baud rate of each working mode will not be doubled. SMOD=0 at reset.
  • SMOD0: frame error detection valid control bit.
    ○ When SMOD0=1, the SM0/FE bit in the SCON register is used for FE (frame error detection) function; ○
    When SMOD0=0, the SM0/FE bit in the SCON register is used for the SM0 function, together with SM1 to specify the serial port way of working. SMOD0=0 at reset

4.3 TMOD

insert image description here

insert image description here

5. Serial communication operation process

5.1 Send data process

Initialization:
• Step1: Configure serial port control register SCON to 0x40 (or 0x50);
• Step2: Configure power supply control register PCON (calculate baud rate);
• Step3: Configure timer T1 (serial port communication can only use timer 1, only Can use 8-bit auto-reload working mode ), start timer T1;
• Step4: disable timer T1 interrupt;

code show as below:

void UartInit()		//[email protected]
{
    
    
	PCON &= 0x7F;		//波特率不倍速
	SCON = 0x40;		//8位数据,仅用于发送
	TMOD &= 0x0F;		//清除定时器1模式位
	TMOD |= 0x20;		//设定定时器1为8位自动重装方式
	TL1 = 0xFA;		//设定定时初值
	TH1 = 0xFA;		//设定定时器重装值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//启动定时器1
}

[Note]: The serial port communication initialization code can also be obtained from STC-ISP:
insert image description here
just copy the code and delete the AURX statement.

send data:

//串口发送一个字节数据
void UART_SendByte(unsigned char Byte){
    
    
	SBUF=Byte;
	//检测是否完成
	while(TI==0);
	TI=0;//TI复位
}

5.2 Receive data process

Initialization:
• Step1: Configure the serial port control register SCON to 0x50;
• Step2: Configure the power control register PCON (calculate the baud rate);
• Step3: Configure the timer T1 (serial port communication can only use timer 1, only 8 bits Auto-reload working mode ), start timer T1;
• Step4: Start total interrupt and serial port interrupt;

//串口初始化
void UartInit()		//[email protected]
{
    
    
	PCON &= 0x7F;		//波特率不倍速
	SCON = 0x50;		//8位数据,可变波特率
	TMOD &= 0x0F;		//清除定时器1模式位
	TMOD |= 0x20;		//设定定时器1为8位自动重装方式
	TL1 = 0xFA;		//设定定时初值
	TH1 = 0xFA;		//设定定时器重装值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//启动定时器1
	//开启中断
	EA=1;	//总中断控制
	ES=1;	//串口中断
}

Receive data:

//串口发送一个字节数据
void UART_SendByte(unsigned char Byte){
    
    
	SBUF=Byte;
	//检测是否完成
	while(TI==0);
	TI=0;//TI复位
}

The interrupt utilizes interrupt 4 (interrupt 4):
insert image description here

code show as below:

//串口中断
void UART_Routine()    interrupt 4
{
    
    
	if(RI==1){
    
    
		P1=SBUF;//显示LED
		UART_SendByte(SBUF);//将数据发回电脑
		RI=0;//复位
	}
	
}

6. Baud rate calculation

insert image description here
insert image description here
Calculation method:
0xFA——>250 (overflow once every 256, that is, count 6 overflow once)
11.0592MHz crystal oscillator counts every 12/11.0592=1.08506944us in 12T mode
(12MHz crystal oscillator counts every 1s in 12T mode One time)
overflow once every 6*1.08506944=6.51041666us --> overflow frequency 1/6.51041666us=0.1536MHz
divided by 16 divided by 2 (do not double) -->0.0048MHz -->4800Hz (baud rate)

Seven, effect demonstration

Demo1: The MCU sends incremental data to the computer every second.
insert image description here
Demo2: The MCU receives the data sent by the computer and lights up the corresponding LED lights and returns the data to the computer for display.
Sending data f0 (1111 0000):
insert image description here
When the value is 0, the LED lights up, That is to light up D1-D3:
insert image description here

Demo1: The MCU sends incremental data to the computer every second. If you don’t understand it, you can refer to the complete code: https://download.csdn.net/download/didi_ya/85186251
Demo2: The MCU receives the data sent by the computer and lights up the corresponding LED If you don't understand it, you can refer to the complete code : https://download.csdn.net/download/didi_ya/85186270


Ok, the above is the whole content of this article, if it is helpful to you, remember to like it~

Guess you like

Origin blog.csdn.net/didi_ya/article/details/124289688