diff --git a/.gitignore b/.gitignore index 733208a..52e43f8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ software/adc software/blink software/blink_ws2812 software/button +software/dht11 software/debugConsole software/fade_ws2812 software/hardwarePWM @@ -19,3 +20,6 @@ software/rgb_cycle_ws2812 software/servo software/softPWM software/spi_LTC1448 + +firmware/main.hex +firmware/main.elf diff --git a/Readme.md b/Readme.md index eb7b3bc..f5a42cb 100644 --- a/Readme.md +++ b/Readme.md @@ -2,7 +2,7 @@ For more details: -Current version is: **v1.3** +Current version is: **v1.4** Previous version releases can be found at: diff --git a/firmware/main.c b/firmware/main.c index f1f7a1d..0bffe65 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -64,7 +64,7 @@ #define delayMicroseconds(value) _delay_us(value); #define sbi(register,bit) (register|=(1<= DHT_LEN) { + sendBuffer[byte_index] |= (1 << bit_index); + } + + if (bit_index == 0) { + bit_index = 7; + byte_index++; + } else { + bit_index--; + } + + } + } + + uint16_t checksum = sendBuffer[0] + sendBuffer[1] + sendBuffer[2] + sendBuffer[3]; + if (sendBuffer[4] != (checksum & 0x00FF)) { + return DHT_CHECKSUM_ERR; + } + + if (type == DHT11) { + // Expand DHT11 values to DHT22 format + f = sendBuffer[0]; + f *= 10; + sendBuffer[0] = 0x00FF & f; + sendBuffer[1] = 0x00FF & (f >> 8); + + f = sendBuffer[2]; + f *= 10; + sendBuffer[2] = 0x00FF & f; + sendBuffer[3] = 0x00FF & (f >> 8); + } else if (type == DHT22) { + f = sendBuffer[1]; + sendBuffer[1] = sendBuffer[0]; + sendBuffer[0] = f; + + f = sendBuffer[3]; + sendBuffer[3] = sendBuffer[2]; + sendBuffer[2] = f; + } + return 0; +} // ---------------------------------------------------------------------------- @@ -1382,7 +1496,13 @@ int main(void) { } jobState=0; break; - + case 18: /* dht11/dht22 read */ + sendBuffer[4] = DHT_Read(rxBuffer[0]); // error msg + sendBuffer[8] = 5; + + jobState=0; + break; + default: jobState=0; break; diff --git a/software/Makefile b/software/Makefile index 0108138..5983378 100644 --- a/software/Makefile +++ b/software/Makefile @@ -28,6 +28,7 @@ CFLAGS = $(USBFLAGS) $(LIBS) -I$(INCLUDE) -O -g $(OSFLAG) LWLIBS = littleWire littleWire_util littleWire_servo opendevice EXAMPLES = adc blink blink_ws2812 rgb_cycle_ws2812 fade_ws2812 button servo i2c_blinkM EXAMPLES += spi_LTC1448 onewire softPWM hardwarePWM debugConsole lwbuttond i2c_nunchuck +EXAMPLES += dht11 .PHONY: clean library docs diff --git a/software/examples/dht11.c b/software/examples/dht11.c new file mode 100644 index 0000000..520476d --- /dev/null +++ b/software/examples/dht11.c @@ -0,0 +1,56 @@ +/* + Updated for the new firmware: July 2012 + by ihsan Kehribar + + Created: December 2011 + by ihsan Kehribar +*/ + +#include +#include +#include +#include "littleWire.h" +#include "littleWire_util.h" + +unsigned char version; + +dht_reading val; + +int main(int argc, char **argv) +{ + littleWire *lw = NULL; + + lw = littleWire_connect(); + + if(lw == NULL){ + printf("> Little Wire could not be found!\n"); + exit(EXIT_FAILURE); + } + + version = readFirmwareVersion(lw); + printf("> Little Wire firmware version: %d.%d\n",((version & 0xF0)>>4),(version&0x0F)); + if(version<0x14) + { + printf("> This example requires the new 1.4 version firmware. Please update soon.\n"); + return 0; + } + + while(1) + { + val = dht_read(lw, DHT11); + if (!val.error) { + printf("humidity: %f, temp %f\n", (float)val.humid/10.0, (float)val.temp/10.0); + } else { + printf("Error Reading sensor!"); + } + + if(lwStatus<0) + { + printf("> lwStatus: %d\n",lwStatus); + printf("> Connection error!\n"); + return 0; + } + + delay(5000); + } +} diff --git a/software/library/littleWire.c b/software/library/littleWire.c index aaa9f98..53bee67 100644 --- a/software/library/littleWire.c +++ b/software/library/littleWire.c @@ -389,7 +389,19 @@ void ws2812_preload(littleWire* lwHandle, unsigned char r,unsigned char g,unsign { lwStatus=usb_control_msg(lwHandle, 0xC0, 54, (g<<8) | 0x20, (b<<8) | r, rxBuffer, 8, USB_TIMEOUT); } + +dht_reading dht_read(littleWire* lwHandle, unsigned char type) +{ + lwStatus=usb_control_msg(lwHandle, 0xC0, 56, type, 0, rxBuffer, 8, USB_TIMEOUT); + delay(100); + + lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT); + + return *(dht_reading*) (void*) rxBuffer; +} + + int customMessage(littleWire* lwHandle,unsigned char* receiveBuffer,unsigned char command,unsigned char d1,unsigned char d2, unsigned char d3, unsigned char d4) { int i; diff --git a/software/library/littleWire.h b/software/library/littleWire.h index 255fe3d..0f656f1 100644 --- a/software/library/littleWire.h +++ b/software/library/littleWire.h @@ -36,6 +36,7 @@ #include "opendevice.h" // common code moved to separate module #include "littleWire_util.h" #include +#include #define VENDOR_ID 0x1781 #define PRODUCT_ID 0x0c9f @@ -93,6 +94,15 @@ #define MOSI_PIN PIN4 #define RESET_PIN PIN3 +#define DHT11 0 +#define DHT22 1 + +typedef struct dht_reading { + uint16_t humid; + uint16_t temp; + uint8_t error; +} dht_reading; + extern unsigned char rxBuffer[RX_BUFFER_SIZE]; /* This has to be unsigned for the data's sake */ extern unsigned char ROM_NO[8]; extern int lwStatus; @@ -557,6 +567,7 @@ void ws2812_preload(littleWire* lwHandle, unsigned char r,unsigned char g,unsign /*! @} */ +dht_reading dht_read(littleWire* lwHandle, unsigned char type); /** * @mainpage Introduction