Serial - add value to the transmission sequence number

Any of a number of bytes transmitted, received and sent back after the MCU plus serial number comprising a maximum of 255

Code

#include"reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
u16 a;
u16 i=0;


void usart_init()    //串口初始化函数
{
	SCON=0x50;       //scon寄存器 sm1=1 REN=1
	EA=1;            //中断总允许位
	ES=1;            //串口中断允许位
	TMOD=0x20;       //TMOD寄存器  定时器T1 方式2
	TH1=TL1=0XF4;    //波特率为2400
	TR1=1;           //开启定时器
}

void main()      
{
	usart_init();
	while(1);
}

void usart() interrupt 4   //中断服务函数
{
	
	RI=0;
	a=SBUF;
	if(i<256)
	{
		SBUF=i;
		while(!TI);
		TI=0;
		i++;
		if(i==255)i=0;		
	}
	SBUF=a;
	while(!TI);
	TI=0;
}

Simulation Figure

Here Insert Picture Description

Serial Assistant

Here Insert Picture Description

Component Name

COMPIM

AT89C51(AT89C52)

Published 11 original articles · won praise 3 · Views 274

Guess you like

Origin blog.csdn.net/weixin_44871327/article/details/104647655