Proteus8 simulation: 51 MCU uses serial port for dual-machine communication

51 MCU uses serial port for two-machine communication

components

components name
resistance RES
51 MCU AT89C51
capacitance CAP
crystal oscillator CRYSTAL
BCD digital tube 7SEG-BCD

Schematic part

insert image description here

Serial port initialization:
first write the corresponding initialization code according to the relevant serial port registers:
insert image description here
STC89C51 single-chip microcomputer has two timers/counters, because timer 1 has 4 working modes, and timer 1 working mode 2 (8-bit automatic reload) as the overflow rate for the baud rate.
The serial port register mainly configures the SCON register.
insert image description here
insert image description here
This time, 8 is used as a variable, so SM0=1;SM1=0;REN=1;through the calculation of the baud rate, the relationship between the baud rate and the initial value of the timer can be calculated, and then the timer initialization and serial port initialization can be written.

void initUart(unsigned int baud)
{
    
    
	SM0=1;SM1=0;REN=1;//8位波特率可变,允许串口接受信号
	TMOD|=0X20;//定时器1为8位自动重装载
	PCON=0X00;//让SMOD==0
	TR1=1;//打开定时器1
	EA=1;ET1=1;//打开串口中断
	TH1=256-11059200/12/32/baud;//设置定时器初值
	TL1=256-11059200/12/32/baud;//设置定时器初值
}

the code

In this experiment, the two machines communicate through the serial port to check whether the data of both parties are the same, and if they are the same, they will be displayed on the digital tube.

MCU 1main.c

code show as below:

#include "reg51.h"

//宏定义
#define uchar unsigned char
#define uint unsigned int
//延时函数
void time(uint ucMs);
//串口初始化
void initUart(unsigned int baud);
//参数定义
uchar ucCounter;

void main(void)
{
    
    
	uchar counter=0;
	time(1);
	initUart(9600);//串口进行初始化
	while(1)
	{
    
    
		SBUF=counter;//将参数给寄存器
		while(TI==0);//发送中断,发送8位时为1
		TI=0;//软件置0
		while(RI==0);//接受中断,接受8位时为1
		RI=0;//软件置0
		if(SBUF==counter)//如果接受到的等于counter
		{
    
    
			P2=counter;//数码管显示counter
			if(++counter>15)counter=0;//大于15清零
			time(500);//延时
		}
	}
}


void initUart(unsigned int baud)
{
    
    
	SM0=1;SM1=0;REN=1;//8位波特率可变,允许串口接受信号
	TMOD|=0X20;//定时器1为8位自动重装载
	PCON=0X00;//让SMOD==0
	TR1=1;//打开定时器1
	EA=1;ET1=1;//打开串口中断
	TH1=256-11059200/12/32/baud;//设置定时器初值
	TL1=256-11059200/12/32/baud;//设置定时器初值
}

//延时函数
void time(uint ucMs)
{
    
    
	while(ucMs!=0)
	{
    
    
		for(ucCounter=0;ucCounter<239;ucCounter++);
		ucMs-=1;
	}
}


MCU 2main.c

#include "reg51.h"

//宏定义
#define uchar unsigned char
#define uint unsigned int
//延时函数
void time(uint ucMs);
//串口初始化
void initUart(unsigned int baud);
//参数定义
uchar ucCounter;

void main(void)
{
    
    
	uchar receive;
	time(1);
	initUart(9600);//串口初始化
	while(1)
	{
    
    
		while(RI==0);//接受8为置1
		RI=0;
		receive=SBUF; //接受信号
		SBUF=receive;//将接受的发出去
		while(TI==0);//发送8位置1
		TI=0;
		P2=receive;//数码管显示信号
	}
}

//串口初始化
void initUart(unsigned int baud)
{
    
    
	SM0=1;SM1=0;REN=1;//8位波特率可变,允许串口接受信号
	TMOD|=0X20;//定时器1为8位自动重装载
	PCON=0X00;//让SMOD==0
	TR1=1;//打开定时器1
	EA=1;ET1=1;//打开串口中断
	TH1=256-11059200/12/32/baud;//设置定时器初值
	TL1=256-11059200/12/32/baud;//设置定时器初值
}

//延时函数
void time(uint ucMs)
{
    
    
	while(ucMs!=0)
	{
    
    
		for(ucCounter=0;ucCounter<239;ucCounter++);
		ucMs-=1;
	}
}

The running effect is as shown in the figure:
insert image description here

Project Files

Project Files

Guess you like

Origin blog.csdn.net/darlingqx/article/details/128285034