DHT22 library, based on Simon Cooksey's. Improved with some error checking and more rigid timing (interrupts off).
Fork of lib_dht22 by
dht22.h@3:40df3c72813f, 2016-11-03 (annotated)
- Committer:
- co657_frmb
- Date:
- Thu Nov 03 11:08:45 2016 +0000
- Revision:
- 3:40df3c72813f
- Parent:
- 1:10ec58346011
- Child:
- 4:30a98da09c59
fixed up some things, still need a touch more tidying.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
co657_sjc80 | 0:257ba13e416e | 1 | /* |
co657_sjc80 | 0:257ba13e416e | 2 | * (C) The University of Kent and Simon Cooksey 2015. |
co657_sjc80 | 0:257ba13e416e | 3 | */ |
co657_sjc80 | 0:257ba13e416e | 4 | |
co657_sjc80 | 0:257ba13e416e | 5 | #ifndef __DHT22_h_ |
co657_sjc80 | 0:257ba13e416e | 6 | #define __DHT22_h_ |
co657_sjc80 | 0:257ba13e416e | 7 | |
co657_sjc80 | 0:257ba13e416e | 8 | // We'll pick a point to decide if a signal is 1 or 0 from. |
co657_frmb | 3:40df3c72813f | 9 | #define DHT22_SIGNAL_HIGH_LOW_BOUNDARY 40 // uS |
co657_frmb | 3:40df3c72813f | 10 | #define DHT22_START_BIT_TIME 1000 // uS |
co657_sjc80 | 0:257ba13e416e | 11 | #define DHT22_START_BIT_RESPONSE 80 // uS |
co657_sjc80 | 0:257ba13e416e | 12 | |
co657_sjc80 | 0:257ba13e416e | 13 | |
co657_sjc80 | 0:257ba13e416e | 14 | typedef struct { |
co657_sjc80 | 0:257ba13e416e | 15 | int temp; |
co657_sjc80 | 0:257ba13e416e | 16 | int humidity; |
co657_sjc80 | 0:257ba13e416e | 17 | uint8_t checksum; |
co657_frmb | 3:40df3c72813f | 18 | char dummy[3]; |
co657_sjc80 | 0:257ba13e416e | 19 | } DHT22_data_t; |
co657_sjc80 | 0:257ba13e416e | 20 | |
co657_sjc80 | 0:257ba13e416e | 21 | class DHT22 { |
co657_sjc80 | 0:257ba13e416e | 22 | public: |
co657_frmb | 3:40df3c72813f | 23 | DHT22 (PinName pin) : dht22_s (pin), debug (PTB19) |
co657_frmb | 3:40df3c72813f | 24 | { |
co657_frmb | 3:40df3c72813f | 25 | dht22_s.input (); |
co657_frmb | 3:40df3c72813f | 26 | isinput = 1; |
co657_frmb | 3:40df3c72813f | 27 | } |
co657_frmb | 3:40df3c72813f | 28 | |
co657_frmb | 3:40df3c72813f | 29 | int read (DHT22_data_t *ptr); |
co657_sjc80 | 0:257ba13e416e | 30 | private: |
co657_sjc80 | 0:257ba13e416e | 31 | DigitalInOut dht22_s; |
co657_frmb | 3:40df3c72813f | 32 | int isinput; |
co657_frmb | 3:40df3c72813f | 33 | DigitalOut debug; |
co657_sjc80 | 0:257ba13e416e | 34 | |
co657_frmb | 3:40df3c72813f | 35 | void wait_2us (void); |
co657_frmb | 3:40df3c72813f | 36 | void setinput (void); |
co657_frmb | 3:40df3c72813f | 37 | void setoutput (void); |
co657_frmb | 3:40df3c72813f | 38 | |
co657_frmb | 3:40df3c72813f | 39 | int wait_for_level (int lvl, const int max); |
co657_frmb | 3:40df3c72813f | 40 | void send_start (void); |
co657_frmb | 3:40df3c72813f | 41 | int wait_start (void); |
co657_frmb | 3:40df3c72813f | 42 | int read_byte (void); |
co657_sjc80 | 0:257ba13e416e | 43 | }; |
co657_sjc80 | 0:257ba13e416e | 44 | |
co657_sjc80 | 0:257ba13e416e | 45 | #endif // __DHT22_h_ |