#include <reg51.h>
#define INT8U unsigned char
#define INT16U unsigned int
const INT8U SEG_CODE[]=
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
sbit BEEP=P3^0;
INT8U pre_keyNo=0xFF,keyNo=0xFF;
void delay_ms(INT16U x)
{
INT8U t;
while (x--)
for (t=0;t<120;t++);
}
void Keys_Scan()
{
P1=0x0F;
delay_ms(1);
if(P1==0x0F)
{
keyNo=0xFF;
return;
}
switch (P1)
{
case 0x0E:keyNo=0;break;
case 0x0D:keyNo=1;break;
case 0x0B:keyNo=2;break;
case 0x07:keyNo=3;break;
default:keyNo=0xFF;return;
}
P1=0xF0;
delay_ms(1);
switch (P1)
{
case 0xE0:keyNo+=0;break;
case 0xD0:keyNo+=4;break;
case 0xB0:keyNo+=8;break;
case 0x70:keyNo+=12;break;
default: keyNo=0xFF;
}
}
void Beep()
{
INT8U i;
for(i=0;i<100;i++)
{
delay_ms(1);
BEEP=~BEEP;
}
BEEP=i;
}
void main()
{
P0=0xFF;
while(1)
{
Keys_Scan();
if(keyNo==0xFF)
{
delay_ms(10);
continue;
}
P0=SEG_CODE[keyNo];
Beep();
while (Keys_Scan(),keyNo!=0xFF);
}
}