简单秒表计时器仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit key1=P3^0;
- sbit key2=P3^1;
- sbit key3=P3^2;
- uchar temp,aa,shi,ge;
- uchar code table[]={ 0x3f,0x06,0x5b,0x4f, 0x66,0x6d,0x7d,0x07, 0x7f,0x6f,0x77,0x7c, 0x39,0x5e,0x79,0x71};
- void display(uchar shi,uchar ge);
- void delay(uint z);
- void init();
- void main() {
- init();
- while(1) {
- if(key1==0)
- { delay(10);
- if(key1==0)
- {
- while(!key1);
- TR0=1;
- }
- }
- if(key2==0)
- {
- delay(10);
- if(key2==0)
- {
- while(!key2);
- TR0=0;
- }
- }
- if(key3==0)
- {
- delay(10);
- if(key3==0)
- {
- while(!key3);
- temp=0;
- shi=0;
- ge=0;
- TR0=0;
- }
- }
- display(shi,ge);
- }
- }
- void delay(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void display(uchar shi,uchar ge)
- {
- P2=0xbf;
- P0=table[shi];
- delay(10);
- P2=0x7f;
- P0=table[ge];
- delay(10);
- }
- void init()
- {
- temp=0;
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- }
- void timer0() interrupt 1 {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- aa++;
- if(aa==20)
- {
- aa=0;
- temp++;
- if(temp==60)
- {
- temp=0;
- }
- shi=temp%100/10;
- ge=temp%10;
- }
-
- }
复制代码
所有资料51hei提供下载:
秒表.rar
(32.41 KB, 下载次数: 9)
|