下面是我的程序:
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
uchar keysign;
ulong key;
uchar checkok;
ulong dat;
ulong count;
uchar temH;
uchar temL;
unsigned char table[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
void delay(uint z) //1ms的延时函数
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan(void)
{
P1 = 0xf0;
if(P1!=0xf0)
{
delay(10);
temH = P1;//低四位先输出0;读入,高四位含有按键信息
P1 = 0x0f;
temL = P1;//然后反转输出0;读入,低四位含有按键信息
keysign=1; } }
//-----------------------------------------------------
//-----------------------------------------------------
void checkkey(void)
{
if(keysign){
switch(temH) {
case 0xe0: key = 0; break;
case 0xd0: key = 1; break;
case 0xb0: key = 2; break;
case 0x70: key = 3; break;
default: key=16;//按下的不是上述按键,就当是没有按键
}
switch(temL) {
case 0x0e: key=key;
case 0x0d: key=key + 4;
case 0x0b: key=key + 8;
case 0x07: key=key + 12;
default: key=16;//按下的不是上述按键,就当是没有按键
} keysign=0; checkok=1; }
}
void chuli(void)
{
if(checkok)
{dat=dat*10+key;
checkok=0;}
}
void display(void)
{
uint ge,shi,bai,qian;
qian=dat/1000;
P2=0xf1;
P0=table[qian];
delay(10);
bai=dat%1000/100;
P2=0xf2;
P0=table[bai];
delay(10);
shi=dat%100/10;
P2=0xf4;
P0=table[shi];
delay(10);
ge=dat%10;
P2=0xf8;
P0=table[ge];
delay(10);
}
void main(void)
{
while(1)
{keyscan();
checkkey();
chuli();
display();}
}
一周热门 更多>