-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
49 lines (37 loc) · 1.11 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include "stm32g4xx_hal.h"
#include "DHT11.h"
UART_HandleTypeDef hlpuart1;
/*IMPORTANT: INSTANCE OF A TIMER TO COUNT 1us*/
TIM_HandleTypeDef htim1;
/*Microcontroller specific functions*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_LPUART1_UART_Init(void);
/*IMPORTANT: FUNCTION TO INITIALIZE A TIMER TO COUNT 1us*/
static void MX_TIM1_Init(void);
int __io_putchar(int ch) {
/*PUT YOUR UART CODE*/
}
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_TIM1_Init(); //initializes the timer
DHT11_InitTypeDef dht;
DHT11_StatusTypeDef err;
HAL_DHT11_Init(&dht, GPIOC, GPIO_PIN_10, &htim1);
while(1) {
HAL_Delay(1000);
err = HAL_DHT11_ReadData(&dht);
if(err != DHT11_OK) {
printf("Failed to read data: %s", HAL_DHT11_GetErrorMsg(err));
continue;
}
printf("T=%.2fC || RH=%.2f%%\n", dht.Temperature, dht.Humidity);
printf("T=%.2fF\n", HAL_DHT11_ReadTemperatureF(&dht));
}
HAL_DHT11_DeInit(&dht); //deinitialses DHT11 driver
return 0;
}