osd-contiki/platform/osd-merkur/dev/dht11.c

132 lines
4.4 KiB
C
Raw Normal View History

2013-02-19 10:22:48 +01:00
/*
DHT-11 Library
(c) Created by Charalampos Andrianakis on 18/12/11.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <avr/io.h>
#include "contiki.h"
#include "dht11.h"
#include "led.h" // debug
#define udelay(u) clock_delay_usec(u)
#define mdelay(u) clock_delay_msec(u)
2013-11-07 16:34:28 +01:00
// define for DHT11 else for DHT22, RHT03
#define DHT11 1
uint8_t DHT_Read_Data(uint16_t *temperature, uint16_t *humidity){
2013-02-19 10:22:48 +01:00
//data[5] is 8byte table where data come from DHT are stored
//laststate holds laststate value
//counter is used to count microSeconds
uint8_t data[5], laststate = 0, counter = 0, j = 0, i = 0;
//Clear array
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
uint8_t volatile sreg;
sreg = SREG; /* Save status register before disabling interrupts. */
cli(); /* Disable interrupts. */
//Prepare the bus
PIN_INIT();
//Set pin Output
//Pin High
DHT_DRIVE();
2013-11-07 16:34:28 +01:00
// mdelay(250); //Wait for 250mS
mdelay(100); //Wait for 100mS
2013-02-19 10:22:48 +01:00
//Send Request Signal
//Pin Low
2013-11-07 16:34:28 +01:00
OUTP_0(); //20ms Low
#if DHT11
mdelay(20);
#else
udelay(500);
#endif
2013-02-19 10:22:48 +01:00
//Pin High
OUTP_1();
udelay(40); //40us High
//Set pin Input to read Bus
//Set pin Input
DHT_RELEASE();
laststate=DHT_INP(); //Read Pin value
//Repeat for each Transistions
for (i=0; i<MAXTIMINGS; i++) {
//While state is the same count microseconds
while (laststate==DHT_INP()) {
udelay(1);
counter++;
if (counter>254) break;
}
if (counter>254) break;
//laststate==_BV(DHT_PIN) checks if laststate was High
//ignore the first 2 transitions which are the DHT Response
//if (laststate==_BV(DHT_PIN) && (i > 2)) {
if ((i&0x01) && (i > 2)) {
//Save bits in segments of bytes
//Shift data[] value 1 position left
//Example. 01010100 if we shift it left one time it will be
//10101000
2013-04-17 13:44:17 +02:00
// led1_on();
2013-02-19 10:22:48 +01:00
data[j/8]<<=1;
if (counter >= 15) { //If it was high for more than 40uS
2013-04-17 13:44:17 +02:00
// led1_off();
2013-02-19 10:22:48 +01:00
data[j/8]|=1; //it means it is bit '1' so make a logic
2013-04-17 13:44:17 +02:00
// led1_on();
2013-02-19 10:22:48 +01:00
} //OR with the value (save it)
j++; //making an OR by 1 to this value 10101000
2013-04-17 13:44:17 +02:00
// led1_off();
2013-02-19 10:22:48 +01:00
} //we will have the resault 10101001
//1 in 8-bit binary is 00000001
//j/8 changes table record every 8 bits which means a byte has been saved
//so change to next record. 0/8=0 1/8=0 ... 7/8=0 8/8=1 ... 15/8=1 16/8=2
laststate=DHT_INP(); //save current state
counter=0; //reset counter
}
SREG = sreg; /* Enable interrupts. */
//Check if data received are correct by checking the CheckSum
if (data[0] + data[1] + data[2] + data[3] == data[4]) {
2013-11-07 16:34:28 +01:00
#ifdef DHT11
*humidity = data[0];
*temperature = data[2];
#else
*humidity = (uint16_t)data[0]<<8 | data[1];
*temperature = (uint16_t)data[2]<<8 | data[3];
#endif
return 0;
2013-02-19 10:22:48 +01:00
}else{
2013-11-07 16:34:28 +01:00
*humidity = 2;
*temperature = 2;
2013-02-19 10:22:48 +01:00
// uart_puts("\r\nCheck Sum Error");
}
return 0xff; // Check Sum Error
}