標(biāo)題:
LCD1602+DTH11+MQ-135空氣污染檢測(cè) 溫濕度程序
[打印本頁(yè)]
作者:
yao123
時(shí)間:
2017-4-6 23:21
標(biāo)題:
LCD1602+DTH11+MQ-135空氣污染檢測(cè) 溫濕度程序
1602的溫濕度程序
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int temp;//溫度
int humi;//濕度
int tol;//校對(duì)碼
int j;
unsigned int loopCnt;
int chr[40] = {0};//創(chuàng)建數(shù)字?jǐn)?shù)組,用來(lái)存放40個(gè)bit
unsigned long time;
#define pin 7
int Aout=A0 ;//MQ-135 AO 接 Arduino Uno A0
int temp1 = 0; //臨時(shí)變量,存儲(chǔ)A0讀取的數(shù)據(jù)
void setup() {
Serial.begin(9600);//定義波特率
pinMode(A0, INPUT);//定義A0為INPUT模式
lcd.begin(16, 2);
}
void loop() {
bgn:
delay(2000);
//設(shè)置2號(hào)接口模式為:輸出
//輸出低電平20ms(>18ms)
//輸出高電平40μs
pinMode(pin,OUTPUT);
digitalWrite(pin,LOW);
delay(20);
digitalWrite(pin,HIGH);
delayMicroseconds(40);
digitalWrite(pin,LOW);
//設(shè)置2號(hào)接口模式:輸入
pinMode(pin,INPUT);
//高電平響應(yīng)信號(hào)
loopCnt=10000;
while(digitalRead(pin) != HIGH)
{
if(loopCnt-- == 0)
{
//如果長(zhǎng)時(shí)間不返回高電平,輸出個(gè)提示,重頭開(kāi)始。
Serial.println("HIGH");
goto bgn;
}
}
//低電平響應(yīng)信號(hào)
loopCnt=30000;
while(digitalRead(pin) != LOW)
{
if(loopCnt-- == 0)
{
//如果長(zhǎng)時(shí)間不返回低電平,輸出個(gè)提示,重頭開(kāi)始。
Serial.println("LOW");
goto bgn;
}
}
//開(kāi)始讀取bit1-40的數(shù)值
for(int i=0;i<40;i++)
{
while(digitalRead(pin) == LOW)
{}
//當(dāng)出現(xiàn)高電平時(shí),記下時(shí)間“time”
time = micros();
while(digitalRead(pin) == HIGH)
{}
//當(dāng)出現(xiàn)低電平,記下時(shí)間,再減去剛才儲(chǔ)存的time
//得出的值若大于50μs,則為‘1’,否則為‘0’
//并儲(chǔ)存到數(shù)組里去
if (micros() - time >50)
{
chr[i]=1;
}
else{
chr[i]=0;
}
}
//濕度,8位的bit,轉(zhuǎn)換為數(shù)值
humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
//溫度,8位的bit,轉(zhuǎn)換為數(shù)值
temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
//校對(duì)碼,8位的bit,轉(zhuǎn)換為數(shù)值
tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
//輸出:溫度、濕度、校對(duì)碼
lcd.clear ();
lcd.setCursor(0,0);
lcd.print("temp:");
lcd.print(temp);
lcd.print((char) 0xDF);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("humidity:");
lcd.print(humi);
lcd.print("%");
delay (5000);
temp1 = analogRead(Aout); //讀取A0的模擬數(shù)據(jù)
if (temp<500 )
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("status:normal");
lcd.setCursor(0,1);
lcd.print("potency:");
lcd.print(temp1);
}
if (temp1>500)
{
…………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
完整代碼下載:
1602 DTH11 MQ-5.doc
(19 KB, 下載次數(shù): 102)
2017-4-6 23:20 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
daiqiangqin
時(shí)間:
2017-4-7 13:46
應(yīng)該發(fā)表在ARDUINO版,這個(gè)是51版。
作者:
lucq7896
時(shí)間:
2017-4-7 17:05
當(dāng)51程序下的
作者:
6010010
時(shí)間:
2020-8-20 10:40
有原理圖嗎
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1