#include "reg51.h"
unsigned char seg_mod[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //数码管字模
unsigned char time=0;
unsigned char sec=20;
unsigned char min=60;
sbit P3_0=P3^0; //定义位变量
sbit P3_1=P3^1;
sbit key1=P1^0; //清零
sbit key2=P1^1; //暂停
sbit key3=P1^2; //启动
unsigned char wei1;
unsigned char wei2;
void delay(unsigned int a) //误差 0us
{
unsigned int j;
for(;a>0;a--)
for(j=0;j<125;j++);
}
void timer0() interrupt 1 //T0中断函数
{
TH0=0x3c; //计数初值
TL0=0xb0;
if(++time==sec) //1s
{
time=0;
if(min==60) //60s循环
min=0;
wei1=min%10; //个位
wei2=min/10; //十位
min++;
}
}
void main()
{
TMOD=0x01; //T0方式一
TH0=0x3c;
TL0=0xb0;
TR0=0; //定时器打开
EA=1; //开中断
ET0=1;
TR0=1;
while(1)
{
if(key3==0) //启动
{
TR0=1;
}
if(key2==0) //暂停
{
TR0=0;
}
if(key1!=0) //清零
{
P3_1=1;
P2=seg_mod[wei2]; //个位显示
P3_0=0;
delay(5);
P3_0=1;
P2=seg_mod[wei1]; //十位显示
P3_1=0;
delay(5);
}
else
{
wei1=wei2=0;
P3_1=1;
P2=seg_mod[wei2];; //个位显示
P3_0=0;
delay(5);
P3_0=1;
P2=seg_mod[wei1]; //十位显示
P3_1=0;
delay(5);
}
}
}
|