|
基于单片机的秒表设计
- /****************************/
- /* 题2-13 C语言程序 */
- /****************************/
- #include <reg51.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit s1=P3^1;
- sbit wei1=P2^0;
- sbit wei2=P2^1;
- uchar code table[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- uchar shi,ge;
- uint count,second,keycount;
- void init();
- void key();
- void delayms(uint z);
- void display(uchar shi,uchar ge);
- void main()
- {
- init();
- while(1)
- {
- key();
- display(shi,ge);
- }
- }
- void init()
- {
- TMOD=0x10;
- TH1=(65536-20000)/256;
- TL1=(65536-20000)%256;
- TR1=0;
- EA=1;
- ET1=1;
- }
- void delayms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void key()
- {
- if(s1==0)
- {
- delayms(5);
- if(s1==0)
- {
- while(!s1);
- keycount++;
- switch(keycount)
- {
- case 1: TR1=1;break;
- case 2: TR1=0;break;
- case 3:
- {
- keycount=0;
- count=0;
- second=0;
- shi=second/10;
- ge=second%10;
- display(shi,ge);
- TR1=0;
- }break;
- }
- }
- }
- }
- void display(uchar shi,uchar ge)
- {
- P0=table[shi];
- wei1=0;
- delayms(1);
- wei1=1;
- P0=table[ge];
- wei2=0;
- delayms(1);
- wei2=1;
- }
- void timer1() interrupt 3
- {
- TH1=(65536-20000)/256;
- TL1=(65536-20000)%256;
- count++;
- if(count==50)
- {
- count=0;
- second++;
- if(second==11)second=0;
- }
- shi=second/10;
- ge=second%10;
- }
复制代码
|
评分
-
查看全部评分
|