刚写的程序,可以通过S0(功能键),S1(加键),S2(减键)设定初值,倒数为00-00-00时蜂鸣器响,通过八位LED显示;
#include<reg52h>
#include<intrinsh>
#define uchar unsigned char
#define uint unsigned int
uchar code wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//八位位选码表
uchar code duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//段选码表
sbit s0=P3^3; //设置功能键
sbit s1=P3^5; //设置‘加’键
sbit s2=P3^6; //设置‘减’键
sbit beep=P3^7; //设置蜂鸣器
void delay(uint z);
void display();
void init();
void keyscan();
uchar shi,fen,miao,nam;
void main()
{
init();
while(1)
{
display();
keyscan();
beep=1;
if (miao==0&&fen==0&&shi==0)//当倒数计时为00-00-00时蜂鸣器响
{
beep=0;
TR0=0;
}
}
}
void delay(uint z)
{
uint i,j;
for (i=z;i>0;i--)
for (j=110;j>0;j--);
}
void display() //我这里P2为位选,P0为段选,显示格式为 时-分-秒
{
uchar s,g;
s=shi/10;
g=shi%10;
P2=wei[7];
P0=duan[s];
delay(1);
P2=wei[6];
P0=duan[g];
delay(1);
P2=wei[5];
P0=0xbf;
delay(1);
s=fen/10;
g=fen%10;
P2=wei[4];
P0=duan[s];
delay(1);
P2=wei[3];
P0=duan[g];
delay(1);
P2=wei[2];
P0=0xbf;
delay(1);
s=miao/10;
g=miao%10;
P2=wei[1];
P0=duan[s];
delay(1);
P2=wei[0];
P0=duan[g];
delay(1);
}
void init()
{
miao=10; //设定初值为 00-01-10
fen=1;
shi=0;
EA=1;
ET0=1;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
}
void timer() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
nam++;
if (nam==20)
{
nam=0;
if (miao==0)
{
miao=60;
if (fen==0)
{
fen=60;
if (shi==0)
shi=24;
shi--;
}
fen--;
}
miao--;
}
}
void keyscan()
{
uchar key,num;
if (s0==0)
{
delay(5);
if (s0==0)
{
key++;
if (key==4)
key=0;
}
while(!s0)
display();
}
if (s1==0)
{
delay(5);
if (s1==0)
{
num=1;
}
while(!s1)
display();
}
if (s2==0)
{
delay(5);
if (s2==0)
{
num=2;
}
while(!s2)
display();
}
switch(key)
{
case 0:TR0=1;break;
case 1:TR0=0;
if (num==1)
{
num=0;
miao++;
if (miao>=60)
miao=0;
display();
}
if (num==2)
{
num=0;
if (miao==0)
miao=60;
miao--;
display();
}
break;
case 2:
if (num==1)
{
num=0;
fen++;
if (fen>=60)
fen=0;
display();
}
if (num==2)
{
num=0;
if (fen==0)
fen=60;
fen--;
display();
}
break;
case 3:
if (num==1)
{
num=0;
shi++;
if (shi>=24)
shi=0;
display();
}
if (num==2)
{
num=0;
if (shi==0)
shi=24;
shi--;
display();
}
break;
}
}
参考程序:
#include <reg52h>
sbit Beep = P3^0;
unsigned char t1s,t50ms;
unsigned char flag30s = 0,flag1s = 0;
void delayms(unsigned int xms)
{
unsigned int i,j;
for(i=0; i<xms; i++)
for(j=0; j<110; j++);
}
void Alarm(void)
{
Beep = 0;
delayms(200);
Beep = 1;
}
void main()
{
TMOD = 0x01;
TH0 = 0x3C;
TL0 = 0xB0;//50ms中断一次
EA = 1;
ET0 = 1;
TR0 = 1;
while(1)
{
if(flag30s)Beep = 0;//30s到蜂鸣器一直响
if(flag1s)//1s到,蜂鸣器响一下
{
flag1s = 0;
Alarm();
}
}
}
void TIM0(void) interrupt 1
{
TH0 = 0x3C;
TL0 = 0xB0;
t50ms++;
if(t50ms == 20)//1s
{
t50ms = 0;
t1s++;
flag1s = 1;
if(t1s == 30)//30s
{
TR0 = 0;
flag30s = 1;
}
}
}
void main()
{
green=0;
red=1;
while(1)
{
if(K==0)
{
delay(1000); //延时约12ms
if(K==0)
{
green=1;
red=0;
beep=0;
delay(40000); //延时约05s
red=1;
beep=1;
}
}
else
{
green=!green;
delay(20000);//延时约025s
}
}
}
或者将delay(40000) 换成while(k==0);//等待按键抬起
以上就是关于高手帮忙51c单片机——想制作倒计时报警器,求个c程序全部的内容,包括:高手帮忙51c单片机——想制作倒计时报警器,求个c程序、c51单片机: 利用定时器+蜂鸣器实现定时报警功能,30秒内,每一秒响一下,30秒时间到了,蜂鸣器长鸣 求程序、51单片机题目: 要求:按下按钮时启动报警并且红灯闪烁,蜂鸣器报警,按钮松开,报警解除,绿灯闪烁。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)