C51--矩阵键盘--查询--中断--方式

/*
//查询方式4X4键盘,
#include <reg51.h>;

//声明类型
typedef unsigned char uint8;
typedef unsigned short uint16;

//声明各个端口
sbit row1=P0^0;
sbit row2=P0^1;
sbit row3=P0^2;
sbit row4=P0^3;
sbit col1=P0^4;
sbit col2=P0^5;
sbit col3=P0^6;
sbit col4=P0^7;

//显示的字符数组
unsigned char code DispCode[]=
{
	0xc0,0xF9,0xA4,0xB0,0x99,             //0-4
    0x92,0x82,0xF8,0x80,0x90,              //5-9
    0x88,0x83,0xC6,0xA1,0x86,             //A,b,C,d,E,F
    0x8E
};

//延时函数,消除抖动
void Delay(uint16 count)
{
	uint8 i;                
  	while(--count != 0)
    	for(i = 0; i < 125; i++);           
}

//主函数入口
void main()
{
	unsigned char key=0;	
	while(1)
	{
		//初始化各行为低电平
		row1=0;row2=0;row3=0;row4=0;
		col1=1;col2=1;col3=1;col4=1;
		
		//判断是否有列值不为高电压
		if( col1!=1 || col2!=1 || col3!=1 || col4!=1)
		{
			Delay(10);//消除抖动
			if( col1!=1 || col2!=1 || col3!=1 || col4!=1)
			{
				//行循环扫描,判定为哪行哪列被按下,并输出相应的字符
				
				row1=0;row2=1;row3=1;row4=1;
				if(col1==0){key=0;P1=DispCode[key];break;}
				else if(col2==0){key=1;P1=DispCode[key];break;}
				else if(col3==0){key=2;P1=DispCode[key];break;}
				else if(col4==0){key=3;P1=DispCode[key];break;}
				
				row1=1;row2=0;row3=1;row4=1;
				if(col1==0){key=4;P1=DispCode[key];break;}
				else if(col2==0){key=5;P1=DispCode[key];break;}
				else if(col3==0){key=6;P1=DispCode[key];break;}
				else if(col4==0){key=7;P1=DispCode[key];break;}

				row1=1;row2=1;row3=0;row4=1;
				if(col1==0){key=8;P1=DispCode[key];break;}
				else if(col2==0){key=9;P1=DispCode[key];break;}
				else if(col3==0){key=10;P1=DispCode[key];break;}
				else if(col4==0){key=11;P1=DispCode[key];break;}

				row1=1;row2=1;row3=1;row4=0;
				if(col1==0){key=12;P1=DispCode[key];break;}
				else if(col2==0){key=13;P1=DispCode[key];break;}
				else if(col3==0){key=14;P1=DispCode[key];break;}
				else if(col4==0){key=15;P1=DispCode[key];break;}
					
			}
		}
	}
}
*/


//矩阵键盘4X4,中断方式,外部中断0
#include <reg51.h>
sbit p3_2=P3^2;
//声明类型
typedef unsigned char uint8;
typedef unsigned short uint16;
unsigned char key=0;
//声明各个端口
sbit row1=P0^0;
sbit row2=P0^1;
sbit row3=P0^2;
sbit row4=P0^3;
sbit col1=P0^4;
sbit col2=P0^5;
sbit col3=P0^6;
sbit col4=P0^7;

//显示的字符数组
unsigned char code DispCode[]=
{
	0xc0,0xF9,0xA4,0xB0,0x99,             //0-4
    0x92,0x82,0xF8,0x80,0x90,              //5-9
    0x88,0x83,0xC6,0xA1,0x86,             //A,b,C,d,E,F
    0x8E
};

//延时函数,消除抖动
void Delay(uint16 count)
{
	uint8 i;                
  	while(--count != 0)
    	for(i = 0; i < 125; i++);           
}

//主函数入口
void main()
{
	EA=1;
	EX0=1;
	IT0=1;
	while(1)
	{
		//初始化各行为低电压
		row1=0;row2=0;row3=0;row4=0;
		col1=1;col2=1;col3=1;col4=1;
	}
}

//中断函数
void int0_ISR(void) interrupt 0
{
		if(p3_2==0)
		{
			Delay(10);//消除抖动
			if(p3_2==0)
			{
				//行循环扫描,判定为哪行哪列被按下,并输出相应的字符

				row1=0;row2=1;row3=1;row4=1;
				if(col1==0){key=0;P1=DispCode[key];return;}
				else if(col2==0){key=1;P1=DispCode[key];return;}
				else if(col3==0){key=2;P1=DispCode[key];return;}
				else if(col4==0){key=3;P1=DispCode[key];return;}
				
				row1=1;row2=0;row3=1;row4=1;
				if(col1==0){key=4;P1=DispCode[key];return;}
				else if(col2==0){key=5;P1=DispCode[key];return;}
				else if(col3==0){key=6;P1=DispCode[key];return;}
				else if(col4==0){key=7;P1=DispCode[key];return;}

				row1=1;row2=1;row3=0;row4=1;
				if(col1==0){key=8;P1=DispCode[key];return;}
				else if(col2==0){key=9;P1=DispCode[key];return;}
				else if(col3==0){key=10;P1=DispCode[key];return;}
				else if(col4==0){key=11;P1=DispCode[key];return;}

				row1=1;row2=1;row3=1;row4=0;
				if(col1==0){key=12;P1=DispCode[key];return;}
				else if(col2==0){key=13;P1=DispCode[key];return;}
				else if(col3==0){key=14;P1=DispCode[key];return;}
				else if(col4==0){key=15;P1=DispCode[key];return;}
					
			}
		}
}
 






  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值