求大神告知方法
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "adc.h"
#include "timer.h"
#include "usart1.h"
#include "usart3.h"
#include "LCD1602.h"
#include "string.h"
#include "stdio.h"
#include "dht11.h"
#include "stmflash.h"
#define WriteFlashAddress ((u32)0x08010000)//读写起始地址
#define RATIO 800 //系数,建议选择800-1000
u16 PM25_Value = 0; //PM = ((pmBuf[1]<<8)+pmBuf[2])/1024*8*ratio
u16 PM25_Value_max = 200; //PM2.5报警值
u8 pmBuf[7];
void Change_data_handle(void);
void SHOW_AND_BAOJING_Handle(void);
u8 KEY_SCAN(void); //按键函数
u8 SHOW_BUF[16],SEND_BUF[16];
u8 key,PPM,Temperature,HUM;
u16 PPM_MAX=60,Temperature_MAX=40,HUM_MAX=70;//烟雾温湿度上限初始值
u16 write_buf[5];
///////串口相关
extern unsigned char B_RX_OK;
extern u16 USART3_RX_CNT;
extern u8 USART3_RX_BUF[COM_RX_Lenth + 1];
void save_data()//存储数据
{
write_buf[0]=1111;
write_buf[1]=PPM_MAX;
write_buf[2]=Temperature_MAX;
write_buf[3]=HUM_MAX;
write_buf[4]=PM25_Value_max;
STMFLASH_Write(WriteFlashAddress,write_buf,5);
}
void read_data()//读取数据
{
static u16 chcek = 0;
STMFLASH_Read(WriteFlashAddress,write_buf,5);
chcek = write_buf[0];
if(chcek != 1111)//单片机第一次使用时,chcek不等于1111,以下则是保存一下数据,再进行读取
{
save_data();
delay_ms(100);
STMFLASH_Read(WriteFlashAddress,write_buf,5);
}
PPM_MAX = write_buf[1]%100;//烟雾上限值
Temperature_MAX = write_buf[2]%100;//温度上限值
HUM_MAX = write_buf[3]%100;//湿度上限值
PM25_Value_max = write_buf[4]%1000;//PM上限值
}
void Get_PM(void)//获取PM2.5
{
char i = 0;
char j = 0;
USART3_RX_CNT = 0;
if(B_RX_OK == 1)//接收到数据
{
for(i = 0; i<8; i++)
{
if((USART3_RX_BUF[ i] == 0xAA)&&(USART3_RX_BUF[i+6]==0xFF))//判断值是否正确
{
goto find2;
}
}
goto end2;
find2:
for(j = 0; j<7; j++)
{
pmBuf[j] = USART3_RX_BUF[i+j];//数据接收
}
PM25_Value = (unsigned int)((pmBuf[1]*256)+pmBuf[2])*5/2048.0*RATIO;//计算PM2.5
B_RX_OK = 0;
}
end2:
return;
}
int main(void)
{
u16 test,test_adc;
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
KEY_IO_Init();
USART3_Init(2400);//串口3初始化,波特率2400
TIM2_Int_Init(1000,72-1); //定时器初始化,定时1MS
read_data();//开机读取一次存储值
delay_ms(300);
DHT11_Init(); //DHT11初始化
BEEP_DISENABLE(); //蜂鸣器初始化
LCD_Init(); // 1602初始化
Adc_Init(); //ADC初始化
while(1)
{
test_adc = Get_Adc_Average(ADC_Channel_9,20);//读取通道9的AD值,20ms读取一次
PPM = test_adc*99/4096;//12位AD,把AD值转换成百分比0~99
PPM = PPM >= 99? 99: PPM;//最大值不能超过99
DHT11_Read_Data(&Temperature,&HUM);//读取温湿度值
Get_PM();//获取PM2.5值
if(PM25_Value > 999)PM25_Value = 999;
SHOW_AND_BAOJING_Handle();
Change_data_handle();//按键调节
delay_ms(10);
}
}
extern u8 FLICKER;
void SHOW_AND_BAOJING_Handle(void)
{
unsigned char show_buf[16];//显示缓存区
if(Temperature >= Temperature_MAX)//温度超限
{
if(FLICKER)//超标显示闪烁
{
LCD_Write_String(0,0," ");
}
else
{
sprintf(show_buf,"TEMP:%02dC",(u16)Temperature);//转换显示格式为TEMP:00C
LCD_Write_String(0,0,show_buf);//显示
}
}
else
{
sprintf(show_buf,"TEMP:%02dC",(u16)Temperature);//转换显示格式为TEMP:00C
LCD_Write_String(0,0,show_buf);//显示
}
if(HUM >= HUM_MAX)//湿度超限
{
if(FLICKER)//超标显示闪烁
{
LCD_Write_String(9,0," ");
}
else
{
sprintf(show_buf,"HUM:%02d%%",(u16)HUM);//转换显示格式为HUM:00%
LCD_Write_String(9,0,show_buf);//显示
}
}
else
{
sprintf(show_buf,"HUM:%02d%%",(u16)HUM);//转换显示格式为HUM:00%
LCD_Write_String(9,0,show_buf);
}
if(PPM >= PPM_MAX)//烟雾超限
{
if(FLICKER)//超标显示闪烁
{
LCD_Write_String(0,1," ");
}
else
{
sprintf(show_buf,"YW:%02d%%",PPM);//转换显示格式为SMOKE:00%
LCD_Write_String(0,1,show_buf);
}
}
else
{
sprintf(show_buf,"YW:%02d%%",PPM);//转换显示格式为SMOKE:00%
LCD_Write_String(0,1,show_buf);
}
if(PM25_Value >= PM25_Value_max)//PM2.5超限
{
if(FLICKER)//超标显示闪烁
{
LCD_Write_String(7,1," ");
}
else
{
sprintf(show_buf,"P%03dug/m3%",PM25_Value);//转换显示格式为P000ug/m3
LCD_Write_String(7,1,show_buf);
}
}
else
{
sprintf(show_buf,"P%03dug/m3%",PM25_Value);//转换显示格式为P000ug/m3
LCD_Write_String(7,1,show_buf);
}
if(Temperature >= Temperature_MAX || HUM >= HUM_MAX || PPM >= PPM_MAX || PM25_Value >= PM25_Value_max)//温度湿度烟雾pm超限
{
BEEP_ENABLE(); //蜂鸣器报警
FANG = 1;
}
else
{
BEEP_DISENABLE();
FANG = 0;
}
}
u8 KEY_SCAN(void)
{
u8 res = 0;
if(!KEY1)
{
delay_ms(10);
if(!KEY1)
{
while(!KEY1);//卡死
res = 1;
}
}
if(!KEY2)
{
delay_ms(10);
if(!KEY2)
{
while(!KEY2);//卡死
res = 2;
}
}
if(!KEY3)
{
delay_ms(10);
if(!KEY3)
{
while(!KEY3);//卡死
res = 3;
}
}
return res;
}
void Change_data_handle(void)
{
u8 key;
static u8 MODE = 0;
key = KEY_SCAN();//读取按键值
if(key == 1)//按键1按下
{
MODE++;
LCD_Clear();//清屏
delay_ms(100);
}
while(MODE)
{
key = KEY_SCAN();
switch(MODE)
{
case 1: //设置温度
//显示
LCD_Write_String(0,0,"Set the TEMP ");
LCD_Write_String(0,1,"max:");
LCD_Write_Char(8,1,0xdf);
LCD_Write_Char(9,1,'C');
if(key == 2 && Temperature_MAX < 99) Temperature_MAX++;//按键2按下,值加1
if(key == 3 && Temperature_MAX > 0) Temperature_MAX--;//按键3按下,值减1
LCD1602_write_long(6,1,Temperature_MAX,2);//显示值
//存储值
save_data();
delay_ms(10);
break;
case 2: //设置湿度
//显示
LCD_Write_String(0,0,"Set the HUM ");
LCD_Write_String(0,1,"max:");
LCD_Write_String(8,1,"%");
if(key == 2 && HUM_MAX < 99) HUM_MAX += 1;//按键2按下,值加1
if(key == 3 && HUM_MAX > 0) HUM_MAX -= 1;//按键3按下,值减1
LCD1602_write_long(6,1,HUM_MAX,2);
//存储值
save_data();
delay_ms(10);
break;
case 3: //设置烟雾
//显示
LCD_Write_String(0,0,"Set the SMOKE ");
LCD_Write_String(0,1,"max:");
LCD_Write_String(10,1,"% ");
if(key == 2 && PPM_MAX < 99) PPM_MAX += 1;//按键2按下,值加1
if(key == 3 && PPM_MAX > 0) PPM_MAX -= 1;//按键3按下,值减1
LCD1602_write_long(8,1,PPM_MAX,2);
//存储值
save_data();
delay_ms(10);
break;
case 4: //设置PM2.5
//显示
LCD_Write_String(0,0,"Set the PM2.5 ");
LCD_Write_String(0,1,"max:");
LCD_Write_String(8,1,"ug/m3");
if(key == 2 && PM25_Value_max < 999) PM25_Value_max += 1;//按键2按下,值加1
if(key == 3 && PM25_Value_max > 0) PM25_Value_max -= 1;//按键3按下,值减1
LCD1602_write_long(5,1,PM25_Value_max,3);
save_data();//存储值
delay_ms(10);
break;
default: break;
}
if(key == 1)
{
MODE++;
LCD_Clear();
if(MODE == 5)//按下次数等于4退出设置
{
MODE = 0;
}
}
}
}
|