Files
Embedded-Hacking/drivers/0x09_dht11_cbm/Src/main.c
T
Kevin Thomas 108f3f5598 feat: add 0x09_dht11_cbm bare-metal DHT11 driver
- Single-wire protocol on GPIO4 via SIO register bit-bang
- TIMER0 TIMERAWL for microsecond pulse-width measurement
- Tick generator configured for 1 us ticks at 12 MHz clk_ref
- UART output: humidity/temperature or wiring failure message
- Zero warnings, 1876 bytes text
2026-04-05 18:30:18 -04:00

111 lines
3.0 KiB
C

/**
******************************************************************************
* @file main.c
* @author Kevin Thomas
* @brief DHT11 temperature and humidity sensor demonstration.
*
* Reads the DHT11 sensor on GPIO4 every 2 seconds, printing
* humidity and temperature over UART. Reports read failures
* for wiring diagnostics.
*
* Wiring:
* GPIO0 -> UART TX (USB-to-UART adapter RX)
* GPIO1 -> UART RX (USB-to-UART adapter TX)
* GPIO4 -> DHT11 DATA (10k pull-up to 3.3V recommended)
* 3.3V -> DHT11 VCC
* GND -> DHT11 GND
*
******************************************************************************
* @attention
*
* Copyright (c) 2026 Kevin Thomas.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
#include "rp2350_dht11.h"
#include "rp2350_uart.h"
#include "rp2350_delay.h"
#include "rp2350_xosc.h"
/**
* @brief Minimum polling interval for the DHT11 sensor in milliseconds.
*/
#define DHT11_POLL_MS 2000U
/**
* @brief Convert an unsigned integer (0-255) to a decimal string.
* @param value number to convert
* @param buf output buffer (at least 4 bytes)
* @retval None
*/
static void _uint_to_str(uint8_t value, char *buf)
{
uint8_t idx = 0;
if (value >= 100) {
buf[idx++] = (char)('0' + value / 100);
buf[idx++] = (char)('0' + (value / 10) % 10);
buf[idx++] = (char)('0' + value % 10);
} else if (value >= 10) {
buf[idx++] = (char)('0' + value / 10);
buf[idx++] = (char)('0' + value % 10);
} else {
buf[idx++] = (char)('0' + value);
}
buf[idx] = '\0';
}
/**
* @brief Print a successful DHT11 reading over UART.
* @param humidity humidity percentage (integer)
* @param temperature temperature in Celsius (integer)
* @retval None
*/
static void _print_reading(uint8_t humidity, uint8_t temperature)
{
char buf[4];
uart_puts("Humidity: ");
_uint_to_str(humidity, buf);
uart_puts(buf);
uart_puts("% Temperature: ");
_uint_to_str(temperature, buf);
uart_puts(buf);
uart_puts(" C\r\n");
}
/**
* @brief Print a read failure message over UART.
* @retval None
*/
static void _print_failure(void)
{
uart_puts("DHT11 read failed - check wiring on GPIO4\r\n");
}
/**
* @brief Application entry point for the DHT11 sensor demo.
* @retval int does not return
*/
int main(void)
{
uint8_t humidity;
uint8_t temperature;
xosc_set_clk_ref();
dht11_timer_release_reset();
dht11_timer_start_tick();
dht11_init();
uart_puts("DHT11 driver initialized on GPIO4\r\n");
while (1) {
if (dht11_read(&humidity, &temperature))
_print_reading(humidity, temperature);
else
_print_failure();
delay_ms(DHT11_POLL_MS);
}
}