Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
126 pfowler 1
#include <avr/io.h>
2
#include <stdlib.h>
3
#include <avr/pgmspace.h>
4
#include <util/delay.h>
5
 
6
#include "avrutil.h"
7
#include "dht.h"
8
 
9
void dht_init(uint8_t port, uint8_t pin, uint8_t type, uint8_t count) {
10
	dht.port = port;
11
	dht.pin = pin;
12
	dht.type = type;
13
	dht.count = count;
14
	dht.first = 1;
15
 
16
	dht.data = malloc(6 * sizeof(uint8_t));
17
 
18
}
19
 
20
 
21
float dht_readTemp() {
22
	float t = 0.0;
23
 
24
	if (dht_read()) {
25
		switch (dht.type) {
26
			case DHT11:
27
				t = dht.data[2];
28
				break;
29
			case DHT21:
30
			case DHT22:
31
				t = dht.data[2] & 0x7F;
32
				t *= 256;
33
				t += dht.data[3];
34
				t /= 10;
35
				if (dht.data[2] & 0x80)
36
					t *= -1;
37
				break;
38
		}
39
	}
40
 
41
	return t;
42
}
43
 
44
uint8_t dht_read() {
45
 
46
	_delay_us(500);
47
 
48
	uint32_t current = systime;
49
 
50
	if (!dht.first && ((current - dht.last) < 2000)) {
51
		return 1;
52
	}
53
 
54
	dht.first = 0;
55
	dht.last = systime;
56
 
57
	return 0;
58
}