⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 mcs51单片机使用串口进行通信
💻
字号:
#define uchar unsigned char 

#include "string.h" 

#include "reg51.h" 
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_7=P1^7;
sbit P1_6=P1^6;
sbit P1_5=P1^5;
sbit P1_4=P1^4;
sbit P1_3=P1^3;
sbit P1_2=P1^2;

void SendData(uchar Dat) 

{ 	uchar i=0; 

	SBUF=Dat; 

	while(1){ 
		if(TI) 	
		{ 	 TI=0;
			 break;
		}
	}
 

} 

void mDelay(unsigned int DelayTime) 
{ unsigned char j=0; 

	for(;DelayTime>0;DelayTime--) 
	{ 
		for(j=0;j<125;j++) 
		{;} 	
	} 

} 

uchar Key() 
{ 		 
	uchar row,colum;
	P2 = 0x0f;
	if( P2 != 0x0f)
	{
		if(P2 == 0x07) row = 0;
		if(P2 == 0x0b) row = 1;
		if(P2 == 0x0d) row = 2;
		if(P2 == 0x0e) row = 3;

		P2 = 0xf0;
		if(P2 != 0xf0)
		{
			if(P2 == 0x70) colum = 0;
			if(P2 == 0xb0) colum = 1;
			if(P2 == 0xd0) colum = 2;
			if(P2 == 0xe0) colum = 3;
			
			P2 = 0x0f;
			while(P2 != 0x0f);
			return (row*4+colum);
		}
		else 
			return (0xff);
	}

	return(0xff); 
} 

void main() 
{ 
		uchar KeyValue; 
		uchar KeyTemp;
		uchar Get;

		TMOD=0x20; 		//确定定时器工作模式 
		TH1=0xe8; 
		TL1=0xe8; 		//定时初值 
		PCON&=0x80; 		//SMOD=1 
		TR1=1; 				//开启定时器 1
		SCON=0x40;		 //串口工作方式 1 
		REN=1; 				//允许接收 
		
		P1_5 = 0;
		P1_6 = 0;
		P1_7 = 0;
		P1_0 = 0;
		P1_1 = 0;
		P1_2 = 0;
		
		KeyTemp = 0xff;
		for(;;) 
		{ 
		
	//		KeyValue = Key();
			
	//		if(KeyValue != 0xff)
	//			KeyTemp = KeyValue;

			if(RI)
			{
				Get =  SBUF;

				if(Get == 0) {P1_0=1; P1_1=0; P1_2=0;}
			//	if(Get == 1) {P1_1=1; P1_0=0; P1_2=0;}
			//	if(Get == 2) {P1_2=1; P1_0=0; P1_1=0;}
				P1_1 = 1;
				if(Get == 4) 
				{
					P1_2=1;
					P1_7=1;
					mDelay(0x01);
					SendData(0x77);
					mDelay(0x01);
					P1_7=0;
				}
				
		//		KeyTemp = 0xff;
				RI=0;
			}	
		} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -