找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 6174|回复: 2

Arduino下读取DHT22温湿度(不使用第三方库)

[复制链接]

198

主题

13

回帖

1027

积分

管理员

积分
1027
发表于 2020-3-17 12:39:33 | 显示全部楼层 |阅读模式
#include <inttypes.h>
/*
* LED
*/
unsigned int LED = 13;
/*
* DHT22配置程序
*/
unsigned int DHT_PIN = 7;

#define DHT_OK      1
#define DHT_ERR_CHECK 0
#define DHT_ERR_TIMEOUT -1
float humidity;
float temperature;
unsigned char DHT_read()
{
  // BUFFER TO RECEIVE
  unsigned char bits[5] = {0,0,0,0,0};
  unsigned char cnt = 7;
  unsigned char idx = 0;
  unsigned char sum;


  // REQUEST SAMPLE
  pinMode(DHT_PIN, OUTPUT);
  digitalWrite(DHT_PIN, LOW);
  delay(18);
  digitalWrite(DHT_PIN, HIGH);
  delayMicroseconds(40);
  pinMode(DHT_PIN, INPUT);

  // ACKNOWLEDGE or TIMEOUT
  unsigned int count = 10000;
  while(digitalRead(DHT_PIN) == LOW)
    if (count-- == 0) return DHT_ERR_TIMEOUT;

  count = 10000;
  while(digitalRead(DHT_PIN) == HIGH)
    if (count-- == 0) return DHT_ERR_TIMEOUT;

  // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
  for (int i=0; i<40; i++)
  {
    count = 10000;
    while(digitalRead(DHT_PIN) == LOW)
      if (count-- == 0) return DHT_ERR_TIMEOUT;

    unsigned long t = micros();

    count = 10000;
    while(digitalRead(DHT_PIN) == HIGH)
      if (count-- == 0) return DHT_ERR_TIMEOUT;

    if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
    if (cnt == 0)   // next byte?
    {
      cnt = 7;    // restart at MSB
      idx++;      // next byte!
    }
    else cnt--;
  }

  sum = bits[0]+bits[1]+bits[2]+bits[3];
  if(bits[4] != sum) return DHT_ERR_CHECK;


  humidity = (float)((bits[0] << 8)+bits[1])/10;
  temperature = (float)((bits[2] << 8)+bits[3])/10;

  return DHT_OK;
}

void setup() {
   pinMode(13,OUTPUT);//指示灯
   pinMode(DHT_PIN,INPUT);
   digitalWrite(DHT_PIN, HIGH);
}
void loop() {
   DHT_read();
   Serial.print("temperature:");
   Serial.println(temperature);
   Serial.println("============end===============");
   delay(1000);
   digitalWrite(LED,HIGH);
   delay(925); //Delay
   digitalWrite(LED,LOW);
   delay(925); //Delay
}

回复

使用道具 举报

198

主题

13

回帖

1027

积分

管理员

积分
1027
 楼主| 发表于 2020-3-17 12:39:49 | 显示全部楼层
  1. #include <inttypes.h>
  2. /*
  3. * LED
  4. */
  5. unsigned int LED = 13;
  6. /*
  7. * DHT22配置程序
  8. */
  9. unsigned int DHT_PIN = 7;

  10. #define DHT_OK      1
  11. #define DHT_ERR_CHECK 0
  12. #define DHT_ERR_TIMEOUT -1
  13. float humidity;
  14. float temperature;
  15. unsigned char DHT_read()
  16. {
  17.   // BUFFER TO RECEIVE
  18.   unsigned char bits[5] = {0,0,0,0,0};
  19.   unsigned char cnt = 7;
  20.   unsigned char idx = 0;
  21.   unsigned char sum;


  22.   // REQUEST SAMPLE
  23.   pinMode(DHT_PIN, OUTPUT);
  24.   digitalWrite(DHT_PIN, LOW);
  25.   delay(18);
  26.   digitalWrite(DHT_PIN, HIGH);
  27.   delayMicroseconds(40);
  28.   pinMode(DHT_PIN, INPUT);

  29.   // ACKNOWLEDGE or TIMEOUT
  30.   unsigned int count = 10000;
  31.   while(digitalRead(DHT_PIN) == LOW)
  32.     if (count-- == 0) return DHT_ERR_TIMEOUT;

  33.   count = 10000;
  34.   while(digitalRead(DHT_PIN) == HIGH)
  35.     if (count-- == 0) return DHT_ERR_TIMEOUT;

  36.   // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
  37.   for (int i=0; i<40; i++)
  38.   {
  39.     count = 10000;
  40.     while(digitalRead(DHT_PIN) == LOW)
  41.       if (count-- == 0) return DHT_ERR_TIMEOUT;

  42.     unsigned long t = micros();

  43.     count = 10000;
  44.     while(digitalRead(DHT_PIN) == HIGH)
  45.       if (count-- == 0) return DHT_ERR_TIMEOUT;

  46.     if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
  47.     if (cnt == 0)   // next byte?
  48.     {
  49.       cnt = 7;    // restart at MSB
  50.       idx++;      // next byte!
  51.     }
  52.     else cnt--;
  53.   }

  54.   sum = bits[0]+bits[1]+bits[2]+bits[3];
  55.   if(bits[4] != sum) return DHT_ERR_CHECK;
  56.    

  57.   humidity = (float)((bits[0] << 8)+bits[1])/10;
  58.   temperature = (float)((bits[2] << 8)+bits[3])/10;
  59.   
  60.   return DHT_OK;
  61. }

  62. void setup() {
  63.    pinMode(13,OUTPUT);//指示灯
  64.    pinMode(DHT_PIN,INPUT);
  65.    digitalWrite(DHT_PIN, HIGH);
  66. }
  67. void loop() {
  68.    DHT_read();
  69.    Serial.print("temperature:");
  70.    Serial.println(temperature);
  71.    Serial.println("============end===============");
  72.    delay(1000);
  73.    digitalWrite(LED,HIGH);
  74.    delay(925); //Delay
  75.    digitalWrite(LED,LOW);
  76.    delay(925); //Delay
  77. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|小钉锤论坛 粤ICP备18149537号-2

GMT+8, 2024-11-24 12:01 , Processed in 0.051264 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表