矩阵键盘控制0到F

矩阵键盘控制0到F

#include<reg52.h>

 
code unsigned char segcode[16] = { 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e };
 void delay(unsigned int t );
 unsigned char keyscan(void);
 void onedisplay(unsigned char p,unsigned char n);


void main()
{
unsigned char keycode;
while(1)
 {
  keycode = keyscan();


if(keycode !=0 )
onedisplay(7,keycode -1);
 }
}
 void delay(unsigned int t )
 {
  while( t -- );
 }
void onedisplay(unsigned char p,unsigned char n)
{
P0 = 0xff;
P2 = ~(0x01<<p);
P0 = segcode[n];
}
unsigned char keyscan(void)
{
unsigned char state;
unsigned char key = 0;


 //扫描第1行
P1 = 0xfe;
if( P1 != 0xfe )
{
    state = P1;


switch (state)
{
case 0xee : key = 1; break;
case 0xde : key = 2; break;
case 0xbe : key = 3; break;
case 0x7e : key = 4; break;
}
}
 //扫描第2行
P1 = 0xfd;
if( P1 != 0xfd )
{
    state = P1;


switch (state)
{
case 0xed : key = 5; break;
case 0xdd : key = 6; break;
case 0xbd : key = 7; break;
case 0x7d : key = 8; break;
}


   }
 //扫描第3行
P1 = 0xfb;
if( P1 != 0xfb )
{
    state = P1;


switch (state)
{
case 0xeb : key = 9; break;
case 0xdb : key = 10; break;
case 0xbb : key = 11; break;
case 0x7b : key = 12; break;
}


}
    //扫描第4行
P1 = 0xf7;
if( P1 != 0xf7 )
{
    state = P1;


switch (state)
{
case 0xe7 : key = 13; break;
case 0xd7 : key = 14; break;
case 0xb7 : key = 15; break;
case 0x77 : key = 16; break;
}
 }
return key;


}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值