程序:#include<reg51.h>
sbit DIN=P3^0;
sbit LOAD=P3^1;
sbit CLK=P3^2;
sbit key0=P1^0;
sbit key1=P1^1;
sbit key2=P1^2;
sbit key3=P1^3;
sbit key4=P1^4;
sbit key5=P1^5;
sbit key6=P1^6;
sbit key7=P1^7;
sbit key_clear=P2^0;
sbit begin=P2^1;
sbit sounder=P3^7;
unsigned char second=30;
unsigned char counter=0;
unsigned char people=0;
unsigned char num_add[]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
unsigned char num_dat[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89};
unsigned char keyscan()
{
unsigned char keyvalue,temp;
keyvalue=0;
P1=0xff;
temp=P1;
if(~(P1&temp))
{
switch(temp)
{
case 0xfe:
keyvalue=1;
break;
case 0xfd:
keyvalue=2;
break;
case 0xfb:
keyvalue=3;
break;
case 0xf7:
keyvalue=4;
break;
case 0xef:
keyvalue=5;
break;
case 0xdf:
keyvalue=6;
break;
case 0xbf:
keyvalue=7;
break;
case 0x7f:
keyvalue=8;
break;
default:
keyvalue=0;
break;
}
}
return keyvalue;
}
void max7219_send(unsigned char add,unsigned char dat)
{
unsigned char ADS,i,j;
LOAD=0;
i=0;
while(i<16)
{
if(i<8)
{
ADS=add;
}
else
{
ADS=dat;
}
for(j=8;j>=1;j--)
{
DIN=ADS&0x80;
ADS=ADS<<1;
CLK=1;
CLK=0;
}
i=i+8;
}
LOAD=1;
}
void max7219_init()
{
max7219_send(0x0c,0x01);
max7219_send(0x0b,0x07);
max7219_send(0x0a,0xf5);
max7219_send(0x09,0xff);
}
void time_display(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x%10;
max7219_send(num_add[1],num_dat[j]);
max7219_send(num_add[0],num_dat[i]);
}
void scare_display(unsigned char x)
{
unsigned char i,j;
i=x/10;
j=x%10;
max7219_send(num_add[3],num_dat[j]);
max7219_send(num_add[2],num_dat[i]);
}
void holderscan()
{
time_display(second);
scare_display(people);
if(~key_clear)
{
while(~key_clear);
if(people)
{
second=30;
people=0;
}
if(second<60)
{
second++;
}
else
{
second=0;
}
}
}
void timer_init()
{
EA=1;
ET0=1;
TMOD=0x01;
TH0=0xd8;
TL0=0xef;
}
void main()
{
while(1);
{
do
{
holderscan();
}
while(begin);
while(~begin);
max7219_init();
timer_init();
TR0=1;
do
{
time_display(second);
scare_display(people);
people=keyscan();
}
while((!people)&&(second));
TR0=0;
}
}
void timer0()interrupt 1
{
if(counter<100)
{
counter++;
if(counter==50)
{
sounder=0;
}
}
else
{
sounder=1;
counter=0;
second=second-1;
}
TH0=0xd8;
TL0=0xef;
TR0=1;
} |