#include <reg52.h>
sbit ADDR2 = P1^2;
sbit ADDR1 = P1^1;
sbit ADDR0 = P1^0;
sbit ENLED = P1^4;
sbit ADDR3 = P1^3;
sbit BEEP = P1^6;
sbit KEY13 = P2^4;
sbit KEY14 = P2^5;
sbit KEY15 = P2^6;
#define TIMES 300 //此例程的主循环里循环一次所要花费的时间比以往的例程要多,所以按键的times判断需要减少到300,不然还是500的话需要我们长按住一段时间才会响应功能代码
unsigned
char
code LedChar[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
unsigned
char
LedBuff[6]={0xFF, 0xFF, 0xBF, 0xBF, 0xFF, 0xFF};
unsigned
char
man=30,woman=30;
void
UPDATE_LED()
{
LedBuff[5]=LedChar[man/10];
LedBuff[4]=LedChar[man%10];
LedBuff[1]=LedChar[woman/10];
LedBuff[0]=LedChar[woman%10];
}
void
SEG_task()
{
static
unsigned
char
i=0;
P0=0xFF;
switch
(i)
{
case
0:
ADDR2 = 0;ADDR1 = 0;ADDR0 = 0;P0=LedBuff[0];i++;
break
;
case
1:
ADDR2 = 0;ADDR1 = 0;ADDR0 = 1;P0=LedBuff[1];i++;
break
;
case
2:
ADDR2 = 0;ADDR1 = 1;ADDR0 = 0;P0=LedBuff[2];i++;
break
;
case
3:
ADDR2 = 0;ADDR1 = 1;ADDR0 = 1;P0=LedBuff[3];i++;
break
;
case
4:
ADDR2 = 1;ADDR1 = 0;ADDR0 = 0;P0=LedBuff[4];i++;
break
;
case
5:
ADDR2 = 1;ADDR1 = 0;ADDR0 = 1;P0=LedBuff[5];i=0;
break
;
}
}
void
KEY13_task()
{
static
unsigned
char
key_up=1;
static
unsigned
int
times=0;
if
(key_up==0)
{
times++;
if
(times>=TIMES&&KEY13==1)
{
times=0;
man+=3;
woman-=3;
UPDATE_LED();
}
}
key_up=KEY13;
}
void
KEY14_task()
{
static
unsigned
char
key_up=1;
static
unsigned
int
times=0;
if
(key_up==0)
{
times++;
if
(times>=TIMES&&KEY14==1)
{
times=0;
man+=1;
woman-=1;
UPDATE_LED();
}
}
key_up=KEY14;
}
void
KEY15_task()
{
static
unsigned
char
key_up=1;
static
unsigned
int
times=0;
if
(key_up==0)
{
times++;
if
(times>=TIMES&&KEY15==1)
{
times=0;
man-=2;
woman+=2;
UPDATE_LED();
}
}
key_up=KEY15;
}
void
BEEP_ON(unsigned
char
x)
{
unsigned
int
i,
time
;
for
(
time
=0;
time
<2000;
time
++)
{
if
(x==1)BEEP=!BEEP;
else
BEEP=0;
for
(i=0;i<30;i++);
}
}
void
main()
{
unsigned
int
i,x;
ADDR3 = 1;
ENLED = 0;
P2 = 0xFE;
UPDATE_LED();
while
(1)
{
SEG_task();
KEY13_task();
KEY14_task();
KEY15_task();
if
(man>=45 || woman>=45)
{
if
(man>=45){LedBuff[1]=0xFF; LedBuff[0]=0xFF;}
else
{LedBuff[5]=0xFF; LedBuff[4]=0xFF;}
while
(1)
{
P0=0xFF;
BEEP_ON(1);
for
(i=0;i<300;i++)
{
SEG_task();
for
(x=0;x<200;x++);
}
}
}
}
}