完全下图做时间电路和程序,共阳数码管,可以实现楼主想要的功能。 A、B 键互锁,不需要消抖,用闪烁表示换显示位,否则相邻数字相同无法分辨。小数点前为妙,小数点后为毫秒。
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit A1=P3^4;
- sbit A2=P3^5;
- uint m=0;
- uint count,t;
- uchar code table[]={//共阳数码管"0~f .-"
- 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf,0x7f};
- uchar cnt[6]={0xc0,0xc0,0xc0,0xc0,0xc0,0xc0};//小数缓存
- bit flag=0;
- bit sign=0;
- bit init=0;
- void start()
- {
- TMOD=0x01; //T0 使用工作方式 1
- TH0=(65536-1000)/256; //1ms 定时
- TL0=(65536-1000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void display() //显示子程序,从第一位开始逐位显示't 00秒 000毫秒'
- {
- static uint j=0;
- if(sign)
- P0=0xbf;//等待计数时显示'-'
- else
- {
- if(j<=900)
- P0=cnt[m];
- else if(init==1)
- P0=0xff;//闪烁表示换位显示
- j++;
- if(j>=1000)
- {
- j=0;
- m++;
- m%=6;
- }
- }
- }
- void main(void)
- {
- start();
- P0=0xc0;//初始显示0
- while(1)
- {
- if(A1==0 && flag==0) //按键扫描
- {
- flag=1;
- count=0;
- t=0;
- sign=1;
- init=1;
- }
- if(A2==0 && flag==1)
- {
- sign=0;
- flag=0;
- cnt[0]=0x87;
- cnt[1]=table[t/10];
- cnt[2]=table[t%10]&0x7f;//加秒点
- cnt[3]=table[count/100];
- cnt[4]=table[count/10%10];
- cnt[5]=table[count%10];
- m=0;
- }
- }
- }
- void time0(void) interrupt 1 //T0 中断服务子程序
- {
- TH0=(65536-1000)/256;
- TL0=(65536-1000)%256;
- if(sign)
- {
- count++;
- if(count>=1000)
- {
- count=0;
- t++;
- if(t>=60)
- {
- t=0;
- }
- }
- }
- display();
- }
复制代码
|