51单片机数码管仿真(矩阵按键)

在这里插入图片描述

#include<reg52.h>
#define uchar unsigned char
int key,temp;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay(unsigned int xms)
{	
	unsigned int i,j;	
	for(i=xms;i>0;i--)	   //1=xms即延时约xms毫秒	
	    for(j=112;j>0;j--);
}
void main()
{	
P2 = 0Xf0;	
temp = P2;	
temp = temp &0Xf0;	//1110 0000	//1111 0000	//1110 0000	
	if(temp!=0xf0)	 //有按键被按下	
	{		delay(10);		
		if(temp!=0xf0)		
		{			temp = P2;			
			temp =temp|0x0f; //0000 1111		//1110 0000		//0000 1111		//1110 1111			
			P2 = temp;	  //1110 1011 = 0xeb			
			key = P2;			
			switch(key)			
			{				
				case 0xee:					
				P1 = table[0];				
				break;					
				case 0xde:					
				P1 = table[1];				
				break;					
				case 0xbe:					
				P1 = table[2];				
				break;					
				case 0x7e:					
				P1 = table[3];				
				break;					
				case 0xed:					
				P1 = table[4];				
				break;					
				case 0xdd:					
				P1 = table[5];				
				break;					
				case 0xbd:					
				P1 = table[6];				
				break;					
				case 0x7d:					
				P1 = table[7];				
				break;					
				case 0xeb:					
				P1 = table[8];				
				break;					
				case 0xdb:					
				P1 = table[9];				
				break;					
				case 0xbb:					
				P1 = table[10];				
				break;					
				case 0x7b:					
				P1 = table[11];				
				break;					
				case 0xe7:					
				P1 = table[12];				
				break;					
				case 0xd7:					
				P1 = table[13];				
				break;					
				case 0xb7:					
				P1 = table[14];				
				break;					
				case 0x77:					
				P1 = table[15];				
				break;			
				}		
			}	
		} 
	}
发布了31 篇原创文章 · 获赞 28 · 访问量 9515

猜你喜欢

转载自blog.csdn.net/visual_eagle/article/details/102794299