add untested driver prototype for DHT11 humidity and temperature sensor

This commit is contained in:
2025-02-20 01:02:11 +02:00
parent a2c5b2fe87
commit 5b4d65d45c
6 changed files with 152 additions and 11 deletions

30
Core/Inc/dht11.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef __DHT11
#define __DHT11
#define SKIP_LOW while (!(GPIOD->IDR & 0x0800)) {}
#define SKIP_HIGH while (GPIOD->IDR & 0x0800) {}
#define FILL_REGISTER do { \
for (int i = 0; i < 8; i++) { \
read_register <<= 1; \
SKIP_LOW; \
read_register |= dht11_measure_high_duration(); \
} } while (0)
struct SysTickConfig {
uint32_t ctrl;
uint32_t load;
uint32_t val;
};
struct DHT11_Data {
uint8_t humid_integral;
uint8_t humid_decimal;
uint8_t temp_integral;
uint8_t temp_decimal;
uint8_t crc;
};
void dht11_show_both(void);
#endif

98
Core/Src/dht11.c Normal file
View File

@@ -0,0 +1,98 @@
#include "main.h"
#include "generic_macros.h"
#include "dht11.h"
static struct SysTickConfig stc;
static uint32_t *systick_base = (uint32_t *) SysTick_BASE;
static void wait(uint32_t wait_us)
{
int target_time = *(systick_base+8);
target_time -= wait_us << 4;
while (*(systick_base+8) > target_time) {}
}
static void dht11_save_systick_state(void)
{
stc.ctrl = *(systick_base + 0);
stc.load = *(systick_base + 4);
stc.val = *(systick_base + 8);
}
static void dht11_load_systick_state(void)
{
*(systick_base + 4) = stc.load;
*(systick_base + 8) = stc.val;
*(systick_base + 0) = stc.ctrl;
}
static void dht11_init_systick(void)
{
dht11_save_systick_state();
*(systick_base + 0) = 0x5; // disable /8 prescaler, no interrupts, enable counting
*(systick_base + 4) = 0xFFFFFF; // load largest possible value
// verify value load
if (*(systick_base + 8) == 0)
PANIC(0x4000);
}
static size_t dht11_measure_high_duration(void)
{
int current_time = *(systick_base+8);
SKIP_HIGH;
// elapsed_time > 49us ? 1 : 0
return (current_time - *(systick_base+8)) > (49 << 4);
}
static void dht11_read_value(struct DHT11_Data *data)
{
dht11_init_systick();
register uint8_t read_register = 0;
GPIOD->BSRR = GPIO_PIN_11; // enable DHT11
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
// switch GPIOD 11 to input mode
GPIOD->MODER &= 0xFF3FFFFF;
// read transmission start sequence from DHT11
SKIP_HIGH;
SKIP_LOW;
SKIP_HIGH;
// read humidity integral
FILL_REGISTER;
data->humid_integral = read_register;
// read humidity decimal
FILL_REGISTER;
data->humid_decimal = read_register;
// read temperature integral
FILL_REGISTER;
data->temp_integral = read_register;
// read temperature decimal
FILL_REGISTER;
data->temp_decimal = read_register;
// read crc
FILL_REGISTER;
data->crc = read_register;
dht11_load_systick_state();
}
void dht11_show_both(void)
{
struct DHT11_Data data;
dht11_read_value(&data);
}

View File

@@ -25,6 +25,7 @@
#include "generic_macros.h"
#include "lcd.h"
#include "external_temp.h"
#include "dht11.h"
/* USER CODE END Includes */
@@ -49,6 +50,7 @@ ADC_HandleTypeDef hadc1;
/* USER CODE BEGIN PV */
void ((*executors[])(void)) = {
dht11_show_both,
external_temp_show_celsius,
external_temp_show_fahrenheit
};
@@ -104,6 +106,7 @@ int main(void)
GPIOD->ODR = 0xF000;
display_init();
HAL_Delay(1000);
GPIOD->ODR = 0x0000;
/* USER CODE END 2 */
@@ -236,7 +239,8 @@ static void MX_GPIO_Init(void)
|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
@@ -253,8 +257,10 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pins : PD12 PD13 PD14 PD15 */
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
/*Configure GPIO pins : PD11 PD12 PD13 PD14
PD15 */
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;