C51矩阵键盘扫描程序

 
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
sfr KeyPort=0x90;		  //#define KeyPort P1 -----行在P1口低四位
//sbit Key_C1=P1^4;         //第1列接P1.4
//sbit Key_C2=P1^5;         //第2列接P1.5
//sbit Key_C3=P1^6;         //第3列接P1.6
sfr Led=0x80;			  //#define Led P0--------P0口LED
void delay(uchar ms);
void main()
{   uchar totle=0;
	Led=0xff;
	while(1)
	{
		KeyPort=0x6f;  		//0110 1111
		if(KeyPort!=0x6f)
		{
			delay(10);
			if(KeyPort!=0x6f)
			{
				switch(KeyPort)
				{
					case 0x6e:  //0110 1110
							//Led=_crol_(Led,1);
							//	Led=1;
							totle=totle+0x01;
							Led=totle;
							break;
					case 0x6d:  //0110 1101
							//Led=_crol_(Led,4);
							//Led=0x4;
							totle=totle+0x4;
							Led=totle;
							break;
					case 0x6b:  //0110 1011
							//Led=_crol_(Led,7);
							//Led=0x7;
							totle=+totle+0x7;
							Led=totle;
							break;	
					case 0x67:  //0110 0111
							//Led=_crol_(Led,10);
							totle=0x00;
							Led=totle;
							break;
				}
				while(KeyPort!=0x6f);
			}
		}
		
		KeyPort=0x5f;  //0101 1111
		if(KeyPort!=0x5f)
		{
			delay(10);
			if(KeyPort!=0x5f)
			{
				switch(KeyPort)
				{
					case 0x5e:  //0101 1110
							//Led=_crol_(Led,2);
							//Led=0x2;
							totle=totle+0x2;
							Led=totle;
							break;
					case 0x5d:  //0101 1101
							//Led=_crol_(Led,5);
							//Led=0x5;
							totle=totle+0x5;
							Led=totle;
							break;
					case 0x5b:  //0101 1011
							//Led=_crol_(Led,8);
							//Led=0x8;
							totle=totle+0x8;
							Led=totle;
							break;	
				 	case 0x57:  //0101 0111
						//	Led=_crol_(Led,11);
							totle=totle+0x0;
							Led=totle;
						 	break;
				}
				while(KeyPort!=0x5f);
			}
		}	

		KeyPort=0x3f;  		//0011 1111
		if(KeyPort!=0x3f)
		{
			delay(10);
			if(KeyPort!=0x3f)
			{
				switch(KeyPort)
				{
					case 0x3e:  //0011 1110
							//Led=_crol_(Led,3);
							//Led=0x3;
							totle=totle+0x3;
							Led=totle;
							break;
					case 0x3d:  //0011 1101
							//Led=_crol_(Led,6);
							totle=totle+0x6;
							Led=totle;
							break;
					case 0x3b:  //0011 1011
							//Led=_crol_(Led,9);
						//	Led=0x9;
							totle=totle+0x9;
							Led=totle;
							break;	
				//	case 0x37:  //0011 0111
						//	Led=_crol_(Led,12);
						//	break;
				}
				while(KeyPort!=0x3f);
			}
		}
	}
}
void delay(uchar ms)
{
	uchar i;
	for(;ms>0;ms--)
		for(i=0;i<244;i++);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值