单片机源程序如下:
- #include"reg52.h"
- sbitkey=P3^7;
- unsignedchar led_mod[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//字模
- unsignedint count=0,num1=0,num2=0;
- unsignedchar work=0,block=0,record=0,situation=1;
- voidT0_srv (void) interrupt 1 //定时器0,定时1秒,刷新秒表显示
- {
- TH0 =(65536-50000)/256;
- TL0 =(65536-50000)%256;
- count++;
- if(count==20)
- {count=0;
- if(num1==9&&num2==9) num1=num2=0;
- else{
- if(num1==9) num2++,num1=0;
- else num1++;
- }}
- }
- voidT1_srv (void) interrupt 3 //定时器1,定时200ms,用来判断短按和长按
- {
- TH1 =(65536-20000)/256;
- TL1 =(65536-20000)%256;
- record++;
- if(record==10)
- { record=0;
- if(key==0) situation=0;
- elsesituation=1,work=!work;
- block=1;
- }
- }
- voiddelay()
- {
- unsigned int i=2000;
- while(i--);
- }
- voidcheck()//按键检索函数
- {
- if(key==0){ //判断按键长短
- delay();
- if(key==0) {TR0=0,TR1=1;
- while(!block);
- TR1=0;TH1=(65536-50000)/256;TL1=(65536-50000)%256;block=0;//开启定时器1,计时200ms,以此判断长按和短按
- if(situation==0){while(!key);work=0;TR0=0,TH0=(65536-50000)/256;TL0=(65536-50000)%256;num1=num2=0;}//长按清零
- else{ //短按开始或者暂停秒表
- if(work==0) TR0=0;
- else TR0=1;}}}
- }
- voidmain(){
- TMOD=0x11; //定时器工作组1
- TH0=(65536-50000)/256; //定时器初值设定
- TL0=(65536-50000)%256;
- TH1=(65536-50000)/256;
- TL1 =(65536-50000)%256;
- EA=1;
- ET0=1;
- ET1=1;
- TR0=0;
- TR1=0;
- P0=led_mod[num1];
- P2=led_mod[num2];
- while(1)
- {
- check();//按键检索
- P0=led_mod[num1];delay();//死循环显示秒表计数
- P2=led_mod[num2];delay();
- }}
复制代码
|