|
100黑币
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP = P3^0;
sbit K1 = P3^7;
uchar code DSY_CODE[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e
};
uchar Pre_KeyNO = 16,KeyNO = 16;
void DelayMS(uint ms)//延时函数
{
uchar t;
while(ms--)
{
for(t=0;t<120;t++);
}
}
void Keys_Scan()
{
uchar Tmp;
P1 = 0x0f;
DelayMS(1); //利用短延时来消抖
Tmp = P1 ^ 0x0f;
switch(Tmp)
{
case 1: KeyNO = 0; break;//列检测
case 2: KeyNO = 1; break;
case 4: KeyNO = 2; break;
case 8: KeyNO = 3; break;
default: KeyNO = 16;
}
P1 = 0xf0;
DelayMS(1);
Tmp = P1 >> 4 ^ 0x0f;//向后移4位,求出P1的值
switch(Tmp)
{
case 1: KeyNO += 0; break;//行检测
case 2: KeyNO += 4; break;
case 4: KeyNO += 8; break;
case 8: KeyNO += 12;
}
}
void Beep()
{
uchar i;
for(i=0;i<50;i++)
{
DelayMS(1);
BEEP = ~BEEP;
}
BEEP = 0;
}
void main()
{
P0 = 0x00;
P2 = 0x00;
while(1)
{
P1 = 0xf0;
if(P1 != 0xf0)
{
Keys_Scan();
P0 = 0x3f;//0
P2 = ~DSY_CODE[KeyNO];//0~f
Beep();
DelayMS(100);
}
if(K1==0)
{
while(K1==0);
while(1)
{
P1 = 0xf0;
if(P1 != 0xf0)
if(KeyNO<10)
{
Keys_Scan();
P0 = 0x3f;
P2 = ~DSY_CODE[KeyNO];
Beep();
DelayMS(100);
}
else
{
Keys_Scan();
P0 = 0x06;//1
P2 = ~DSY_CODE[KeyNO-10];//0~6
Beep();
DelayMS(100);
}
if(K1==0)
{
while(K1==0);
break;
}
}
}
}
}
|
|