📄 key.c
字号:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit a=P1^0;
sbit b=P1^1;
sbit c=P1^2;
const uchar table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
void delay(uint k)
{
uint i;
for(;k>0;k--)
for(i=110;i>0;i--);
}
void HC138(uchar i)
{
a=i&0x01;
b=i&0x02;
c=i&0x04;
}
void display(uchar num)
{
uchar ge,shi;
shi=num/10;
ge=num%10;
HC138(0);
P2=table[ge];
delay(3);
HC138(1);
P2=table[shi];
delay(3);
}
uchar keyscan()
{
uchar num,temp;
P0=0xfe;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xee:num=0;break;
case 0xde:num=1;break;
case 0xbe:num=2;break;
case 0x7e:num=3;break;
}
while(P0!=0xf0)
P0&=0xf0;
}
}
P0=0xfd;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xed:num=4;break;
case 0xdd:num=5;break;
case 0xbd:num=6;break;
case 0x7d:num=7;break;
}
while(P0!=0xf0)
P0&=0xf0;
}
}
P0=0xfb;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xeb:num=8;break;
case 0xdb:num=9;break;
case 0xbb:num=10;break;
case 0x7b:num=11;break;
}
while(P0!=0xf0)
P0&=0xf0;
}
}
P0=0xf7;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P0;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xe7:num=12;break;
case 0xd7:num=13;break;
case 0xb7:num=14;break;
case 0x77:num=15;break;
}
while(P0!=0xf0)
P0&=0xf0;
}
}
return num;
}
void main()
{
uchar key=0;
while(1)
{
key=keyscan();
display(key);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -