单片机:手动设定倒计时时间,0报警
首先显示”时.分“,K1时增加,K2分增加(如果不修改时分默认5分钟:300秒倒计时)
K3开始倒计时,显示秒倒计时,到时报警BEEP
K4停止报警,并返回到”时分“状态,又可以设置倒计时时间,来回循环
#include<reg51.h> #define uchar unsigned char; #define uint unsigned int; uchar position; uchar tt,bz=0,bza=1; uint second; uchar minute; uchar hour; uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9 sbit smg_q = P2^0; sbit smg_b = P2^1; sbit smg_s = P2^2; sbit smg_g = P2^3; sbit key1=P3^2; sbit key2=P3^3; sbit key3=P3^4; sbit key4=P3^5; sbit led1=P1^0; sbit led2=P1^1; sbit led3=P1^2; sbit led4=P1^3; sbit led5=P1^4; sbit led6=P1^5; sbit beep=P2^5; void keyscan(); void display(unsigned char hour,unsigned char minute); void displaym(unsigned int second); void delay(unsigned int timer); void init(); void main() { init(); while(1) { if(tt==20) { led1=~led1;led2=~led2; tt=0; if(second==0 && bz==1) { while (bza==1) { beep=0; delay(100); beep=1; break; } } else second--; } keyscan(); if (bz==0) display(hour,minute); else displaym(second); delay(1); } } void keyscan() { if(key1==0) { TR0=0; hour++; if(hour==24) hour=0; delay(200) ; } if(key2==0) {TR0=0; minute++; if(minute==60) minute=0; delay(200) ; } if(key3==0) { bz=1; bza=1; TR0=0; second=hour*3600+minute*60; if (second==0) second=300; delay(200) ; TR0=1; } if(key4==0) { beep=1; bz=0; bza=0; minute=0; TR0=0; led1=1; led2=1; second=0; hour=0; minute=0; } } void init() { tt=0; bza=1; position=0; second=0; minute=0; hour=0; smg_q=1; smg_b=1; smg_s=1; smg_g=1; key1=1; key2=1; TMOD=0X01; TH0=0x4c; TL0=0x00; EA=1; ET0=1; TR0=0; } void t0() interrupt 1 { TH0=0x4c; TL0=0x00; tt++; } void display(unsigned char hour,unsigned char minute) { P0=0XFF; switch(position) { case 0: smg_g=1;smg_q=0;P0=table[hour/10]; break; case 1: smg_q=1;smg_b=0; P0=table[hour%10];if(tt>=10) P0&=0x7f; break; case 2: smg_b=1;smg_s=0;P0=table[minute/10];break; case 3: smg_s=1;smg_g=0;P0=table[minute%10];break; } position++; if(position>3) position=0; } void displaym(unsigned int second) { P0=0XFF; switch(position) { case 0: smg_g=1;smg_q=0;P0=table[second/1000]; break; case 1: smg_q=1;smg_b=0; P0=table[second/100%10]; break; case 2: smg_b=1;smg_s=0;P0=table[second/10%10];break; case 3: smg_s=1;smg_g=0;P0=table[second%10];if(tt>=10) P0&=0x7f;break; } position++; if(position>3) position=0; } void delay(unsigned int time) { unsigned int x,y; for(x=time;x>0;x--) for(y=110;y>0;y--); }
更新:显示四位以上的秒时:显示最后四位的秒数,第一位数码管右侧的点亮。如果不亮,则表明四位数码管能完整显示倒计时秒数
如下是更新的代码
#include<reg51.h> #include <math.h> #include <stdio.h> #define uchar unsigned char; #define uint unsigned int; uchar position; uchar tt,bz=0,bza=1,bk; uint second,ws,i=0,ws1; uchar minute; uchar hour; char cc[10]; uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9 sbit smg_q = P2^0; sbit smg_b = P2^1; sbit smg_s = P2^2; sbit smg_g = P2^3; sbit key1=P3^2; sbit key2=P3^3; sbit key3=P3^4; sbit key4=P3^5; sbit led1=P1^0; sbit led2=P1^1; sbit led3=P1^2; sbit led4=P1^3; sbit led5=P1^4; sbit led6=P1^5; sbit beep=P2^5; sbit dian=P0^7; unsigned int jcws(unsigned int a); void keyscan(); void display(unsigned char hour,unsigned char minute); void displaym(unsigned int second); void delay(unsigned int timer); void init(); void main() { init(); while(1) { if(tt==20) { led1=~led1;led2=~led2; tt=0; if(second==0 && bz==1) { while (bza==1) { beep=0; delay(100); beep=1; break; } } else second--; } keyscan(); if (bz==0) display(hour,minute); else displaym(second); delay(2); } } unsigned int jcws(unsigned int a) { sprintf(cc,"%d",a); i=0; while(cc[i++] != '\0'); return i-1; } void keyscan() { if(key1==0 && bz==0) { TR0=0; hour++; if(hour==24) hour=0; display(hour,minute); delay(200) ; } if(key2==0 && bz==0) {TR0=0; minute++; if(minute==60) minute=0; display(hour,minute); delay(200) ; } if(key3==0) { bz=1; bza=1; TR0=0; second=hour*3600+minute*60; if (second==0) { hour=0;minute=0; second=300;} //delay(200) ; TR0=1; } if(key4==0) { beep=1; bz=0; bza=0; minute=0; TR0=0; led1=1; led2=1; second=0; hour=0; minute=0; } } void init() { tt=0; bza=1; position=0; second=0; minute=0; hour=0; smg_q=1; smg_b=1; smg_s=1; smg_g=1; key1=1; key2=1; TMOD=0X01; TH0=0x4c; TL0=0x00; EA=1; ET0=1; TR0=0; } void t0() interrupt 1 { TH0=0x4c; TL0=0x00; tt++; } void display(unsigned char hour,unsigned char minute) { P0=0XFF; switch(position) { case 0: smg_g=1;smg_q=0;P0=table[hour/10]; break; case 1: smg_q=1;smg_b=0; P0=table[hour%10]; dian=0x00; break; case 2: smg_b=1;smg_s=0;P0=table[minute/10];break; case 3: smg_s=1;smg_g=0;P0=table[minute%10];break; } position++; if(position>3) position=0; } void displaym(unsigned int second) { P0=0XFF; ws=jcws(second); switch(position) { case 0: smg_g=1;smg_q=0; P0=table[second/1000%10]; if (ws>4) { dian=0x00; } else {dian=0xff; } //delay(2); break; //case 0: smg_g=1;smg_q=0;P0=table[second/1000%10]; break; case 1: smg_q=1;smg_b=0; P0=table[second/100%10];; break; case 2: smg_b=1;smg_s=0;P0=table[second/10%10];break; case 3: smg_s=1;smg_g=0;P0=table[second%10];if(tt>=10) P0&=0x7f;break; } position++; if(position>3) position=0; } void delay(unsigned int time) { unsigned int x,y; for(x=time;x>0;x--) for(y=110;y>0;y--); }