mirror of
https://github.com/mytechnotalent/Embedded-Hacking.git
synced 2026-08-15 15:40:19 +02:00
Updated Drivers
This commit is contained in:
@@ -45,24 +45,37 @@
|
||||
|
||||
#define DHT11_GPIO 4
|
||||
|
||||
|
||||
/**
|
||||
* @brief Read the DHT11 sensor and print the result over UART
|
||||
*
|
||||
* Attempts a single read. On success prints humidity and temperature;
|
||||
* on failure prints a wiring-check message. Waits 2 s before returning
|
||||
* to respect the DHT11 minimum polling interval.
|
||||
*/
|
||||
static void _print_reading(void) {
|
||||
float humidity = 0.0f;
|
||||
float temperature = 0.0f;
|
||||
if (dht11_read(&humidity, &temperature))
|
||||
printf("Humidity: %.1f%% Temperature: %.1f C\r\n", humidity, temperature);
|
||||
else
|
||||
printf("DHT11 read failed - check wiring on GPIO %d\r\n", DHT11_GPIO);
|
||||
sleep_ms(2000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Application entry point for the DHT11 sensor demo
|
||||
*
|
||||
* Initializes the DHT11 on GPIO4 and continuously reads temperature
|
||||
* and humidity, printing results over UART every 2 seconds.
|
||||
*
|
||||
* @return int Does not return
|
||||
*/
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
dht11_init(DHT11_GPIO);
|
||||
|
||||
printf("DHT11 driver initialized on GPIO %d\r\n", DHT11_GPIO);
|
||||
|
||||
while (true) {
|
||||
float humidity = 0.0f;
|
||||
float temperature = 0.0f;
|
||||
|
||||
if (dht11_read(&humidity, &temperature)) {
|
||||
printf("Humidity: %.1f%% Temperature: %.1f C\r\n",
|
||||
humidity, temperature);
|
||||
} else {
|
||||
printf("DHT11 read failed - check wiring on GPIO %d\r\n",
|
||||
DHT11_GPIO);
|
||||
}
|
||||
|
||||
sleep_ms(2000);
|
||||
}
|
||||
while (true)
|
||||
_print_reading();
|
||||
}
|
||||
|
||||
+100
-32
@@ -33,46 +33,114 @@
|
||||
|
||||
static uint dht_pin;
|
||||
|
||||
void dht11_init(uint8_t pin) {
|
||||
dht_pin = pin;
|
||||
gpio_init(pin);
|
||||
gpio_pull_up(pin);
|
||||
}
|
||||
|
||||
bool dht11_read(float *humidity, float *temperature) {
|
||||
uint8_t data[5] = {0};
|
||||
|
||||
// Start signal
|
||||
/**
|
||||
* @brief Send the DHT11 start signal on the data pin
|
||||
*
|
||||
* Drives the pin LOW for 18 ms then HIGH for 40 us before switching
|
||||
* the pin to input mode to listen for the sensor response.
|
||||
*/
|
||||
static void _send_start_signal(void) {
|
||||
gpio_set_dir(dht_pin, GPIO_OUT);
|
||||
gpio_put(dht_pin, 0);
|
||||
sleep_ms(18);
|
||||
gpio_put(dht_pin, 1);
|
||||
sleep_us(40);
|
||||
gpio_set_dir(dht_pin, GPIO_IN);
|
||||
|
||||
// Wait for response
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Wait for the pin to leave a given logic level
|
||||
*
|
||||
* Spins until the pin no longer reads the specified level, returning
|
||||
* false if a timeout of 10 000 iterations is exceeded.
|
||||
*
|
||||
* @param level Logic level to wait through (0 or 1)
|
||||
* @return bool true once the level changed, false on timeout
|
||||
*/
|
||||
static bool _wait_for_level(int level) {
|
||||
uint32_t timeout = 10000;
|
||||
while (gpio_get(dht_pin) == 1) if (--timeout == 0) return false;
|
||||
timeout = 10000;
|
||||
while (gpio_get(dht_pin) == 0) if (--timeout == 0) return false;
|
||||
timeout = 10000;
|
||||
while (gpio_get(dht_pin) == 1) if (--timeout == 0) return false;
|
||||
|
||||
// Read 40 bits
|
||||
for (int i = 0; i < 40; i++) {
|
||||
timeout = 10000;
|
||||
while (gpio_get(dht_pin) == 0) if (--timeout == 0) return false;
|
||||
uint32_t start = time_us_32();
|
||||
timeout = 10000;
|
||||
while (gpio_get(dht_pin) == 1) if (--timeout == 0) return false;
|
||||
uint32_t duration = time_us_32() - start;
|
||||
data[i / 8] <<= 1;
|
||||
if (duration > 40) data[i / 8] |= 1;
|
||||
}
|
||||
|
||||
// Check checksum
|
||||
if (data[4] != ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) return false;
|
||||
|
||||
while (gpio_get(dht_pin) == level)
|
||||
if (--timeout == 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Wait for the DHT11 response after the start signal
|
||||
*
|
||||
* The sensor pulls LOW then HIGH then LOW again; each transition
|
||||
* is awaited with a timeout.
|
||||
*
|
||||
* @return bool true if the full response was received, false on timeout
|
||||
*/
|
||||
static bool _wait_response(void) {
|
||||
if (!_wait_for_level(1)) return false;
|
||||
if (!_wait_for_level(0)) return false;
|
||||
if (!_wait_for_level(1)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Read a single bit from the DHT11 data stream
|
||||
*
|
||||
* Waits for the low-period to end, measures the high-period duration,
|
||||
* and shifts the result into the appropriate byte of the data array.
|
||||
*
|
||||
* @param data 5-byte array accumulating the received bits
|
||||
* @param i Bit index (0-39)
|
||||
* @return bool true on success, false on timeout
|
||||
*/
|
||||
static bool _read_bit(uint8_t *data, int i) {
|
||||
if (!_wait_for_level(0)) return false;
|
||||
uint32_t start = time_us_32();
|
||||
if (!_wait_for_level(1)) return false;
|
||||
uint32_t duration = time_us_32() - start;
|
||||
data[i / 8] <<= 1;
|
||||
if (duration > 40) data[i / 8] |= 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Read all 40 data bits from the DHT11
|
||||
*
|
||||
* @param data 5-byte array filled with the received data
|
||||
* @return bool true if all 40 bits were read, false on timeout
|
||||
*/
|
||||
static bool _read_40_bits(uint8_t *data) {
|
||||
for (int i = 0; i < 40; i++)
|
||||
if (!_read_bit(data, i)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Verify the DHT11 checksum byte
|
||||
*
|
||||
* @param data 5-byte received data (bytes 0-3 plus checksum in byte 4)
|
||||
* @return bool true if the checksum matches, false otherwise
|
||||
*/
|
||||
static bool _validate_checksum(const uint8_t *data) {
|
||||
return data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF);
|
||||
}
|
||||
|
||||
|
||||
void dht11_init(uint8_t pin) {
|
||||
dht_pin = pin;
|
||||
gpio_init(pin);
|
||||
gpio_pull_up(pin);
|
||||
}
|
||||
|
||||
|
||||
bool dht11_read(float *humidity, float *temperature) {
|
||||
uint8_t data[5] = {0};
|
||||
_send_start_signal();
|
||||
if (!_wait_response()) return false;
|
||||
if (!_read_40_bits(data)) return false;
|
||||
if (!_validate_checksum(data)) return false;
|
||||
*humidity = data[0] + data[1] * 0.1f;
|
||||
*temperature = data[2] + data[3] * 0.1f;
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user