s1.c

来自「单片及c语言轻松入门的随书光盘」· C语言 代码 · 共 61 行

C
61
字号
#define uchar unsigned char
#include "string.h"
#include "reg51.h"
#include "stdio.h"
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 KValue;
	P3|=0x3e;	//中间4位置高电平
	if((KValue=P3|0xe3)!=0xff)
	{	mDelay(10);
		if((KValue=P3|0xe3)!=0xff)
		{	for(;;)
			if((P3|0xe3)==0xff)
				return(KValue);
		}
	}
	return(0);
}	

void main()
{	uchar KeyValue;
	uchar c;
	P1=0xff;	//关闭P1口接的所有灯
	TMOD=0x20;	//确定定时器工作模式
	TH1=0xFD;
	TL0=0xFD;	//定时初值
	PCON&=0x80;	//SMOD=1
	TR1=1;		//开启定时器1
	SCON=0x40;	//串口工作方式1
	REN=1;		//允许接收
	for(;;)
	{	if(KeyValue=Key())
		{	if((KeyValue|0xfb)!=0xff)	//K1按下
				SendData(0x55);
			if((KeyValue|0xf7)!=0xff)
				SendData(0xaa);
		}

		if(RI)
		{	c=SBUF;
			P1=c;
			RI=0;
		}
	}
}

⌨️ 快捷键说明

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