#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
sbit IO1=P1^0;
sbit IO2=P1^1;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,};
uchar keybuf,keydat,num;
bit bkeyok;
void delay_us(unsigned int n)
{
while(--n);
}
void delay_ms(unsigned int n)
{
do
{
delay_us(1000);
}
while(--n);
}
bit scan_key(void)
{
uchar tempkey;
IO1=1;IO2=1;
if(!IO1)
{
if (!IO2)
tempkey=3;
else
tempkey=2;
}
else
{
if (!IO2)
tempkey=1;
else
{
IO1=0;
if (!IO2)
{
IO1=1;IO2=0;
if (!IO1)
tempkey=4;
else
tempkey=6;
}
else
{
IO1=1;IO2=0;
if (!IO1)
tempkey=5;
else
tempkey=0;
}
}
}
IO1=1;IO2=1;
if((keybuf !=0) && (keybuf == tempkey))
{
bkeyok=1;
keydat=tempkey;
}
keybuf=tempkey;
return(bkeyok);
}
uchar read_key(void)
{
bkeyok=0;
return(keydat);
}
void doanything(uchar key)
{
P2=table[key];
P0=0xf0;
}
void main(void)
{
while(1)
{
if(scan_key())
doanything(read_key());
delay_ms(10);
}
}
/*我试了不行,请问有错误吗*/ |