找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1689|回复: 0
收起左侧

arduino 温湿度传感器dht11程序

[复制链接]
ID:806626 发表于 2020-7-22 10:13 | 显示全部楼层 |阅读模式
  1. //
  2. //    FILE: dht22.cpp
  3. // VERSION: 0.1.00
  4. // PURPOSE: DHT22 Temperature & Humidity Sensor library for Arduino
  5. //
  6. // DATASHEET:
  7. //
  8. // HISTORY:
  9. // 0.1.0 by Rob Tillaart (01/04/2011)
  10. // inspired by DHT11 library
  11. //

  12. #include "dht.h"

  13. #define TIMEOUT 10000

  14. /////////////////////////////////////////////////////
  15. //
  16. // PUBLIC
  17. //


  18. // return values:
  19. //  0 : OK
  20. // -1 : checksum error
  21. // -2 : timeout
  22. int dht::read11(uint8_t pin)
  23. {
  24.         // READ VALUES
  25.         int rv = read(pin);
  26.         if (rv != 0) return rv;

  27.         // CONVERT AND STORE
  28.         humidity    = bits[0];  // bit[1] == 0;
  29.         temperature = bits[2];  // bits[3] == 0;

  30.         // TEST CHECKSUM
  31.         uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0
  32.         if (bits[4] != sum) return -1;

  33.         return 0;
  34. }

  35. // return values:
  36. //  0 : OK
  37. // -1 : checksum error
  38. // -2 : timeout
  39. int dht::read22(uint8_t pin)
  40. {
  41.         // READ VALUES
  42.         int rv = read(pin);
  43.         if (rv != 0) return rv;

  44.         // CONVERT AND STORE
  45.         humidity    = word(bits[0], bits[1]) * 0.1;

  46.         int sign = 1;
  47.         if (bits[2] & 0x80) // negative temperature
  48.         {
  49.                 bits[2] = bits[2] & 0x7F;
  50.                 sign = -1;
  51.         }
  52.         temperature = sign * word(bits[2], bits[3]) * 0.1;


  53.         // TEST CHECKSUM
  54.         uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
  55.         if (bits[4] != sum) return -1;

  56.         return 0;
  57. }

  58. /////////////////////////////////////////////////////
  59. //
  60. // PRIVATE
  61. //

  62. // return values:
  63. //  0 : OK
  64. // -2 : timeout
  65. int dht::read(uint8_t pin)
  66. {
  67.         // INIT BUFFERVAR TO RECEIVE DATA
  68.         uint8_t cnt = 7;
  69.         uint8_t idx = 0;

  70.         // EMPTY BUFFER
  71.         for (int i=0; i< 5; i++) bits[i] = 0;

  72.         // REQUEST SAMPLE
  73.         pinMode(pin, OUTPUT);
  74.         digitalWrite(pin, LOW);
  75.         delay(20);
  76.         digitalWrite(pin, HIGH);
  77.         delayMicroseconds(40);
  78.         pinMode(pin, INPUT);

  79.         // GET ACKNOWLEDGE or TIMEOUT
  80.         unsigned int loopCnt = TIMEOUT;
  81.         while(digitalRead(pin) == LOW)
  82.                 if (loopCnt-- == 0) return -2;

  83.         loopCnt = TIMEOUT;
  84.         while(digitalRead(pin) == HIGH)
  85.                 if (loopCnt-- == 0) return -2;

  86.         // READ THE OUTPUT - 40 BITS => 5 BYTES
  87.         for (int i=0; i<40; i++)
  88.         {
  89.                 loopCnt = TIMEOUT;
  90.                 while(digitalRead(pin) == LOW)
  91.                         if (loopCnt-- == 0) return -2;

  92.                 unsigned long t = micros();

  93.                 loopCnt = TIMEOUT;
  94.                 while(digitalRead(pin) == HIGH)
  95.                         if (loopCnt-- == 0) return -2;

  96.                 if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
  97.                 if (cnt == 0)   // next byte?
  98.                 {
  99.                         cnt = 7;   
  100.                         idx++;      
  101.                 }
  102.                 else cnt--;
  103.         }

  104.         return 0;
  105. }
  106. //
  107. // END OF FILE
  108. //
复制代码


回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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