基于89C51的一個(gè)簡單的警報(bào)系統(tǒng)
#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的延時(shí)×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) //初始化定時(shí)時(shí)鐘
{
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) //判斷警報(bào),為高電平打開警報(bào)
{
if(alarm == 1)
{
delay_1ms(10);
if(alarm == 1);
{
alarm_flag = 1;//響鈴標(biāo)志
flag_change = 1; //鈴聲改變標(biāo)志
t0_set = 5;//調(diào)響鈴節(jié)奏
}
}
}
void alarm_close(void) //低電平,解除警報(bào)
{
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;//跳出循環(huán)
}
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) //調(diào)時(shí)間長短
{
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;
}