[DHT11] add start condition repetition in case the first one fails (which seems to be common with this device)
This commit is contained in:
parent
a3925be518
commit
df649b3ba5
|
@ -31,29 +31,34 @@ static size_t dht11_read_value(struct DHT11_Data *data)
|
||||||
|
|
||||||
register uint32_t read_register = 0;
|
register uint32_t read_register = 0;
|
||||||
|
|
||||||
TIM2->CNT = 0;
|
|
||||||
HAL_TIM_Base_Start(&htim2);
|
HAL_TIM_Base_Start(&htim2);
|
||||||
|
|
||||||
GPIOD->MODER |= 0x00400000; // enable output mode on GPIOD 11
|
// try to trigger the output
|
||||||
|
for (int t = 0; t < 10; t++) {
|
||||||
|
TIM2->CNT = 0;
|
||||||
|
|
||||||
GPIOD->BSRR = GPIO_PIN_11; // enable DHT11
|
GPIOD->MODER |= 0x00400000; // enable output mode on GPIOD 11
|
||||||
wait(50000); // hold HIGH for 50 ms
|
|
||||||
GPIOD->BSRR = GPIO_PIN_11 << 16; // start signal
|
|
||||||
wait(30000); // hold it for 30 ms
|
|
||||||
GPIOD->BSRR = GPIO_PIN_11; // pull up, DHT will now take control over the connection
|
|
||||||
|
|
||||||
wait(100);
|
GPIOD->BSRR = GPIO_PIN_11; // enable DHT11
|
||||||
GPIOD->MODER &= 0xFF3FFFFF; // switch GPIOD 11 to input mode
|
wait(50000); // hold HIGH for 50 ms
|
||||||
|
GPIOD->BSRR = GPIO_PIN_11 << 16; // start signal
|
||||||
|
wait(30000); // hold it for 30 ms
|
||||||
|
GPIOD->BSRR = GPIO_PIN_11; // pull up, DHT will now take control over the connection
|
||||||
|
|
||||||
// reading pull down from DHT11
|
wait(100);
|
||||||
SKIP_LOW;
|
GPIOD->MODER &= 0xFF3FFFFF; // switch GPIOD 11 to input mode
|
||||||
|
|
||||||
// waiting for 500ms max
|
// reading pull down from DHT11
|
||||||
for (size_t i = TIM2->CNT + (500000 << 4); TIM2->CNT <= i; )
|
SKIP_LOW;
|
||||||
if (!(GPIOD->IDR & 0x0800))
|
|
||||||
goto reading_data;
|
// waiting for response
|
||||||
|
for (size_t i = TIM2->CNT + (200000 << 4); TIM2->CNT <= i; )
|
||||||
|
if (!(GPIOD->IDR & 0x0800))
|
||||||
|
goto reading_data;
|
||||||
|
}
|
||||||
|
|
||||||
// if didn't exit through goto, then DHT11 awaiting has timed out
|
// if didn't exit through goto, then DHT11 awaiting has timed out
|
||||||
|
HAL_TIM_Base_Stop(&htim2);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
reading_data:
|
reading_data:
|
||||||
|
|
Loading…
Reference in New Issue