#include<reg52.h>
#include<intrins.h>
unsigned char code duan[ ] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int sec=0;
int num=0;
sbit s7=P3^0;
sbit s6=P3^1;
sbit s5=P3^2;
sbit s4=P3^3;
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void display()
{
//消影、位选、段选
P2 = 0x00;P0 = 0x00;
P2 = 0xc0;P0 = 0x01;
P2 = 0xe0;P0 = duan[sec/10];
Delay1ms();
P2 = 0x00;P0 = 0x00;
P2 = 0xc0;P0 = 0x02;
P2 = 0xe0;P0 = duan[sec%10];
Delay1ms();
}
void main()
{
//关掉流水灯、蜂鸣器
P2 = 0x80;P0 = 0xff;
P2 = 0xa0;P0 = 0x00;
//赋初值
TMOD = 0x01;
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
EA = 1;
ET0 = 1;
TR0 = 0;
while(1){
display();
if(sec == 60){
sec = 0;
}
if(sec == 0){
sec = 59;
}
if(num == 100){
num = 0;
sec++;
}
//按s7 +1
if(!s7){
Delay10ms();
if(!s7){
while(!s7);
sec++;
}
}
//按s6 -1
if(!s6){
Delay10ms();
if(!s6){
while(!s6);
sec--;
}
}
/按/s5 清0
if(!s5){
Delay10ms();
if(!s5){
while(!s5);
sec = 0;
}
}
//按s4自动+1 再按暂停
if(!s4){
Delay10ms();
if(!s4){
while(!s4);
TR0 = ~TR0;
}
}
}
}
void time0() interrupt 1
{
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
num++;
}