C51矩陣鍵盤掃描程式

c979170768發表於2011-11-12
 
#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++);
}

相關文章