DHT22 library, based on Simon Cooksey's. Improved with some error checking and more rigid timing (interrupts off).

Fork of lib_dht22 by Jodie Perry

Committer:
co657_sjc80
Date:
Mon Jan 04 14:45:33 2016 +0000
Revision:
0:257ba13e416e
Child:
1:10ec58346011
Initial commit;

Who changed what in which revision?

UserRevisionLine numberNew 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_sjc80 0:257ba13e416e 9 #define DHT22_SIGNAL_HIGH_LOW_BOUNDARY 50 // uS
co657_sjc80 0:257ba13e416e 10 #define DHT22_START_BIT_TIME 500 // uS
co657_sjc80 0:257ba13e416e 11 #define DHT22_START_BIT_RESPONSE 80 // uS
co657_sjc80 0:257ba13e416e 12
co657_sjc80 0:257ba13e416e 13 typedef enum {
co657_sjc80 0:257ba13e416e 14 EDGE_TYPE_FALLING,
co657_sjc80 0:257ba13e416e 15 EDGE_TYPE_RISING,
co657_sjc80 0:257ba13e416e 16 } edge_type_t;
co657_sjc80 0:257ba13e416e 17
co657_sjc80 0:257ba13e416e 18 typedef struct {
co657_sjc80 0:257ba13e416e 19 int temp;
co657_sjc80 0:257ba13e416e 20 int humidity;
co657_sjc80 0:257ba13e416e 21 uint8_t checksum;
co657_sjc80 0:257ba13e416e 22 } DHT22_data_t;
co657_sjc80 0:257ba13e416e 23
co657_sjc80 0:257ba13e416e 24 class DHT22 {
co657_sjc80 0:257ba13e416e 25 public:
co657_sjc80 0:257ba13e416e 26 DHT22(PinName pin) : dht22_s(pin) { }
co657_sjc80 0:257ba13e416e 27 void read(DHT22_data_t * data);
co657_sjc80 0:257ba13e416e 28 private:
co657_sjc80 0:257ba13e416e 29 DigitalInOut dht22_s;
co657_sjc80 0:257ba13e416e 30 Timer dht22_t;
co657_sjc80 0:257ba13e416e 31
co657_sjc80 0:257ba13e416e 32 int wait_for_edge(edge_type_t type);
co657_sjc80 0:257ba13e416e 33 void send_start();
co657_sjc80 0:257ba13e416e 34 void await_start_response();
co657_sjc80 0:257ba13e416e 35 int16_t read_word();
co657_sjc80 0:257ba13e416e 36 uint8_t read_checksum();
co657_sjc80 0:257ba13e416e 37 };
co657_sjc80 0:257ba13e416e 38
co657_sjc80 0:257ba13e416e 39 #endif // __DHT22_h_