From 747b1110249589fb2c0b3be5ad13f4a787dcd3fc Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Mon, 15 Dec 2014 01:30:42 +0100 Subject: [PATCH] Moving DHT11 support to a specific external driver. --- extdrv/dht11.c | 134 +++++++++++++++++++++++++++++++++++++++++ include/extdrv/dht11.h | 61 +++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 extdrv/dht11.c create mode 100644 include/extdrv/dht11.h diff --git a/extdrv/dht11.c b/extdrv/dht11.c new file mode 100644 index 0000000..97fb05a --- /dev/null +++ b/extdrv/dht11.c @@ -0,0 +1,134 @@ +/**************************************************************************** + * extdrv/dht11.c + * + * + * + * Copyright 2013 Nathael Pajani + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +/* + * Support for the DHT11 Temperature and Humidity sensor + * Remember that the sensor operates on 3.5 to 5.5V, and that a level shifter must + * be used to interface with the DHT11 sensor. + * + * Note : This support uses a lot of "sleep()" calls, and is thus very ineficient + * and many data conversion can occur (checksum errors, though on all of my tests + * the errors were always on the lower bit, when the temp and humidity values + * were changing, and the scope always reported very stable signals, so I suspect + * that the error is not on the micro-controler side ... ) + * The addition of the "epsilon_error" with a value of 1 removed all errors on my + * side., while still getting coherent values. + * + * DHT11 protocol can be found here : http://embedded-lab.com/blog/?p=4333 + * + */ + +#include +#include "core/lpc_regs_12xx.h" +#include "core/system.h" +#include "core/pio.h" +#include "lib/stdio.h" +#include "drivers/gpio.h" +#include "drivers/serial.h" +#include "extdrv/status_led.h" + + +static struct pio dht11_gpio; +void dht11_config(const struct pio* gpio) +{ + /* Configure as output and set it high. */ + /* This is the "do nothing" state */ + config_gpio(gpio, LPC_IO_MODE_PULL_UP, GPIO_DIR_OUT, 1); + pio_copy(&dht11_gpio, gpio); +} + +static unsigned char dht11_read_dat() +{ + struct lpc_gpio* gpio_port_regs = LPC_GPIO_REGS(dht11_gpio.port); + int i = 0; + unsigned char val = 0; + for (i = 0; i < 8; i++) { + /* Wait end of 'low' */ + while(!(gpio_port_regs->in & (1 << dht11_gpio.pin))); + + /* Wait 35us */ + usleep(35); + + /* read one bit */ + if (gpio_port_regs->in & (1 << dht11_gpio.pin)) { + val |= (1 << (7-i)); + } + + /* Wait end of bit */ + while (gpio_port_regs->in & (1 << dht11_gpio.pin)); + } + return val; +} + +void dht11_display(int serial_num, int epsilon_error) +{ + struct lpc_gpio* gpio_port_regs = LPC_GPIO_REGS(dht11_gpio.port); + unsigned char data[5]; + unsigned char checksum = 0; + char buff[60]; + int len = 0, err = 0, i = 0; + + /* Set pin as output */ + gpio_port_regs->data_dir |= (1 << dht11_gpio.pin); + + /* Send the "start" bit : low pulse of more than 18ms */ + gpio_port_regs->clear = (1 << dht11_gpio.pin); + msleep(25); + gpio_port_regs->set = (1 << dht11_gpio.pin); + + /* Set pin as input */ + gpio_port_regs->data_dir &= ~(1 << dht11_gpio.pin); + + /* Wait for start condition ack : 80us low followed by 80us high. + * Do not care about the durations */ + while (gpio_port_regs->in & (1 << dht11_gpio.pin)); + while (!(gpio_port_regs->in & (1 << dht11_gpio.pin))); + while (gpio_port_regs->in & (1 << dht11_gpio.pin)); + + /* Start reading data : 40 bits */ + for (i = 0; i < 5; i++){ + data[i] = dht11_read_dat(); + if (i < 4) { + checksum += data[i]; + } + } + + if (checksum != data[4]) { + if (epsilon_error == 0) { + err = 1; + } else if ((checksum < (data[4] - epsilon_error)) && (checksum > (data[4] + epsilon_error))) { + err = 1; + } + } + if (err == 1) { + status_led(red_only); + len = snprintf(buff, 60, "TH_ERR - H: 0x%02x,0x%02x - T: 0x%02x,0x%02x - C: 0x%02x\r\n", + data[0], data[1], data[2], data[3], data[4]); + serial_write(serial_num, buff, len); + return; + } + + len = snprintf(buff, 25, "H: %d,%d - T: %d,%d\r\n", data[0], data[1], data[2], data[3]); + serial_write(serial_num, buff, len); + status_led(green_only); +} diff --git a/include/extdrv/dht11.h b/include/extdrv/dht11.h new file mode 100644 index 0000000..b5bdce5 --- /dev/null +++ b/include/extdrv/dht11.h @@ -0,0 +1,61 @@ +/**************************************************************************** + * extdrv/dht11.h + * + * + * + * Copyright 2013 Nathael Pajani + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + *************************************************************************** */ + +#ifndef EXTDRV_DHT11_H +#define EXTDRV_DHT11_H + +/* + * Support for the DHT11 Temperature and Humidity sensor + * Remember that the sensor operates on 3.5 to 5.5V, and that a level shifter must + * be used to interface with the DHT11 sensor. + * + * Note : This support uses a lot of "sleep()" calls, and is thus very ineficient + * and many data conversion can occur (checksum errors, though on all of my tests + * the errors were always on the lower bit, when the temp and humidity values + * were changing, and the scope always reported very stable signals, so I suspect + * that the error is not on the micro-controler side ... ) + * The addition of the "epsilon_error" with a value of 1 removed all errors on my + * side., while still getting coherent values. + * + * DHT11 protocol can be found here : http://embedded-lab.com/blog/?p=4333 + * + */ + +#include + +/***************************************************************************** */ +/* DHT11 Humidity and temp sensor configuration : Set up the GPIO used for + * communication with the DHT11 sensor + */ +void dht11_config(const struct pio* gpio); + +/* Display both temperature and humidity values on the given serial line. + * serial_num is the serial line number (0 or 1) + * epsilon_error is the allowed error on the checksum + */ +void dht11_display(int serial_num, int epsilon_error); + + + +#endif /* EXTDRV_DHT11_H */ + -- 2.43.0