给你改写了一下程序,程序构架与你原程序变化较大,现仿真正常,做实物还需要再完善,主要是数码管位驱动要加晶体管放大。
999s倒计时+仿真.zip
(114.4 KB, 下载次数: 6)
- //头文件
- #include <reg51.h>
- #include <intrins.h>
- //宏定义
- #define uchar unsigned char
- #define uint unsigned int
- #define key_S 20 //宏定义短按(20ms)
- #define key_L 500 //宏定义长按(0.5s)
- #define key_M 300 //宏定义连按间隔(0.2s)
- //共阳极数码管段码
- uchar code SEG7[]={/*0,1,2,3,4,5,6,7,8,9,*/
- 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,};
- //数码管显示缓冲
- uchar num[]={1,2,3,4};
- //数码管位码端口
- /*
- sbit q1=P2^0;
- sbit q2=P2^1;
- sbit q3=P2^2;
- sbit q4=P2^3;
- */
- //复位开始停止加1减1按键
- sbit k1=P1^0;
- sbit k2=P1^1;
- sbit k3=P1^2;
- sbit k4=P1^3;
- sbit OUT=P3^7;
- sbit buzz=P3^5;
- uint Cnt1s=35,sec=0,count=0;
- char Cnt100ms=0;
- uchar Choice,Cnt1ms=0,Cnt250us=0;
- bit sign=0,buzzer=0;
- bit flag1=0,flag2;
- void Timer0Init()//250微秒@12.000MHz
- {
- TMOD |= 0x02; //设置定时器模式
- TL0 = 0x06; //设置定时初值
- TH0 = 0x06; //设置定时重载值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- }
- //显示控制
- void dispaly()
- {
- if(flag1==0)
- {
- num[0]=SEG7[sec/100%10];//显示十位
- num[1]=SEG7[sec/10%10]; //显示小数位
- num[2]=SEG7[sec%10]&0x7f;//显示个位加点
- num[3]=SEG7[Cnt100ms%10];//显示十分位
- }
- else
- {
- num[0]=SEG7[Cnt1s/100%10];//显示十位
- num[1]=SEG7[Cnt1s/10%10]; //显示小数位
- num[2]=SEG7[Cnt1s%10]&0x7f;//显示个位加点
- num[3]=0xbf;//显示-
- }
- }
- //数码管显示
- void dis()
- {
- static i=0;
- P0=0xff;
- switch(i)
- {
- case 0: P2=0xf1; break;
- case 1: P2=0xf2; break;
- case 2: P2=0xf4; break;
- case 3: P2=0xf8; break;
- }
- P0=num[i];
- i++;
- i%=4;
- }
- //按键扫描
- void key_scan()
- {
- static bit key1_sign,key4_sign; //按键自锁标志变量
- static uint count1,count2,count3=0,count4=0;//消抖计数变量
- if(k1==0 && Choice==0) //检测按键1按下与K4互锁
- {
- count1++; //消抖计数1自+1
- if((count1>=key_S)&&(key1_sign==0))//检测消抖计数与按键1自锁标志
- {
- key1_sign=1; //按键1自锁标志置1
- flag1=~flag1;
- if(flag1==0)
- sec=Cnt1s;
- }
- }
- else
- {
- key1_sign=0; //按键1自锁标志清0
- count1=0; //消抖计数count1清0
- }
- if(flag1) //调整时间
- {
- if(!k2) //检测按键2按下
- {
- count2++;
- if(count2>=key_L) //长按快调
- {
- if(Cnt1s<999)
- Cnt1s++;
- count2=key_M;
- }
- }
- else //按键抬起
- {
- if(count2>key_S && count2<key_L)//短按
- {
- if(Cnt1s<999)
- Cnt1s++;
- }
- count2=0; //count2清0
- }
- if(!k3) //检测按键3按下
- {
- count3++;
- if(count3>=key_L) //长按快调
- {
- if(Cnt1s>0)
- Cnt1s--;
- count3=key_M;
- }
- }
- else //按键抬起
- {
- if(count3>key_S && count3<key_L)//短按
- {
- if(Cnt1s>0)
- Cnt1s--;
- }
- count3=0; //count3清0
- }
- }
- if(k4==0 && flag1==0) //检测按键4按下与k1互锁
- {
- count4++; //消抖计数4自+1
- if((count4>=key_S)&&(key4_sign==0))//检测消抖计数与按键4自锁标志
- {
- key4_sign=1; //按键4自锁标志置1
- Choice++;
- if(Choice==1) //开始
- {
- OUT=0;
- Cnt1ms=0;
- Cnt100ms=0;
- }
- if(Choice==2) //停止
- OUT=1;
- if(Choice>=3) //复位
- {
- Choice=0;
- Cnt1ms=0;
- Cnt100ms=0;
- sec=Cnt1s;
- }
- }
- }
- else
- {
- key4_sign=0; //按键4自锁标志清0
- count4=0; //消抖计数count4清0
- }
- }
- //报警程序
- void Alarm()
- {
- if(sign==1 && Cnt100ms==0 && sec==0)
- {
- sign=0;
- OUT=1;
- Choice=0;
- buzzer=1;
- }
- if(buzzer)
- {
- buzz=~buzz;
- count++;
- if(count>=1000)
- {
- count=0;
- buzzer=0;
- buzz=1;
- sec=Cnt1s;
- }
- }
- }
- //-----------------------
- void main()
- {
- Timer0Init();
- sec=Cnt1s;
- OUT=1;
- buzz=1;
- while(1)
- {
- if(TF0==1)
- {
- TF0=0;
- Alarm();
- Cnt250us++;
- if(Cnt250us>=4)//1ms
- {
- Cnt250us=0;
- key_scan(); //按键扫描
- dis(); //显示刷新
- if(Choice==1)//启动倒计时
- {
- Cnt1ms++;
- if(Cnt1ms>=100)
- {
- Cnt1ms=0;
- Cnt100ms--;
- if(Cnt100ms<0)
- {
- Cnt100ms=9;
- sec--;
- if(sec==0)
- sign=1;
- }
- }
- }
- }
- }
- dispaly();//显示控制
- }
- }
复制代码
|