首页 > 其他分享 >12-串口通信进阶

12-串口通信进阶

时间:2022-12-31 10:11:39浏览次数:43  
标签:P2 case P0 12 进阶 void unsigned command 串口

#include "reg52.h"

sfr AUXR = 0x8e;

void delay(unsigned char t)
{
	while(t--);
}

void SelectHC573(unsigned char n)
{
	switch(n)
	{
		case 4:
			P2 = (P2&0x1f)|0x80;
		break;
		
		case 5:
			P2 = (P2&0x1f)|0xa0;
		break;
		case 6:
			P2 = (P2&0x1f)|0xc0;
		break;
		case 7:
			P2 = (P2&0x1f)|0xe0;
		break;
		
	}
}

void InitSystem()
{
	SelectHC573(5);
	P0 = 0x00;
	SelectHC573(4);
	P0 = 0xff;
}


void InitUart()
{
	TMOD = 0x20;
	TH1 = 0xfd;
	TL1 = 0xfd;
	TR1 = 1;
	
	SCON = 0x50;
	AUXR = 0x00;
	ES = 1;
	EA = 1;
}

void SendByte(unsigned char dat)//发送
{
	SBUF = dat;
	while(TI == 0);
	TI = 0;
}

void SendString(unsigned char *str)
{
	while(*str != '\0')
	{
		SendByte(*str++);
	}
}


unsigned char command = 0x00;
void ServiceUart() interrupt 4 //接收
{
	if(RI == 1)
	{
		RI = 0;
		command = SBUF;
	}
	
}
//=======================
void Working()
{
	if(command != 0x00)
	{
		switch(command & 0xf0)
		{
			case 0xa0:
				P0 = (P0 | 0x0f) & (~command | 0xf0);
				command = 0x00;
			break;
			
			case 0xb0:
				P0 = (P0 | 0xf0) & ((~command <<4)| 0x0f);
				command = 0x00;
			break;
			
			case 0xc0:
				SendString("The System is Running...\r\n");
				command = 0x00;
			break;
			
		}
	}
}


void main()
{
	InitSystem();
	InitUart();
	delay(100);
	SendString("welcome to sk world!\r\n");
	while(1)
	{
		Working();
	}
}

 

标签:P2,case,P0,12,进阶,void,unsigned,command,串口
From: https://www.cnblogs.com/Unclesundada/p/17016258.html

相关文章