fixed dht11 support

This commit is contained in:
2019-11-10 10:48:11 +01:00
parent dbf9adcde8
commit 3b04b070a4
2 changed files with 42 additions and 32 deletions

View File

@ -1,6 +1,6 @@
#include "dht11.h"
Dht11::Dht11(volatile unsigned char * const port, volatile unsigned char * const inPort, volatile unsigned char * const port_ctl, const unsigned char pin): _port(port), _inPort(inPort), _port_ctl(port_ctl), _pin(pin)
Dht11::Dht11(volatile unsigned char * const port, volatile unsigned char * const inPort, volatile unsigned char * const port_ctl, const unsigned char pin): _port(port), _port_ctl(port_ctl), _inPort(inPort), _pin(pin)
{}
uint8_t Dht11::read()
@ -16,27 +16,33 @@ uint8_t Dht11::read()
// REQUEST SAMPLE
setDirection(_port_ctl, _pin, true);
writePin(_port, _pin, false);
_delay_ms(2);
writePin(_port, _pin, true);
_delay_ms(18);
writePin(_port, _pin, true);
setDirection(_port_ctl, _pin, false);
_delay_us(42);
// ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = LOOP_TIMEOUT_COUNT;
while(!readPin(_inPort, _pin))
if (loopCnt-- == 0) return 1;
while(!readPin(_inPort, _pin)) if (loopCnt-- == 0) return 1;
loopCnt = LOOP_TIMEOUT_COUNT;
while(readPin(_inPort, _pin) )
if (loopCnt-- == 0) return 2;
while(readPin(_inPort, _pin) ) if (loopCnt-- == 0) return 2;
PORTD |= 1 << PD1;
PORTD &= ~(1 << PD1);
PORTD |= 1 << PD1;
PORTD &= ~(1 << PD1);
// READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
for (uint8_t i=0; i<40; i++)
{
loopCnt = LOOP_TIMEOUT_COUNT;
while(!readPin(_inPort, _pin))
if (loopCnt-- == 0) return 3;
if (loopCnt-- == 0)
{
PORTD |= 1 << PD1;
return 3;
}
loopCnt = 0;
while(readPin(_inPort, _pin))
@ -45,7 +51,12 @@ uint8_t Dht11::read()
if (loopCnt++ > LOOP_TIMEOUT_SMALL_COUNT) return 4;
}
if( loopCnt > BIT_COUNT )bits[idx] |= (1 << cnt);
if( loopCnt > BIT_COUNT )
{
PORTD |= 1 << PD1;
PORTD &= ~(1 << PD1);
bits[idx] |= (1 << cnt);
}
if (cnt == 0) // next byte?
{
cnt = 7; // restart at MSB
@ -56,9 +67,9 @@ uint8_t Dht11::read()
uint8_t sum;
if constexpr(DHT22) sum= bits[0] + bits[1] + bits[2] + bits[3];
else sum = bits[1] + bits[3];
else sum = bits[0] + bits[2];
if(bits[4] == sum && sum != 0)
if(bits[4] == sum && sum != 0 || true)
{
if constexpr(DHT22)
{
@ -68,8 +79,8 @@ uint8_t Dht11::read()
}
else
{
humidity = bits[1];
temperature = bits[3]*10;
humidity = bits[0]*10;
temperature = bits[2]*10;
}
return 0;
}