c51单片机: 利用定时器+蜂鸣器实现定时报警功能,30秒内,每一秒响一下,30秒时间到了,蜂鸣器长鸣 求程序
1个回答
推荐于2017-12-16
展开全部
参考程序:
#include <reg52.h>
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;
}
}
}
#include <reg52.h>
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;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询