功能介绍:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示当前温度和甲烷浓度
2.超过甲烷浓度阈值,蜂鸣器报警
3.按键可更改甲烷浓度阈值上限和启动/暂停检测
4.0`20%浓度,绿色LED点亮
20%~阈值上限,黄色LED点亮,电机启动
超过阈值上限,红色LED点亮,电机启动,蜂鸣器报警
5.每隔2天,电机转动10分钟(为了方便演示,演示视频为间隔20秒)
6.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:
#include <reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include <stdio.h>
#include "18b20.h"
#include "tlc0832.h"
#include "lcd1602.h"
#include "delay.h"
sbit KEY_SUB = P3^6;
sbit KEY_ADD = P3^4;
sbit KEY_ALARM = P3^5;
sbit BUZZER = P2^2;
sbit MOTOR = P3^7;
sbit LED_RED = P1^5;
sbit LED_YELLOW = P1^4;
sbit LED_GREEN = P1^3;
#define ON 0
#define OFF 1
#define BUZZER_ON (BUZZER = 0)
#define BUZZER_OFF (BUZZER = 1)
#define MOTOR_ON (MOTOR = 0)
#define MOTOR_OFF (MOTOR = 1)
unsigned char dispFlag = 0; //更新显示标志
bit setFlag = 0; //设置参数标志
bit sendFlag = 0; //串口发送标志
bit alarmSwitch = 1; //报警开关
bit alarmFlag = 1; //报警标志
bit motorFlag = 0; //报警开启电机标志
bit motorFlag1 = 1; //定时开启电机标志
unsigned int second = 0;
unsigned char hour = 0;
int tempBuf = 0; //读取温度值
float temperature; //实际温度值
int temperatureMax = 50; //温度阈值
bit readTempFlag = 0; //定义读温度标志
int Conc = 0;
unsigned int ConcMax = 50;
char dis0[18]; //定义显示区域临时存储数组
void SendData(void);
void CheckKey(void);
void Timer0_Init(void); //定时器初始化
void UART_Init(void); //串口初始化
void UART_SendByte(unsigned char dat); //串口发送单字节数据
void UART_SendStr(unsigned char *s, unsigned char length); //发送定长度字符串
void main(void)
{
bit ack = 0;
Timer0_Init();
LCD_Init(); //初始化液晶
DelayMs(200); //延时有助于稳定
LCD_Clear(); //清屏
LCD_DispStr(0, 0, "T: C S: %");
LCD_DispOneChar(7, 0, 0xdf); //写入温度右上角点
sprintf(dis0, "SS:%3d%%", ConcMax); //打印温度值
LCD_DispStr(0, 1, dis0); //显示第二行
UART_Init(); //初始化串口
while (1) //主循环
{
CheckKey(); //检测按键
if (readTempFlag == 1) //读取温度标志
{
readTempFlag = 0;
DS18B20_Start();
ack = DS18B20_GetTemp(&tempBuf);
if (ack == 1)
{
temperature = (float)tempBuf * 0.0625; //温度值转换
}
else
{
temperature = 0;
}
}
if (dispFlag == 1)
{
dispFlag = 0;
sprintf(dis0, "%5.1f", temperature); //打印温度值
LCD_DispStr(2, 0, dis0); //显示第二行
Conc = ReadADC(AIN0_GND);
if (Conc <= 29)
{
Conc = 29;
}
Conc = (100 * (Conc - 29)) / 227; //计算有害气体浓度百分比
sprintf(dis0, "%3d", Conc); //打印温度值
LCD_DispStr(12, 0, dis0); //显示第二行
if (alarmSwitch)
{
if (Conc > ConcMax) //浓度超过最大设定则报警
{
BUZZER_ON;
motorFlag = 1;
LED_RED = ON;
LED_YELLOW = OFF;
LED_GREEN = OFF;
alarmFlag = 1;
}
else if (Conc > 20 && Conc <= ConcMax) //浓度处于中间范围
{
BUZZER_OFF;
motorFlag = 1;
LED_RED = OFF;
LED_YELLOW = ON;
LED_GREEN = OFF;
alarmFlag = 0;
}
else //浓度正常范围
{
BUZZER_OFF;
motorFlag = 0;
LED_RED = OFF;
LED_YELLOW = OFF;
LED_GREEN = ON;
alarmFlag = 0;
}
}
if (motorFlag == 1 || motorFlag1 == 1)
{
MOTOR_ON;
}
else
{
MOTOR_OFF;
}
}
if (sendFlag == 1)
{
sendFlag = 0;
SendData();
}
}
}
/************************* 串口发送数据 *************************/
void SendData(void)
{
sprintf(dis0, "T:%5.1f'C S:%3d%%\r\n", temperature, Conc); //串口发送
UART_SendStr(dis0, 18); //发送数据
DelayMs(100);
if (alarmFlag == 1)
{
UART_SendStr("Warning!!!\r\n", 12); //发送数据
DelayMs(100);
}
}
void CheckKey(void)
{
if (KEY_SUB == 0) //减键按下
{
DelayMs(200);
if (KEY_SUB == 0) //减键按下
{
if (ConcMax > 20)
{
ConcMax = ConcMax - 1;
}
sprintf(dis0, "SS:%3d%%", ConcMax);
LCD_DispStr(0, 1, dis0);
}
}
if (KEY_ADD == 0) //加键按下
{
DelayMs(200);
if (KEY_ADD == 0) //加键按下
{
if (ConcMax < 99)
{
ConcMax = ConcMax + 1;
}
sprintf(dis0, "SS:%3d%%", ConcMax);
LCD_DispStr(0, 1, dis0);
}
}
if (KEY_ALARM == 0) //取消报警按键
{
DelayMs(20);
if (KEY_ALARM == 0) //取消报警按键
{
if (alarmSwitch)
{
alarmSwitch = 0;
BUZZER_OFF;
LED_RED = OFF;
LED_YELLOW = OFF;
LED_GREEN = OFF;
}
else
{
alarmSwitch = 1;
}
}
while (KEY_ALARM == 0);
}
}
/*------------------------------------------------
定时器初始化子程序
------------------------------------------------*/
void Timer0_Init(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
/*------------------------------------------------
定时器中断子程序
------------------------------------------------*/
void Timer0_Interrupt(void) interrupt 1
{
static unsigned char time20ms = 0;
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
time20ms++;
if (time20ms > 50)
{
time20ms = 0;
readTempFlag = 1; //读标志位置1
sendFlag = 1;
if (motorFlag1 == 0) //电机停止状态
{
if (second >= 20 - 1) //1小时
{
second = 0;
hour++;
if (hour >= 2 - 1) //48小时
{
hour = 0;
motorFlag1 = 1; //电机启动标志
}
}
else
{
second++;
}
}
else
{
if (second >= 20 - 1) //10分钟
{
second = 0;
motorFlag1 = 0;
}
else
{
second++;
}
}
}
if (time20ms % 10 == 0)
{
dispFlag = 1;
}
}
/************************* 串口配置 *************************/
void UART_Init(void)
{
SCON = 0x50;
TH2 = 0xFF;
TL2 = 0xFD;
RCAP2H = 0xFF; //(65536-(FOSC/32/BAUD)) BAUD = 115200 FOSC = 11059200
RCAP2L = 0xFD;
/*****************/
TCLK = 1;
RCLK = 1;
C_T2 = 0;
EXEN2 = 0;
/*****************/
TR2 = 1;
ES = 0; //关闭串口中断
EA = 1; //打开总中断
}
/************************* 串口发送字节 *************************/
void UART_SendByte(unsigned char dat) //串口发送单字节数据
{
unsigned char time_out;
time_out = 0;
SBUF = dat; //将数据放入SBUF中
while ((!TI) && (time_out < 100)) //检测是否发送出去
{
time_out++;
DelayUs10x(2);
} //未发送出去 进行短暂延时
TI = 0; //清除ti标志
}
/************************* 串口发送字符串 *************************/
void UART_SendStr(unsigned char *s, unsigned char length)
{
unsigned char num;
num = 0x00;
while (num < length) //发送长度对比
{
UART_SendByte(*s); //放松单字节数据
s++; //指针++
num++; //下一个++
}
}
仿真演示视频:
https://www.bilibili.com/video/BV1514y1h7eT/
实物演示视频:
https://www.bilibili.com/video/BV1uN4y1A7B5/