Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ****************************************************************************
- * *
- * Name : DHT11 Temperature and humidity digital sensor *
- * (cheap version of Sensirion SHT11) *
- * Author : saper_2 / nolin11 *
- * Contact : nolinno11@gmail.com *
- * Date : 2012.08.28 *
- * Version : 0.1 *
- * License : You are free to use this library in any kind project, but at *
- * least send me a info that YOU are using it. I'll be happy *
- * for memory :) *
- * About : *
- * What to write? Name gives what is this :) *
- * *
- * Bugs : nothing so far... *
- * *
- **************************************************************************** */
- #include <avr/io.h>
- #include <inttypes.h>
- #include <avr/pgmspace.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- //#include <avr/power.h>
- //#include <avr/eeprom.h>
- //#include <math.h>
- #include "dht11.h"
- #include "delay.h"
- #define DHT_DEBUG 0
- #if (DHT_DEBUG == 1)
- #include "usart.h"
- #endif
- void dht11_init(void) {
- // pull-up, input
- dht_data_in;
- // configure interrupt
- #if (DHT_DATA_INT_SOURCE == DHT_DATA_INT_SOURCE_NOT_USED)
- // nothing to do :)
- #elif (DHT_DATA_INT_SOURCE == DHT_DATA_INT_SOURCE_INT0)
- #elif (DHT_DATA_INT_SOURCE == DHT_DATA_INT_SOURCE_INT1)
- #else
- #error "Interrupt souce not supported!"
- #endif
- }
- uint8_t dht11_read(uint8_t* data) {
- //uint8_t dht11_read(void) {
- uint8_t tout=0, tmp=0, csm=0;
- // reset structure
- for (uint8_t i=0;i<4;i++) *(data+i)=0;
- *(data+4)=0xaa;
- // ---- start ----
- // pull down data for at least 18ms
- dht_data_lo;
- delay1ms(25);
- // pull up and wait about 40us
- dht_data_hi;
- tout=0;
- while (dht_get_data > 0) {
- tout++;
- delay1us(1);
- if (tout>100) return DHT11_READ_ERROR_START; // return error-start
- }
- tout=0;
- // now dht11 pulls down data for at least 80us
- while (dht_get_data < 1) {
- tout++;
- delay1us(1);
- if (tout>120) return DHT11_READ_ERROR_ACK; // return error-ack start
- }
- // now dht release data line for at least 80us...
- tout=0;
- while (dht_get_data > 0) {
- tout++;
- delay1us(1);
- if (tout>120) return DHT11_READ_ERROR_ACK2; // return error-ack start
- }
- // ---- data , 8bit from MSB to LSB: RH integral, RH decimal, TEMP integral, TEMP decimal, CSM (sum of all 4 bytes)
- // ---- each data bit starts with 50us data pulled down and then released, release time determinates 0 or 1 in rule:
- // ---- 26-28us is 0 , ~70us is "1"
- #if (DHT_DEBUG != 0)
- uint8_t dta[5] = {0,0,0,0,0};
- uint8_t touts1[40] = {0,0,0,0,0};
- uint8_t touts2[40] = {0,0,0,0,0};
- uint8_t touts_cnt=0;
- #endif
- for (uint8_t j=0;j<5;j++) {
- tmp=0;
- for (uint8_t i=0;i<8;i++) {
- tmp <<= 1;
- // pull down - 50us
- tout=0;
- while (dht_get_data < 1) {
- tout++;
- delay1us(1);
- if (tout>120) return DHT11_READ_ERROR_TIME; // return error-ack start
- }
- #if (DHT_DEBUG != 0)
- touts1[touts_cnt] = tout;
- #endif
- // pull up: ~26-28us=0 , ~70us=1
- tout=0;
- while (dht_get_data > 0) {
- tout++;
- delay1us(1);
- if (tout>150) return DHT11_READ_ERROR_TIME2; // return error-ack start
- }
- #if (DHT_DEBUG != 0)
- touts2[touts_cnt] = tout;
- #endif
- // 0
- if ( (tout > 3) && (tout < 10) ) {
- }
- // 1
- if ( tout > 12 ) {
- tmp |= 0x01;
- }
- #if (DHT_DEBUG != 0)
- touts_cnt++;
- #endif
- }
- #if (DHT_DEBUG != 0)
- dta[j]=tmp;
- #endif
- *(data+j) = tmp;
- // do not include into checksum checksum :)
- if (j < 4) csm = csm + tmp;
- }
- #if (DHT_DEBUG != 0)
- usartSendStrP((prog_char*)PSTR("\r\n\r\n"));
- usartSendStrP((prog_char*)PSTR("DATA[0..4]="));
- for (uint8_t j=0;j<5;j++) {
- usartSendHexByte(dta[j]);
- usartSendChar(' ');
- }
- usartSendStrP((prog_char*)PSTR(" = DEC{ "));
- for (uint8_t j=0;j<5;j++) {
- usartSendDec(dta[j]);
- usartSendChar(',');
- usartSendChar(' ');
- }
- usartSendStrP((prog_char*)PSTR("}"));
- usartSendStrP((prog_char*)PSTR("\r\n" "TOUT_LOW_BIT counters: "));
- for (uint8_t j=0;j<40;j++) {
- if (touts1[j] < 10) usartSendChar('0');
- usartSendDec(touts1[j]);
- usartSendChar(' ');
- }
- usartSendStrP((prog_char*)PSTR("\r\n" "TOUT_HIGH_BIT counters: "));
- for (uint8_t j=0;j<40;j++) {
- if (touts2[j] < 10) usartSendChar('0');
- usartSendDec(touts2[j]);
- usartSendChar(' ');
- }
- usartSendStrP((prog_char*)PSTR("\r\n"));
- #endif
- if (*(data+4) == csm) return DHT11_READ_OK;
- return DHT11_READ_ERROR_CSM;
- //return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement