標(biāo)題: LCD1602+DTH11+MQ-135空氣污染檢測(cè) 溫濕度程序 [打印本頁(yè)]

作者: yao123    時(shí)間: 2017-4-6 23:21
標(biāo)題: LCD1602+DTH11+MQ-135空氣污染檢測(cè) 溫濕度程序
1602的溫濕度程序

  1. #include <LiquidCrystal.h>
  2. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  3. int temp;//溫度
  4. int humi;//濕度
  5. int tol;//校對(duì)碼
  6. int j;
  7. unsigned int loopCnt;
  8. int chr[40] = {0};//創(chuàng)建數(shù)字?jǐn)?shù)組,用來(lái)存放40個(gè)bit
  9. unsigned long time;
  10. #define pin 7
  11. int Aout=A0 ;//MQ-135 AO 接 Arduino Uno A0   
  12. int temp1 = 0; //臨時(shí)變量,存儲(chǔ)A0讀取的數(shù)據(jù)   
  13. void setup() {   
  14.   Serial.begin(9600);//定義波特率  
  15.   pinMode(A0, INPUT);//定義A0為INPUT模式
  16.   lcd.begin(16, 2);
  17.   }  
  18.   
  19. void loop() {
  20.   bgn:
  21.   delay(2000);
  22. //設(shè)置2號(hào)接口模式為:輸出
  23. //輸出低電平20ms(>18ms)
  24. //輸出高電平40μs
  25.   pinMode(pin,OUTPUT);
  26.   digitalWrite(pin,LOW);
  27.   delay(20);
  28.   digitalWrite(pin,HIGH);
  29.   delayMicroseconds(40);
  30.   digitalWrite(pin,LOW);
  31. //設(shè)置2號(hào)接口模式:輸入
  32.   pinMode(pin,INPUT);
  33.   //高電平響應(yīng)信號(hào)
  34.   loopCnt=10000;
  35.   while(digitalRead(pin) != HIGH)
  36.   {
  37.     if(loopCnt-- == 0)
  38.     {
  39. //如果長(zhǎng)時(shí)間不返回高電平,輸出個(gè)提示,重頭開(kāi)始。
  40.       Serial.println("HIGH");
  41.       goto bgn;
  42.     }
  43.   }
  44.   //低電平響應(yīng)信號(hào)
  45.   loopCnt=30000;
  46.   while(digitalRead(pin) != LOW)
  47.   {
  48.     if(loopCnt-- == 0)
  49.     {
  50. //如果長(zhǎng)時(shí)間不返回低電平,輸出個(gè)提示,重頭開(kāi)始。
  51.       Serial.println("LOW");
  52.       goto bgn;
  53.     }
  54.   }
  55. //開(kāi)始讀取bit1-40的數(shù)值  
  56.     for(int i=0;i<40;i++)
  57.   {
  58.     while(digitalRead(pin) == LOW)
  59.     {}
  60. //當(dāng)出現(xiàn)高電平時(shí),記下時(shí)間“time”
  61.     time = micros();
  62.     while(digitalRead(pin) == HIGH)
  63.     {}
  64. //當(dāng)出現(xiàn)低電平,記下時(shí)間,再減去剛才儲(chǔ)存的time
  65. //得出的值若大于50μs,則為‘1’,否則為‘0’
  66. //并儲(chǔ)存到數(shù)組里去
  67.     if (micros() - time >50)
  68.     {
  69.       chr[i]=1;
  70.     }
  71.     else{
  72.       chr[i]=0;
  73.     }
  74.   }
  75.    
  76. //濕度,8位的bit,轉(zhuǎn)換為數(shù)值
  77. humi=chr[0]*128+chr[1]*64+chr[2]*32+chr[3]*16+chr[4]*8+chr[5]*4+chr[6]*2+chr[7];
  78.    
  79. //溫度,8位的bit,轉(zhuǎn)換為數(shù)值
  80. temp=chr[16]*128+chr[17]*64+chr[18]*32+chr[19]*16+chr[20]*8+chr[21]*4+chr[22]*2+chr[23];
  81.   //校對(duì)碼,8位的bit,轉(zhuǎn)換為數(shù)值
  82. tol=chr[32]*128+chr[33]*64+chr[34]*32+chr[35]*16+chr[36]*8+chr[37]*4+chr[38]*2+chr[39];
  83. //輸出:溫度、濕度、校對(duì)碼
  84.       lcd.clear ();
  85.       lcd.setCursor(0,0);
  86.        lcd.print("temp:");
  87.        lcd.print(temp);
  88.        lcd.print((char) 0xDF);
  89.        lcd.print("C");
  90.        lcd.setCursor(0,1);
  91.        lcd.print("humidity:");
  92.        lcd.print(humi);
  93.        lcd.print("%");
  94.        delay (5000);
  95.       
  96.   
  97.   
  98.   temp1 = analogRead(Aout); //讀取A0的模擬數(shù)據(jù)
  99.   
  100.   if (temp<500 )
  101.   {
  102.        lcd.clear();
  103.        lcd.setCursor(0,0);
  104.        lcd.print("status:normal");
  105.        lcd.setCursor(0,1);
  106.        lcd.print("potency:");
  107.        lcd.print(temp1);
  108.   
  109.     }
  110.    if (temp1>500)
  111.     {
  112.       

  113. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………

復(fù)制代碼

完整代碼下載:
1602 DTH11 MQ-5.doc (19 KB, 下載次數(shù): 102)


作者: 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