Subversion Repositories group.electronics

Rev

Rev 126 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <avr/io.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

#include "avrutil.h"
#include "dht.h"

void dht_init(uint8_t port, uint8_t pin, uint8_t type, uint8_t count) {
        dht.port = port;
        dht.pin = pin;
        dht.type = type;
        dht.count = count;
        dht.first = 1;

        dht.data = malloc(6 * sizeof(uint8_t));

}


float dht_readTemp() {
        float t = 0.0;

        if (dht_read()) {
                switch (dht.type) {
                        case DHT11:
                                t = dht.data[2];
                                break;
                        case DHT21:
                        case DHT22:
                                t = dht.data[2] & 0x7F;
                                t *= 256;
                                t += dht.data[3];
                                t /= 10;
                                if (dht.data[2] & 0x80)
                                        t *= -1;
                                break;
                }
        }

        return t;
}

uint8_t dht_read() {

        _delay_us(500);

        uint32_t current = systime;

        if (!dht.first && ((current - dht.last) < 2000)) {
                return 1;
        }

        dht.first = 0;
        dht.last = systime;

        return 0;
}