本项目使用Proteus8仿真STM32单片机控制器,使用按键、LCD1602液晶、蜂鸣器模块、CO、NOx、HC和PM2.5气体传感器等。
主要功能:
系统运行后,LCD1602显示CO、NOx、HC和PM2.5气体浓度检测值,可通过按键K3进入阈值设置模式,K1和K2调节阈值,K4键确定并返回主界面。
当检测值高于阈值,则蜂鸣器报警,并且相应指示灯点亮。
/*
作者:嗨小易(QQ技术交流群:570487280)
*/
//系统数据显示
void sys_data_show(void)
{
u8 buf[5];
while(1)
{
//正常模式显示
if(sys_ctrl.mode==0)
{
//co显示
buf[0]=sys_ctrl.co/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.co%100/10+0x30;
buf[3]=sys_ctrl.co%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(3,0,buf);
//nox显示
buf[0]=sys_ctrl.nox/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.nox%100/10+0x30;
buf[3]=sys_ctrl.nox%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(12,0,buf);
//hc显示
buf[0]=sys_ctrl.hc/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.hc%100/10+0x30;
buf[3]=sys_ctrl.hc%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(3,1,buf);
//pm25显示
buf[0]=sys_ctrl.pm25/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.pm25%100/10+0x30;
buf[3]=sys_ctrl.pm25%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(12,1,buf);
}
//阈值设置显示
else
{
//co显示
buf[0]=sys_ctrl.threshold[0]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[0]%100/10+0x30;
buf[3]=sys_ctrl.threshold[0]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(3,0,buf);
//nox显示
buf[0]=sys_ctrl.threshold[1]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[1]%100/10+0x30;
buf[3]=sys_ctrl.threshold[1]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(12,0,buf);
//hc显示
buf[0]=sys_ctrl.threshold[2]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[2]%100/10+0x30;
buf[3]=sys_ctrl.threshold[2]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(3,1,buf);
//pm25显示
buf[0]=sys_ctrl.threshold[3]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[3]%100/10+0x30;
buf[3]=sys_ctrl.threshold[3]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(12,1,buf);
switch(sys_ctrl.mode)
{
case 1:
lcd1602_show_string(3,0," ");
delay_ms(100);
buf[0]=sys_ctrl.threshold[0]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[0]%100/10+0x30;
buf[3]=sys_ctrl.threshold[0]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(3,0,buf);
break;
case 2:
lcd1602_show_string(12,0," ");
delay_ms(100);
buf[0]=sys_ctrl.threshold[1]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[1]%100/10+0x30;
buf[3]=sys_ctrl.threshold[1]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(12,0,buf);
break;
case 3:
lcd1602_show_string(3,1," ");
delay_ms(100);
buf[0]=sys_ctrl.threshold[2]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[2]%100/10+0x30;
buf[3]=sys_ctrl.threshold[2]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(3,1,buf);
break;
case 4:
lcd1602_show_string(12,1," ");
delay_ms(100);
buf[0]=sys_ctrl.threshold[3]/100+0x30;
buf[1]='.';
buf[2]=sys_ctrl.threshold[3]%100/10+0x30;
buf[3]=sys_ctrl.threshold[3]%100%10+0x30;
buf[4]='\0';
lcd1602_show_string(12,1,buf);
break;
}
}
break;
}
}
//系统数据设置
void sys_data_set(void)
{
u8 key=0;
key=key_scan(0);
//模式设置
if(key==KEY3_PRESS)
{
sys_ctrl.mode++;
if(sys_ctrl.mode>4)sys_ctrl.mode=1;
}
if(sys_ctrl.mode!=0)
{
//在设置模式下,加
if(key==KEY1_PRESS)
{
switch(sys_ctrl.mode)
{
case 1://co
sys_ctrl.threshold[0]++;
if(sys_ctrl.threshold[0]>100)sys_ctrl.threshold[0]=0;
break;
case 2://nox
sys_ctrl.threshold[1]++;
if(sys_ctrl.threshold[1]>100)sys_ctrl.threshold[1]=0;
break;
case 3://hc
sys_ctrl.threshold[2]++;
if(sys_ctrl.threshold[2]>100)sys_ctrl.threshold[2]=0;
break;
case 4://pm25
sys_ctrl.threshold[3]++;
if(sys_ctrl.threshold[3]>100)sys_ctrl.threshold[3]=0;
break;
}
}
//在设置模式下,减
else if(key==KEY2_PRESS)
{
switch(sys_ctrl.mode)
{
case 1://co
sys_ctrl.threshold[0]--;
if(sys_ctrl.threshold[0]<0)sys_ctrl.threshold[0]=100;
break;
case 2://nox
sys_ctrl.threshold[1]--;
if(sys_ctrl.threshold[1]<0)sys_ctrl.threshold[1]=100;
break;
case 3://hc
sys_ctrl.threshold[2]--;
if(sys_ctrl.threshold[2]<0)sys_ctrl.threshold[2]=100;
break;
case 4://pm25
sys_ctrl.threshold[3]--;
if(sys_ctrl.threshold[3]<0)sys_ctrl.threshold[3]=100;
break;
}
}
//确定
else if(key==KEY4_PRESS)
{
sys_ctrl.mode=0;
}
}
}
//系统功能控制
void sys_fun_ctrl(void)
{
//正常工作模式下
if(sys_ctrl.mode==0)
{
//如果CO浓度超限,报警,指示灯亮
if(sys_ctrl.co>sys_ctrl.threshold[0])
{
LED_CO=0;
beep_alarm(1,1000);
}
else
{
LED_CO=1;
}
//如果nox浓度超限,报警,指示灯亮
if(sys_ctrl.nox>sys_ctrl.threshold[1])
{
LED_NOX=0;
beep_alarm(1,1000);
}
else
{
LED_NOX=1;
}
//如果hc浓度超限,报警,指示灯亮
if(sys_ctrl.hc>sys_ctrl.threshold[2])
{
LED_HC=0;
beep_alarm(1,1000);
}
else
{
LED_HC=1;
}
//如果pm25浓度超限,报警,指示灯亮
if(sys_ctrl.pm25>sys_ctrl.threshold[3])
{
LED_PM25=0;
beep_alarm(1,1000);
}
else
{
LED_PM25=1;
}
}
}
//应用控制系统
void appdemo_show(void)
{
sys_parm_init();//系统参数初始化
lcd1602_init();
sys_open_show();//系统开机界面显示
while(1)
{
sys_data_get();//系统数据获取
sys_data_show();//系统数据显示
sys_data_set();//系统数据设置
sys_fun_ctrl();//系统功能控制
}
}
B站演示视频:https://space.bilibili.com/444388619
视频地址:https://space.bilibili.com/444388619/video
专注于51单片机、STM32、国产32、DSP、Proteus、arduino、ESP32、物联网软件开发,PCB设计,视频分享,技术交流。