此设计无法进行仿真,因为isis里面的库文件有限制32单片机和甲醛无法仿真,所以此设计没有办法进行仿真,
大家需要注意的是 ,仿真和实物还是有很大差别的,因为仿真里面电阻,电容或者是其他的芯片用
其他器件代替都会导致程序不一样,既然实物都做出来了,说明肯定会没有问题。有的师傅可能会问,
为什么没有仿真,你是如何做出来的。其实做东西不一定需要仿真,厂家都会提供芯片手册,我们只需
要按照芯片手册进行编写程序就可以了。芯片手册里面会写的很详细
测量环境 温湿度 甲醛 程序附件包括
主程序流程图
DHT11温湿度电路讲解
此设计是stm32单片机,单片机上电后引脚为高组态,所以我们不可以使用8550,因为8550 是低电平导通,8050 是高电平导通,当我们想让蜂鸣器进行报警的时候,我们给三极管一个高电平 ,就实现了蜂鸣器报警。三极管本身具有的作用是放大电流(放大200倍)加1k电阻是为了进行限流。
电源有很多种方式,以上面一种为连接方式,如果大家遇到其他的连接方式,都是一样的
Swich 是电源开关,pow是电源输入端
供电电压为DC5v dc是代表直流,
焊接如图所示:
板子正面:
板子背面:
电路中10k(或者是5K)为上拉电阻,因为普通单片机上拉能力不够,如果线路较长,就会有很大的寄生电容,从而变成RC充放电...慢到一定程度数据就会出错的
上拉电阻,当没数据时,该信号线被拉成高电平
2 原理图讲解 ,3温湿度甲醛程序
- #include "led.h"
- #include "delay.h"
- #include "sys.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)//读写起始地址
- u16 CH20_MAX_val = 100;//甲醛报警值
- u16 CH2O_mgvalue = 0;//甲醛
- void Change_data_handle(void);
- void SHOW_AND_BAOJING_Handle(void);
- u8 KEY_SCAN(void); //按键函数
- u8 SHOW_BUF[16],SEND_BUF[16];
- u8 key,Temperature,HUM;
- u16 Temperature_MAX=40,HUM_MAX=70;//温湿度上限初始值
- u16 write_buf[4];
- ///////串口相关
- extern char RxCounter,Usart1RecBuf[64];//串口1接收数据缓存
- extern char bUsart1RecFlag; //串口1收到数据标志位
- void save_data()//存储数据
- {
- write_buf[0]=1111;
- write_buf[1]=Temperature_MAX;
- write_buf[2]=HUM_MAX;
- write_buf[3]=CH20_MAX_val;
- STMFLASH_Write(WriteFlashAddress,write_buf,4);
- }
- void read_data()//读取数据
- {
- static u16 chcek = 0;
- STMFLASH_Read(WriteFlashAddress,write_buf,4);
- chcek = write_buf[0];
- if(chcek != 1111)//单片机第一次使用时,chcek不等于1111,以下则是保存一下数据,再进行读取
- {
- save_data();
- delay_ms(100);
- STMFLASH_Read(WriteFlashAddress,write_buf,4);
- }
- Temperature_MAX = write_buf[1]%100;//温度上限值
- HUM_MAX = write_buf[2]%100;//湿度上限值
- CH20_MAX_val = write_buf[3]%1000;//PM上限值
- }
- void Get_CH2O(void)//获取甲醛
- {
- char i = 0;
- if(Usart1RecBuf[i+5] == Usart1RecBuf[i]+Usart1RecBuf[i+1]+Usart1RecBuf[i+2]+Usart1RecBuf[i+3]+Usart1RecBuf[i+4])//校验
- {
- CH2O_mgvalue = Usart1RecBuf[1]*256 + Usart1RecBuf[2];//计算甲醛
- bUsart1RecFlag = 0;
- }
- RxCounter = 0;
- }
- int main(void)
- {
- u16 test,test_adc;
-
- delay_init(); //延时函数初始化
- NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- KEY_IO_Init();
- Usart1_Init(9600);//串口3初始化,波特率9600
- TIM2_Int_Init(1000,72-1); //定时器初始化,定时1MS
- read_data();//开机读取一次存储值
- delay_ms(300);
- DHT11_Init(); //DHT11初始化
- BEEP_DISENABLE(); //蜂鸣器初始化
- LCD_Init(); // 1602初始化
- while(1)
- {
- DHT11_Read_Data(&Temperature,&HUM);//读取温湿度值
- Get_CH2O();//获取甲醛值
- SHOW_AND_BAOJING_Handle();
- Change_data_handle();//按键调节
- delay_ms(100);
- }
- }
- 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(CH2O_mgvalue >= CH20_MAX_val && FLICKER)//甲醛超限
- {
- LCD_Write_String(0,1," ");
- }
- else
- {
- LCD_Write_Char(0,1,'C');
- LCD_Write_Char(1,1,'H');
- LCD_Write_Char(2,1,'2');
- LCD_Write_Char(3,1,'O');
- LCD_Write_Char(4,1,':');
- LCD_Write_Char(5,1,CH2O_mgvalue/100+'0');
- LCD_Write_Char(6,1,'.');
- LCD_Write_Char(7,1,CH2O_mgvalue%100/10+'0');
- LCD_Write_Char(8,1,CH2O_mgvalue%10+'0');
- LCD_Write_Char(9,1,'m');
- LCD_Write_Char(10,1,'g');
- LCD_Write_Char(11,1,'/');
- LCD_Write_Char(12,1,'m');
- LCD_Write_Char(13,1,'3');
- }
- if(Temperature >= Temperature_MAX || HUM >= HUM_MAX|| CH2O_mgvalue >= CH20_MAX_val)//温度湿度甲醛超限
- {
- BEEP_ENABLE(); //蜂鸣器报警
- }
- else
- {
- BEEP_DISENABLE();
- }
- }
- 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 CH2O ");
- LCD_Write_String(0,1,"max:");
- LCD_Write_String(9,1,"mg/m3");
- if(key == 2 && CH20_MAX_val < 999) CH20_MAX_val += 1;//按键2按下,值加1
- if(key == 3 && CH20_MAX_val > 0) CH20_MAX_val -= 1;//按键3按下,值减1
- LCD_Write_Char(5,1,CH20_MAX_val/100+'0');
- LCD_Write_Char(6,1,'.');
- LCD_Write_Char(7,1,CH20_MAX_val%100/10+'0');
- LCD_Write_Char(8,1,CH20_MAX_val%10+'0');
- save_data();//存储值
-
- delay_ms(10);
- break;
- default: break;
- }
- if(key == 1)
- {
- MODE++;
- LCD_Clear();
- if(MODE == 4)//按下次数等于4退出设置
- {
- MODE = 0;
- }
- }
- }
-
- }
复制代码
Keil代码下载(仅供参考):
温湿度甲醛Keil工程.7z
(206.92 KB, 下载次数: 31)
|