基于51單片機的二氧化碳、溫濕度檢測系統設計。采用LCD1602顯示,CO2傳感器使用MH-Z19B,溫濕度傳感器使用DHT11,此文件含有代碼、原理圖、PCB文件。
制作出來的實物圖如下:
IMG_20190428_012755.jpg (10.06 MB, 下載次數: 70)
下載附件
2019-6-4 08:00 上傳
TIM截圖20190604075936.png (4.78 KB, 下載次數: 85)
下載附件
2019-6-4 08:00 上傳
Altium Designer畫的原理圖和PCB圖如下:(51hei附件中可下載工程文件)
0.png (43.66 KB, 下載次數: 70)
下載附件
2019-6-11 02:16 上傳
0.png (50.07 KB, 下載次數: 70)
下載附件
2019-6-11 02:16 上傳
單片機源程序如下:
- /********************************** (C) COPYRIGHT *******************************
- * File Name : main.C
- * Author :
- * License : MIT
- * Version : V1.0
- * Date : 2019/04/12
- * Description : 51溫濕度二氧化碳檢測
- *******************************************************************************/
- #include <reg52.h>
- #include "1602.h"
- #include "dht.h"
- #include "2402.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
-
- //定義三個LED燈
- sbit Led_qushi = P1^0; //去濕燈
- sbit Led_jiangwen = P1^1; //降溫燈
- sbit led_CO2 = P1^2; //二氧化碳控制燈
- //定義蜂鳴器
- sbit fmq = P2^3; //蜂鳴器
- //定義設置鍵、調整鍵
- sbit Key_TH1 = P3^0;//溫度上限值調整按鍵
- sbit Key_TH2 = P3^1;
- sbit Key_HH1 = P3^2;//濕度上限值調整按鍵
- sbit Key_HH2 = P3^3;
- sbit shezhi = P2^7;//設置鍵
- sbit Key_CH1 = P2^6;//二氧化碳上限值調整按鍵
- sbit Key_CH2 = P2^5;
- //二氧化碳發送指令
- uchar code tab[9]={0xff,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
- uchar date[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- //定義標識
- volatile bit FlagStartRH = 0; //開始溫濕度轉換標志
- volatile bit FlagKeyPress = 0; //有鍵按下
- volatile bit fmqt = 0;
- volatile bit fmqh = 0;
- volatile bit fmqc = 0;
- //定義溫濕度傳感器用外部變量
- extern U8 U8FLAG,k;
- extern U8 U8count,U8temp;
- extern U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
- extern U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
- extern U8 U8comdata;
- extern U8 count, count_r;
- U16 temp;
- S16 temperature, humidity;
- S16 idata TH, HH, ch; //溫度上限和濕度上限//二氧化碳上限值
- char * pSave;
- U8 keyvalue, keyTH1, keyTH2, keyHH1, keyHH2, keyCH1, keyCH2;
- unsigned char moshi=0;
- //定義變量
- U16 RHCounter;
- /************************************
- 延時子程序
- 延時時間(xms*1)ms
- *************************************/
- void delayms(uint xms)
- {
- uint x,y;
- for(x=xms;x>0;x--)
- for(y=110;y>0;y--);
- }
- /*******************************************************************************
- * 功 能 : 串口發送
- * 注意事項 :
- *******************************************************************************/
- void send(uchar dat) //發送一字節
- {
- SBUF=dat;
- while(!TI);
- TI=0;
- }
- /*******************************************************************************
- * 功 能 : 數據初始化程序
- * 注意事項 :
- *******************************************************************************/
- void Data_Init()
- {
- RHCounter = 0;
- Led_qushi = 1;
- Led_jiangwen = 1;
- led_CO2 = 1;
- TH = 40; //溫度初始上限值
- HH = 85; //濕度初始上限限制
- ch = 25; //二氧化碳初始上限值
- keyvalue = 0;
- keyTH1 = 1;
- keyTH2 = 1;
- keyHH1 = 1;
- keyHH2 = 1;
- keyCH1 = 1;
- keyCH2 = 1;
- }
- //定時器0初始化
- void Timer0_Init()
- {
- ET0 = 1; //允許定時器0中斷
- TMOD = 1; //定時器工作方式選擇
- TL0 = 0x06;
- TH0 = 0xf8; //定時器賦予初值
- TR0 = 1; //啟動定時器
- }
- //存入設定值、
- void Save_Setting()
- {
- pSave = (char *)&TH; //地址低位對應低8位,高位對應高8位
- wrteeprom(0, *pSave); //存溫度上限值TH低8位
- DELAY(300);
- pSave ++;
- wrteeprom(1, *pSave); //存溫度上限值TH高8位
- DELAY(300);
- pSave = (char *)&HH;
- wrteeprom(2, *pSave); //存濕度上限值RH低8位
- DELAY(300);
- pSave ++;
- wrteeprom(3, *pSave); //存濕度上限值RH高8位
- DELAY(300);
- pSave = (char *)&ch;
- wrteeprom(4, *pSave); //存CO2上限值RH低8位
- DELAY(300);
- pSave ++;
- wrteeprom(5, *pSave); //存CO2上限值RH高8位
- DELAY(300);
- }
- //載入設定值、
- void Load_Setting()
- {
- pSave = (char *)&TH;
- *pSave++ = rdeeprom(0);
- *pSave = rdeeprom(1);
- pSave = (char *)&HH;
- *pSave++ = rdeeprom(2);
- *pSave = rdeeprom(3);
- pSave = (char *)&ch;
- *pSave++ = rdeeprom(4);
- *pSave = rdeeprom(5);
- if ((TH>99)||(TH<0)) TH = 40;
- if ((HH>99)||(HH<0)) HH = 85;
- if ((ch>50)||(ch<0)) ch = 25;
- }
- //按鍵掃描程序
- void Key_set_scan()
- {
- if(shezhi==0)
- {
- delayms(10);
- if(shezhi==0)
- {
- while(!shezhi);
- L1602_init(); //初始化液晶
- moshi++;
- if(moshi >= 2)moshi = 0;
- if(moshi == 0)
- {
- L1602_string(1,1,"Te: C ");
- L1602_string(1,9,"Hu: % ");
- L1602_string(2,1,"PPM: ");
- }
- else if(moshi == 1)
- {
- L1602_string(1,1,"TH:");
- L1602_string(1,9,"HH:");
- L1602_string(2,1,"CH:");
- L1602_string(2,8,"0");
- L1602_string(2,9,"0");
- }
- }
- }
- }
- //按鍵加減設定值
- void KeyProcess(uint num)
- {
- switch (num)
- {
- case 1:
- if (TH<99) TH++;
- L1602_char(1, 5, TH/10+48);
- L1602_char(1, 6, TH%10+48);
- break;
- case 2:
- if (TH>1) TH--;
- L1602_char(1, 5, TH/10+48);
- L1602_char(1, 6, TH%10+48);
- break;
- case 3:
- if (HH<99) HH++;
- L1602_char(1, 13, HH/10+48);
- L1602_char(1, 14, HH%10+48);
- break;
- case 4:
- if (HH>1) HH--;
- L1602_char(1, 13, HH/10+48);
- L1602_char(1, 14, HH%10+48);
- break;
- case 5:
- if (ch<50) ch++;
- L1602_char(2, 6, ch/10+48);
- L1602_char(2, 7, ch%10+48);
- break;
- case 6:
- if (ch>1) ch--;
- L1602_char(2, 6, ch/10+48);
- L1602_char(2, 7, ch%10+48);
- break;
- default:
- break;
- }
- Save_Setting();
- }
- /*******************************************************************************
- * 函 數 名 : main
- * 函數功能 : 主函數
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void main()
- {
- U16 testnum;
- uint tmp;
- uchar m;
- EA = 0;
- Timer0_Init(); //定時器0初始化
- Data_Init();
- EA = 1;
- TMOD=0x20; // T1定時方式2
- SCON=0x40;//串行工作 方式1
- PCON=0x00; //不加倍
- TH1=TL1=0xfd; //定時器1賦初值 保證波特率9600
- TR1=1; //開定時器1
- REN=1; //允許串口接收
- delayms(10);
- ES=1; //打開串口中斷
- EA=1; //開總中斷
- L1602_init();
- Load_Setting();
- while(1)
- {
- for(m=0;m<9;m++) //向co2傳感器發送數據
- {
- send(tab[m]);
- }
- delayms(100); //延時0.1s 刷新測得數據
- tmp=date[2]*256+date[3]; //計算出co2濃度值
-
- //溫濕度轉換標志檢查
- if (FlagStartRH == 1)
- {
- TR0 = 0;
- testnum = RH();
- FlagStartRH = 0;
- TR0 = 1;
- //讀出溫濕度,只取整數部分
- humidity = U8RH_data_H;
- temperature = U8T_data_H;
- }
- //溫濕度控制
- if (temperature > TH)
- {
- Led_jiangwen = 0;
- fmqt=1;
- }
- else
- {
- Led_jiangwen = 1;
- fmqt=0;
- } //降溫
- if (humidity > HH)
- {
- Led_qushi = 0;
- fmqh=1;
- }
- else
- {
- Led_qushi = 1;
- fmqh=0;
- } //去濕
- if (tmp/100 > ch)
- {
- led_CO2 = 0;
- fmqc=1;
- }
- else
- {
- led_CO2 = 1;
- fmqc=0;
- } //CO2
- if((fmqt==1)||(fmqh==1)||(fmqc==1))
- fmq=0;
- else
- fmq=1;
-
-
- Key_set_scan();//按鍵掃描
- if(moshi == 0) //設置模式切換
- {
- delayms(50);
- L1602_string(1,1,"Te: C ");
- L1602_string(1,9,"Hu: % ");
- L1602_string(2,1,"PPM: ");
- if(tmp>9999)
- tmp=0;
- L1602_int(2,5,tmp);
- if((tmp/1000)>0)
- L1602_int(2,5,tmp/1000);
- //顯示溫濕度
- L1602_int(1,3, temperature);
- L1602_int(1,11,humidity);
- }
- else if(moshi == 1) //設置上限模式
- {
- L1602_char(1, 5, TH/10+48);
- L1602_char(1, 6, TH%10+48);
- L1602_char(1, 13, HH/10+48);
- L1602_char(1, 14, HH%10+48);
- L1602_char(2, 6, ch/10+48);
- L1602_char(2, 7, ch%10+48);
- }
-
- //U8 keyvalue, keyTH1, keyTH2, keyHH1, keyHH2, keyCH1, keyCH2;
- //鍵盤查詢,在彈起時響應
- if ((Key_TH1)&&(keyTH1==0)) {FlagKeyPress = 1; keyvalue = 1;}
- else if ((Key_TH2)&&(keyTH2==0)) {FlagKeyPress = 1; keyvalue = 2;}
- else if ((Key_HH1)&&(keyHH1==0)) {FlagKeyPress = 1; keyvalue = 3;}
- else if ((Key_HH2)&&(keyHH2==0)) {FlagKeyPress = 1; keyvalue = 4;}
- else if ((Key_CH1)&&(keyCH1==0)) {FlagKeyPress = 1; keyvalue = 5;}
- else if ((Key_CH2)&&(keyCH2==0)) {FlagKeyPress = 1; keyvalue = 6;}
- if (FlagKeyPress == 1)
- {
- KeyProcess(keyvalue);
- FlagKeyPress = 0;
- }
- if (!Key_TH1) keyTH1 = 0;
- else keyTH1 = 1;
- if (!Key_TH2) keyTH2 = 0;
- else keyTH2 = 1;
- if (!Key_HH1) keyHH1 = 0;
- else keyHH1 = 1;
- if (!Key_HH2) keyHH2 = 0;
- else keyHH2 = 1;
-
- if (!Key_CH1) keyCH1 = 0;
- else keyCH1 = 1;
- if (!Key_CH2) keyCH2 = 0;
- else keyCH2 = 1;
-
- }
- }
- //定時器0中斷
- void Timer0_ISR (void) interrupt 1
- {
- TL0 = 0x06;
- TH0 = 0xf8; //定時器賦予初值
- //每2秒鐘啟動一次溫濕度轉換
- RHCounter ++;
- if (RHCounter >= 1000)
- {
- FlagStartRH = 1;
- RHCounter = 0;
- }
- }
- //串口中斷服務函數
- void serial()interrupt 4
- {
- static uchar j;
- ES=0; //禁止中斷
- if(!RI);
- RI=0; //清楚接收完畢標志
- date[j]=SBUF;
- if(date[0]==0xff) //判斷接收到的第一位為傳感器所發數據
- j++;
- else
- j=0;
- if(j==9) //接收傳感器所發數據9字節
- j=0;
- ES=1; //允許串口中斷
- }
復制代碼
0.png (10.12 KB, 下載次數: 78)
下載附件
2019-6-11 02:15 上傳
所有資料51hei提供下載:
51單片機二氧化碳_溫濕度檢測C程序+原理圖+PCB.zip
(1.85 MB, 下載次數: 261)
2019-6-4 08:01 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|