如果改成这样,应该怎么改?:初始时间及定时时间可调,且动态显示剩余时间,并在最后一分钟发出警告声,时间到提醒。
转:倒计时器带闹c程序,最长定时99:59,时间到,闹铃10秒,已在学习板上通过,稍改后可用于控制其他负载。刚学请各位老师指导!用数码管显示的吧,电路下载:http://www.51hei.com/f/ks51.pdf
#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90};
uint8 a[6];
uint16 counter = 0;
uint8 sec,min=0,hour=0,t_min=0,t_hour=0,ky=0; //操作:a:1、开机显示6个0,可进行闹时设置。-> 2、功能按键K3按1下走时。-> // 3 、功能键再按1下关闹铃。->再按1下回到闹时设置。循环。
uint16 counter1=0;
//b: K1、K2可设置时、分,最长定时99:59。
sbit BUZZ = P1^6;
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit KeyOut1=P2^3;
sbit K1 = P2^4;
sbit K2 = P2^5;
sbit K3 = P2^6;
void timer1_init() //定时器1初使化
{
TMOD |= 0x10;
TMOD &= 0xdf;
TH1 = 0xFC;
TL1 = 0x67;
ET1 = 1;
EA = 1;
TR1 = 1;
}
void interrupt_timer1()interrupt 3 //中断函数
{ TH1 = 0xFC;
TL1 = 0x67;
counter1++;
if(counter1 == 1000)
{ counter1 =0;
TR1 = 0;}
BUZZ = ~BUZZ;
}
void timer2_init() // 定时器T2运行函数
{
T2MOD=0X00;
TH2=(65536-110592/60)/256;
TL2=(65536-110592/60)%256;
RCAP2H=TH2;
RCAP2L=TL2;
EA=1;
ET2=1;
TR2=1;
}
void refresh_led() //数码管刷新函数
{
static uint8 j = 0;
switch(j)
{
case 0: ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;j++;P0=0x7f & number[a[0]];break;
case 1: ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;j++;P0 = number[a[1]];break;
case 2: ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;j++;P0 =0x7f & number[a[2]];break;
case 3: ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;j++;P0 = number[a[3]];break;
case 4: ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;j++;P0 =0x7f & number[a[4]];break;
case 5: ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;j = 0;P0 = number[a[5]];break;
default: break;
}
}
void Hms(void) //秒,分,时计算函数
{
static uint16 ui2msCount=0;
if(ky==1)
ui2msCount++;
if(ui2msCount>499)
{
ui2msCount=0;
if (sec==0)
{
sec=60;
if (min==0)
{
min=60;
if (hour==0)
hour=99;
hour--;
}
min--;
}
sec--;
}
a[5]=hour/10;
a[4]=hour%10;
a[3]=min/10;
a[2]=min%10;
a[1]=sec/10;
a[0]=sec%10;
if((ky==1)&&(sec==0)&&(min==0)&&(hour==0))
(timer1_init(),ky=0);
}
void Key3(void) //K3按键处理函数
{
while(!K3);
ky++;
if(ky > 1)
ky=0;
}
void Key2(void) //K2按键处理函数
{
while(!K2);
if(ky==0)
min++;
if(min>59)
min=0;
}
void Key1(void) //K1按键处理函数
{
while(!K1);
if(ky==0)
hour++;
if(hour>99)
hour=0;
}
void Delay(uint8 ms) //毫秒延时函数
{
uint8 i;
while(ms--)
{
for( i=0;i<125;i++)
;
}
}
main() //主函数
{
ENLED = 0; ADDR3 = 1;
timer2_init();
KeyOut1=0;
while(1) //主循环
{
if(!K1)
Delay(5);
if(!K1)
Key1();
if(!K2)
Delay(5);
if(!K2)
Key2();
if(!K3)
Delay(5);
if(!K3)
Key3();
}
}
void interrupt_timer2() interrupt 5 //定时器T2中断函数
{
TF2=0;
Hms();
refresh_led();
} |