diff --git a/Core/Inc/dht11.h b/Core/Inc/dht11.h new file mode 100644 index 0000000..ec8e5a3 --- /dev/null +++ b/Core/Inc/dht11.h @@ -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 diff --git a/Core/Src/dht11.c b/Core/Src/dht11.c new file mode 100644 index 0000000..52730e2 --- /dev/null +++ b/Core/Src/dht11.c @@ -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); +} diff --git a/Core/Src/main.c b/Core/Src/main.c index 9aaa192..2309746 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -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; diff --git a/Debug/Core/Src/subdir.mk b/Debug/Core/Src/subdir.mk index 7a0fd30..2ae5ea3 100644 --- a/Debug/Core/Src/subdir.mk +++ b/Debug/Core/Src/subdir.mk @@ -5,6 +5,7 @@ # Add inputs and outputs from these tool invocations to the build variables C_SRCS += \ +../Core/Src/dht11.c \ ../Core/Src/external-temperature.c \ ../Core/Src/external_temp.c \ ../Core/Src/lcd.c \ @@ -16,6 +17,7 @@ C_SRCS += \ ../Core/Src/system_stm32f4xx.c OBJS += \ +./Core/Src/dht11.o \ ./Core/Src/external-temperature.o \ ./Core/Src/external_temp.o \ ./Core/Src/lcd.o \ @@ -27,6 +29,7 @@ OBJS += \ ./Core/Src/system_stm32f4xx.o C_DEPS += \ +./Core/Src/dht11.d \ ./Core/Src/external-temperature.d \ ./Core/Src/external_temp.d \ ./Core/Src/lcd.d \ @@ -45,7 +48,7 @@ Core/Src/%.o Core/Src/%.su Core/Src/%.cyclo: ../Core/Src/%.c Core/Src/subdir.mk clean: clean-Core-2f-Src clean-Core-2f-Src: - -$(RM) ./Core/Src/external-temperature.cyclo ./Core/Src/external-temperature.d ./Core/Src/external-temperature.o ./Core/Src/external-temperature.su ./Core/Src/external_temp.cyclo ./Core/Src/external_temp.d ./Core/Src/external_temp.o ./Core/Src/external_temp.su ./Core/Src/lcd.cyclo ./Core/Src/lcd.d ./Core/Src/lcd.o ./Core/Src/lcd.su ./Core/Src/main.cyclo ./Core/Src/main.d ./Core/Src/main.o ./Core/Src/main.su ./Core/Src/stm32f4xx_hal_msp.cyclo ./Core/Src/stm32f4xx_hal_msp.d ./Core/Src/stm32f4xx_hal_msp.o ./Core/Src/stm32f4xx_hal_msp.su ./Core/Src/stm32f4xx_it.cyclo ./Core/Src/stm32f4xx_it.d ./Core/Src/stm32f4xx_it.o ./Core/Src/stm32f4xx_it.su ./Core/Src/syscalls.cyclo ./Core/Src/syscalls.d ./Core/Src/syscalls.o ./Core/Src/syscalls.su ./Core/Src/sysmem.cyclo ./Core/Src/sysmem.d ./Core/Src/sysmem.o ./Core/Src/sysmem.su ./Core/Src/system_stm32f4xx.cyclo ./Core/Src/system_stm32f4xx.d ./Core/Src/system_stm32f4xx.o ./Core/Src/system_stm32f4xx.su + -$(RM) ./Core/Src/dht11.cyclo ./Core/Src/dht11.d ./Core/Src/dht11.o ./Core/Src/dht11.su ./Core/Src/external-temperature.cyclo ./Core/Src/external-temperature.d ./Core/Src/external-temperature.o ./Core/Src/external-temperature.su ./Core/Src/external_temp.cyclo ./Core/Src/external_temp.d ./Core/Src/external_temp.o ./Core/Src/external_temp.su ./Core/Src/lcd.cyclo ./Core/Src/lcd.d ./Core/Src/lcd.o ./Core/Src/lcd.su ./Core/Src/main.cyclo ./Core/Src/main.d ./Core/Src/main.o ./Core/Src/main.su ./Core/Src/stm32f4xx_hal_msp.cyclo ./Core/Src/stm32f4xx_hal_msp.d ./Core/Src/stm32f4xx_hal_msp.o ./Core/Src/stm32f4xx_hal_msp.su ./Core/Src/stm32f4xx_it.cyclo ./Core/Src/stm32f4xx_it.d ./Core/Src/stm32f4xx_it.o ./Core/Src/stm32f4xx_it.su ./Core/Src/syscalls.cyclo ./Core/Src/syscalls.d ./Core/Src/syscalls.o ./Core/Src/syscalls.su ./Core/Src/sysmem.cyclo ./Core/Src/sysmem.d ./Core/Src/sysmem.o ./Core/Src/sysmem.su ./Core/Src/system_stm32f4xx.cyclo ./Core/Src/system_stm32f4xx.d ./Core/Src/system_stm32f4xx.o ./Core/Src/system_stm32f4xx.su .PHONY: clean-Core-2f-Src diff --git a/Debug/objects.list b/Debug/objects.list index 41f9996..5497887 100644 --- a/Debug/objects.list +++ b/Debug/objects.list @@ -1,3 +1,4 @@ +"./Core/Src/dht11.o" "./Core/Src/external-temperature.o" "./Core/Src/external_temp.o" "./Core/Src/lcd.o" diff --git a/test1.ioc b/test1.ioc index 1203522..d386e47 100644 --- a/test1.ioc +++ b/test1.ioc @@ -24,10 +24,11 @@ Mcu.Name=STM32F407V(E-G)Tx Mcu.Package=LQFP100 Mcu.Pin0=PA0-WKUP Mcu.Pin1=PB1 -Mcu.Pin10=PD13 -Mcu.Pin11=PD14 -Mcu.Pin12=PD15 -Mcu.Pin13=VP_SYS_VS_Systick +Mcu.Pin10=PD12 +Mcu.Pin11=PD13 +Mcu.Pin12=PD14 +Mcu.Pin13=PD15 +Mcu.Pin14=VP_SYS_VS_Systick Mcu.Pin2=PE7 Mcu.Pin3=PE10 Mcu.Pin4=PE11 @@ -35,8 +36,8 @@ Mcu.Pin5=PE12 Mcu.Pin6=PE13 Mcu.Pin7=PE14 Mcu.Pin8=PE15 -Mcu.Pin9=PD12 -Mcu.PinsNb=14 +Mcu.Pin9=PD11 +Mcu.PinsNb=15 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F407VGTx @@ -58,6 +59,8 @@ PA0-WKUP.Locked=true PA0-WKUP.Signal=GPXTI0 PB1.Locked=true PB1.Signal=ADCx_IN9 +PD11.Locked=true +PD11.Signal=GPIO_Output PD12.Locked=true PD12.Signal=GPIO_Output PD13.Locked=true @@ -111,7 +114,7 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=true -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_ADC1_Init-ADC1-false-HAL-true RCC.AHBFreq_Value=16000000 RCC.APB1Freq_Value=16000000 RCC.APB1TimFreq_Value=16000000