■在蓝桥杯单片机综合训练平台上,利用定时器T0、数码管模块和2个独立按键(J5的2-3短接),设计一个秒表,具有清零、暂停、启动功能。
1.显示格式为:分-秒.0.05秒(即50ms)08-26-18表示: 8分26秒900毫秒
2.独立按键S4为:暂停/启动
独立按键S5为:清零
按键均为按下有效。
已完成仿真与调试。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include "reg52.h"
- sbit HC138_A = P2^5;
- sbit HC138_B = P2^6;
- sbit HC138_C = P2^7;
-
- sbit S5 = P3^2;
- sbit S4 = P3^3;
- //共阳4位数码管的段码数组
- unsigned char code SMG_NoDot[18] =
- {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
- 0xbf,0x7f};
-
- char minute=0;
- char second=0;
- char ms=0;
- void DisplaySMG_Dynamic();
- void Delay(unsigned int time);
- void DelaySMG(unsigned int time)
- {
- while(time--);
- }
- void Delay(unsigned int time)
- {
- while(time--);
- }
- void Init74HC138(unsigned char n)
- {
- switch(n)
- {
- case 4:
- HC138_A = 0;
- HC138_B = 0;
- HC138_C = 1;
- break;
- case 5:
- HC138_A = 1;
- HC138_B = 0;
- HC138_C = 1;
- break;
- case 6:
- HC138_A = 0;
- HC138_B = 1;
- HC138_C = 1;
- break;
- case 7:
- HC138_A = 1;
- HC138_B = 1;
- HC138_C = 1;
- break;
- case 8:
- HC138_A = 0;
- HC138_B = 0;
- HC138_C = 0;
- break;
- }
- }
- void DispaySMG_Bit(unsigned char value, unsigned char pos)
- {
- Init74HC138(7);
- P0 = 0xff;
- Init74HC138(6);
- P0 = (0x01 << pos);
- Init74HC138(7);
- P0 = value;
- }
- void DisplaySMG_Dynamic()
- {
- DispaySMG_Bit(SMG_NoDot[minute/10],0);
- DelaySMG(30);
- DispaySMG_Bit(SMG_NoDot[minute%10],1);
- DelaySMG(30);
- DispaySMG_Bit(SMG_NoDot[16],2);
- DelaySMG(30);
- DispaySMG_Bit(SMG_NoDot[second/10],3);
- DelaySMG(30);
- DispaySMG_Bit(SMG_NoDot[second%10],4);
- DelaySMG(30);
- DispaySMG_Bit(SMG_NoDot[16],5);
- DelaySMG(30);
-
- DispaySMG_Bit(SMG_NoDot[ms/10],6);
- DelaySMG(30);
- DispaySMG_Bit(SMG_NoDot[ms%10],7);
- DelaySMG(30);
- }
- void InitTimer0()
- {
- TMOD = 0X01;
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void ServiceTimer0() interrupt 1
- {
- TH0=(65535-50000)/256;
- TL0=(65535-50000)%256;
- ms++;
- if(ms ==20)
- {
- second++;
- ms=0;
- if(second==60)
- {
- minute++;
- second=0;
- }
- if(minute==60)
- {
- minute=0;
- }
-
- }
-
- }
- void ScanKeys()
- {
-
- if(S5 == 0)
- {
- Delay(100);
- if(S5 == 0)
- {
- minute=0;
- second=0;
- ms=0;
- while(S5 ==0)
- {
- DisplaySMG_Dynamic();
- }
- }
- }
- if(S4 == 0)
- {
- Delay(100);
- if(S4 == 0)
- {
- TR0=~TR0;
- while(S4 ==0)
- {
- DisplaySMG_Dynamic();
- }
-
- }
- }
- }
-
- main()
- {
- InitTimer0();
- while(1)
- {
- DisplaySMG_Dynamic();
- ScanKeys();
- }
- }
复制代码
Keil代码与Proteus7.5版本的仿真dsn文件下载(其他版本打开会出错):
11 定时器进阶-数字钟(更新后).7z
(320.89 KB, 下载次数: 13)
|