基于89C51的一个简单的警报系统
扫描二维码
随时随地手机看文章
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit light = P1^0
sbit loud = P1^7;
sbit alarm = P2^0;
bit alarm_flag,flag_dec,flag_change;
uint t0_count = 1;
uint t0_set,second;
void delay_1ms(unsigned int count)//1MS的延时×count
{
unsigned int i,j;
for(i=0;i
}
void light_show(int i) //灯闪亮
{
light = 0;
delay_1ms(i);
light = 1;
delay_1ms(i);
}
void init_t0(void) //初始化定时时钟
{
TMOD=0x01; //方式2
TH0=(65536-65436)/256;
TL0=(65536-65436)%256;
ET0=1;
EA=0;
}
void init_IO(void)
{
P1 = 0;
P2 = 0;
}
void alarm_check(void) //判断警报,为高电平打开警报
{
if(alarm == 1)
{
delay_1ms(10);
if(alarm == 1);
{
alarm_flag = 1;//响铃标志
flag_change = 1; //铃声改变标志
t0_set = 5;//调响铃节奏
}
}
}
void alarm_close(void) //低电平,解除警报
{
if(alarm == 0)
{
delay_1ms(10);
if(alarm == 0);
{
alarm_flag = 0;
light = 0;
loud = 0;
TR0 = 0;
EA = 0;
}
}
}
void main()
{
uint i,j;
init_t0();
init_IO();
while(1)
{
alarm_check();
while(alarm_flag)
{
TR0 = 1;
EA = 1;
for(j = 0;j < 40;j++)
{
alarm_close();
if(alarm_flag == 0)
{
break;//跳出循环
}
light_show(100);
}
}
}
}
void t0(void) interrupt 1 using 0
{
EA = 0;
TR0 = 0;
t0_count--;
if(t0_count == 0)
{
t0_count = t0_set;
if(flag_change == 1)
{
loud = ~loud;
}
}
if(flag_change == 0)
{
loud = 1;
}
second++;
if(second == 30) //调时间长短
{
flag_dec = 1;
second = 0;
if(t0_set ==1)
{
t0_set = 1;
flag_dec =0;
flag_change = 0;
}
if(flag_dec == 1)
{
t0_set--;
}
}
TR0 = 1;
EA = 1;
}