功能介绍:
0.本系统采用STC89C52作为单片机
1.采用LCD1602液晶作为系统的显示器可实时显示数据
2.系统设计了蜂鸣器和LED组成的声光报警电路,当检测到天然气泄漏,可实现声光报警
3.除了现场的声光报警,系统还可以通过GSM模块实现无线报警
4.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB:
主程序:
#define __MAIN_C
#include "main.h"
#include "stdio.h"
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "bsp_gpio.h"
#include "bsp_clkconfig.h"
#include "bsp_delay.h"
#include "bsp_lcd1602.h"
#include "bsp_GeneralTim.h"
#include "bsp_key.h"
#include "bsp_usart.h"
volatile CreatByte Flag;
int flowA = 0;
int flowB = 0;
unsigned int time500ms = 0;
char dis[16];
int main(void)
{
// 使用HSI,SYSCLK = 4M * RCC_PLLMul_x, x:[2,3,...16],最高是64MH
HSI_SetSysClock(RCC_PLLMul_2); //使用内部8MHz晶振,并设置PLL输出为8MHz
// GPIO端口初始化
GPIO_Config();
Key_GPIO_Config();
LCD_GPIO_Init();
LCD_Init();
LCD_Clear();
DelayMs(250);
LCD_DispStr(0, 0, " Welcome! ");
USART_Config();
DelayMs(50);
printf("AT+CMGF=1\r\n");
DelayMs(1000); //延时等待gsm稳定
LCD_Clear();
GENERAL_TIM_Init(); //初始化定时器
sprintf(dis, "Flow A:%3d B:%3d", flowA, flowB);
LCD_DispStr(0, 0, dis);
LCD_DispStr(0, 1, "Gas:Normal ");
LCD_DispStr(13, 1, "OFF");
while (1)
{
//每秒钟更新数据
if (refreshFlag == 1)
{
refreshFlag = 0;
sprintf(dis, "Flow A:%3d B:%3d", flowA, flowB);
LCD_DispStr(0, 0, dis);
if (startFlag == 1)
{
if (flowA > flowB + 5 || flowA < flowB - 5) //流量误差大于5则判断泄漏
{
alarmFlag = 1; //报警
LCD_DispStr(0, 0, "Flow Warning! ");
}
flowA = 0;
flowB = 0;
if (MQ2_IN == 0) //监测到天然气含量过高
{
time500ms++;
if (time500ms > 4) //2s后仍检测到,则确认检测到天然气,否则认为误报
{
alarmFlag = 1; //报警
LCD_DispStr(0, 1, "Gas:Warning!");
time500ms = 0;
}
}
else
{
LCD_DispStr(0, 1, "Gas:Normal ");
time500ms = 0;
}
LCD_DispStr(13, 1, " ON");
if (alarmFlag == 1)
{
BUZZER_ON; //打开蜂鸣器
RELAY_OFF; //关闭阀门
startFlag = 0; //停止标志
LCD_DispStr(13, 1, "OFF");
/************************* 发送短信 *************************/
printf("AT+CMGF=1\r\n");
DelayMs(1000);
printf("AT+CSCS=\"GSM\"\r\n");
DelayMs(1000);
printf("AT+CMGS=\"+86xxxxxxxxxxx\"\r\n"); //可以修改电话号码
DelayMs(1000);
printf("Please be aware of pipe leaks!\x1a"); //可以修改短信内容
DelayMs(1000);
}
}
}
KeyProcess();
}
}
/*********************************************END OF FILE**********************/