Jump to content

touch

Members
  • Content Count

    107
  • Joined

  • Last visited

  • Days Won

    8

Reputation Activity

  1. Like
    touch got a reaction from Newbie in Reading a DHT11 Temperature and Humidity sensor   
    I picked up a few of these sensors awhile back and wanted to get some code working for them.. Finally got the sensor working.
     
    I'm using a MSP430G2553 clocked @ 8mhz, all of it is done in software, no timers, so if you want to change the clock speed, you will need to change the __delay_cycles and the counter that determines if a bit if 1 or 0 (t). I'll eventually update this to work at other frequencies without having to fool with this stuff.
     
    Sensor is hooked to P1.7 in this code, but can be any unused port on P1, could easily be changed to P2 if needed.
     
    ex output, temp is in c, humidity is in %.
    Temperature: 25
    Humidity: 32
     

    #include #include #define TIMEOUT 1000 int temp,humidity; char read(char pin); void UART_Print(char *string); int main( void ) { WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_8MHZ; DCOCTL = CALDCO_8MHZ; P1SEL = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 0x41; // 8MHz 9600 UART UCA0BR1 = 0x03; // 8MHz 9600 UART UCA0MCTL = UCBRS0; UCA0CTL1 &= ~UCSWRST; IE2 |= UCA0RXIE; while(1){ if(read(BIT7)){ char temp2[20]; char humidity2[20]; sprintf(temp2, "Temperature: %d", temp); sprintf(humidity2, "Humidity: %d", humidity); UART_Print(temp2); UART_Print("\r"); UART_Print(humidity2); UART_Print("\r"); }else{ UART_Print("Error reading DHT11 sensor\r"); } //2 second delay __delay_cycles(36000000); } } char read(char pin) { // bit buffers & timeout char bitcount = 7; char byte = 0; char bits[5] = {0,0,0,0,0}; unsigned int timer; // request reading P1DIR |= pin; P1OUT &= ~pin; __delay_cycles(144000); //18ms delay @ 8mhz sysclk P1OUT |= pin; __delay_cycles(320); //40us delay @ 8mhz sysclk P1DIR &= ~pin; // wait for request reply timer = TIMEOUT; while(!(P1IN&pin)){ if (timer-- == 0){ return 0; } } timer = TIMEOUT; while(P1IN&pin){ if (timer-- == 0) { return 0;} } // start receiving 40 bits char i; for (i=0; i < 40; i++) { timer = TIMEOUT; while(!(P1IN&pin)){ if (timer-- == 0){ return 0; } } timer = TIMEOUT; char t = 0; while(P1IN&pin){ t++; if (timer-- == 0){ return 0; } } if (t > 40) bits[byte] |= (1 << bitcount); t = 0; if (bitcount == 0) { bitcount = 7; byte++; }else{ bitcount--; } } // checksum if((bits[0] + bits[2]) == bits[4]){ temp = bits[2]; humidity = bits[0]; return 1; }else{ return 0; } } void UART_Print(char *string) { while (*string) { while (!(IFG2&UCA0TXIFG)); UCA0TXBUF = *string++; } }
  2. Like
    touch got a reaction from jsolarski in Manchester decoding   
    Here's some code I wrote in a hurry to test out some alarm system modules I got on eBay... If memory serves me right they used some sort of PT2262 encoder (it seemed like a chinese knockoff clone) that generates a 16us sync code pulse and then uses Manchester encoding for the data.
     
    The code's pretty sloppy but it worked for what I was testing and I guess I used the "analog style" that jpnorair was talking about.
    #include <msp430g2553.h> #include <stdio.h> void startTimerA(void){ TAR = 0; TACTL = TASSEL_2 + MC_2; } void stopTimerA(void){ TACTL = TASSEL_2 + MC_0; } void main(void) { //Kill WDT and setup clocks WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; P1DIR |= 0x01; P1IES &= ~BIT4; // P1.4 Hi/lo edge P1IFG &= ~BIT4; // P1.4 IFG cleared P1IE |= BIT4; // P1.4 interrupt enabled P1SEL = BIT1 + BIT2; P1SEL2 = BIT1 + BIT2; UCA0CTL1 |= UCSSEL_2; UCA0BR0 = 104; UCA0BR1 = 0; UCA0MCTL = UCBRS0; UCA0CTL1 &= ~UCSWRST; _BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt } // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { if(P1IFG & BIT4){ int sync = 0; while((P1IN & BIT4)){} startTimerA(); while(!(P1IN & BIT4)){} sync = TAR; stopTimerA(); if(sync > 14500 && sync < 16700){ int j = 0; char bitcnt = 7; char byte = 0; char data[3] = {0,0,0}; for(j=0; j < 24; j++){ int low = 0; int high = 0; startTimerA(); while(P1IN & BIT4){} high = TAR; stopTimerA(); startTimerA(); while(!(P1IN & BIT4)){} low = TAR; stopTimerA(); if(low > 1200 && high > 300){ data[byte] |= (0 << bitcnt); } else if(low > 300 && high > 1200){ data[byte] |= (1 << bitcnt); }else { } if(bitcnt == 0){ bitcnt = 7; byte++; }else{ bitcnt--; } } while (!(IFG2&UCA0TXIFG)); UCA0TXBUF = data[0]; while (!(IFG2&UCA0TXIFG)); UCA0TXBUF = data[1]; if(data[0] == 0xFD && data[1] == 0x4F){ P1OUT ^= 0x01; } if(data[0] == 0xFF && data[1] == 0xF5){ P1OUT ^= 0x01; } } P1IFG &= ~0x10; } }
  3. Like
    touch got a reaction from abecedarian in Total "food stamp" recipients exceed population of 24 states combined   
    While it's true raising the minimum wage does cause price increases, in the last few years the prices have increased drastically while the amount of money people are making has not. Adjusted for inflation people are making less now than they were in the 70's and yet many companies are having record profits (that go back to the investors) while they raise the price of goods and do not pay their employees anymore.
  4. Like
    touch got a reaction from SugarAddict in The 43oh Store will be closing temporarily   
    I've had a bit of experience with this, I worked on a project where we wrote and designed a custom storefront/inventory/shipping system. I would certainly look into label printers that can print customs/postage & flat rate envelopes & boxes. You probably already know this but if you plan on doing this software and organization is the key, its also not a bad idea to speak with a accountant/lawyer about opening a LLC or a C-Corp for it, that way you can get a Paypal, bank account, etc in the business name and don't have to use your personal accounts anymore and this simplifies tax related stuff as well.
     
    Hope you get it sorted out!
  5. Like
    touch got a reaction from bluehash in The 43oh Store will be closing temporarily   
    I've had a bit of experience with this, I worked on a project where we wrote and designed a custom storefront/inventory/shipping system. I would certainly look into label printers that can print customs/postage & flat rate envelopes & boxes. You probably already know this but if you plan on doing this software and organization is the key, its also not a bad idea to speak with a accountant/lawyer about opening a LLC or a C-Corp for it, that way you can get a Paypal, bank account, etc in the business name and don't have to use your personal accounts anymore and this simplifies tax related stuff as well.
     
    Hope you get it sorted out!
  6. Like
    touch reacted to RobG in [Group Buy-11] [C] RGB LED Strips (with WS2811)   
    I got extra so yes, you can have 2m.
     
  7. Like
    touch reacted to cubeberg in 32x8 Yellow LED Matrix   
    Saw this post on DP - http://dangerousprototypes.com/2012/10/ ... n-arduino/
    about a board to drive four 8x8 LED matrices.
     
    Looks like lcdsmartie has a Yellow version of the board on eBay for $6.11. Only two left, but it's a pretty good deal.
     
    http://www.ebay.com/itm/160871706290
  8. Like
    touch got a reaction from RobG in Solutions to drive a 8x8 bi-color CA LED Matrix   
    I saw the TLC59213, however I was trying to keep the display either SPI or I2C driven. I think the best solution is like you said digital transistors.
     
    I could also use a single chip solution like the HT1632C ($2 in single qtys). A strange thing I noticed is that it does PWM control but also requires current limiting resistors on the LED's.
  9. Like
    touch got a reaction from larsie in Garage door opener   
    Hows the range on the modules? I only get like 25 feet max with these CC2500 modules. I've been trying to find a suitable PA/LNA to boost the signal a few dB. None of the manufacturers of them will send me samples though .
     
    I noticed you wanted to make a mailbox monitor.. I've made something similar using one of these: http://www.ebay.com/itm/Wireless-Door-Window-Magnet-Sensor-Detector-For-Alarm-System-433-MHz-/271033425757?pt=LH_DefaultDomain_0&hash=item3f1ad9df5d
     
    They use a PT2262 OOK encoder, I just used a receiver like this one http://www.ebay.com/itm/433Mhz-RF-transmitter-and-receiver-link-kit-for-Arduino-ARM-MCU-voltage-3v-12v-/300733295714?pt=LH_DefaultDomain_0&hash=item460519ec62 and did the decoding in software.
     
    One time lightning blew out the remote receiver for my garage door, damn thing cost $50, you made a nice one for a couple bucks, good work!
     
    I wish I documented my projects better myself, I don't really document anything so half the time I end up redoing stuff I've already done :oops:
  10. Like
    touch reacted to rbasoalto in Garage door opener   
    Very nice!
    Maybe you can have a look at http://en.wikipedia.org/wiki/HOTP for security. It's the kind of cryptographically-secure unpredictable sequence generators that banks use for two-factor authentication. Google's two-factor authentication uses it too.
     
    The idea is that you apply an HMAC function (some kind of hash involving a private key) to a counter. Both transmitter and receiver need to store this counter. Since both will usually be in sync, the HMAC received can be compared to the expected value, based on the shared private key, and the counter. If it fails, it can also try a few counts ahead too, in case the button was pressed accidentally out of the receiver's range.
     
    Although banks and Google use a time-based algorithm, it's essentially the same, but using the count of 30-second ticks instead of an event counter. I think I've seen a version for the Chronos that is compatible with Google Authenticator. Should be trivial to adapt.
     
    Since you will need to store a counter, FRAM would be ideal for zero-power-standby. But you can also use flash, or just have the MSP sleeping in LPM4 the whole time and wake it, with an interrupt from the button, for transmission (at a few uA it shouldn't be noticeable).
    In this last case, you could use a three-way handshake, much like TCP/IP sockets, for keeping the counters in sync. Something like

    [*:3rhtxy43]Remote: HEY, the current code is 123456.
    [*:3rhtxy43]Receiver: OK, please come in.
    [*:3rhtxy43]Remote: Thanks! Increment the counter! (I will increment it too)
     
    I could continue giving ideas for centuries, it's a very interesting topic.
     
    Cheers! I hope you're not locked out!
  11. Like
    touch reacted to JWoodrell in SPI 4 digit 7 segment displays   
    in answering the question of whether there are decimal LED's buried in there somewhere I can conclusively say no there are not.
     
    I sacrificed one to the gods of tearing stuff apart and found there are neither LED's or driving circuit pads for them, just the 4 7 segment characters and the two colon points.
     
    it was interesting extricating the circuit board without breaking it (although the LED's get destroyed when you split the two halves apart)

  12. Like
    touch reacted to RobG in SPI 4 digit 7 segment displays   
    OK, got mine, they are green with red colon, not sure what BLUE + RED mean (maybe that's why they ended up with the surplus guy )
    In any case, thanks for finding them touch, they are awesome.
     
    Here's my test code

    #include void sendData(unsigned char data, unsigned char hold); const char segments[16] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x58, 0x5e, 0x79, 0x71 }; void main(void) { WDTCTL = WDTPW + WDTHOLD; // stop WDT P1OUT |= BIT0 + BIT5 + BIT6; P1DIR |= BIT0 + BIT5 + BIT6; sendData(0x02, 0); sendData(0x40, 0); sendData(0x8F, 0); char i = 0; char colon = 1; while (1) { for (i = 0; i < 16; i++) { colon ^= 0x01; sendData(0xC0, 1); sendData(segments[i] + (colon ? BIT7 : 0), 0); sendData(0xC2, 1); sendData(segments[i] + (colon ? BIT7 : 0), 0); sendData(0xC4, 1); sendData(segments[i], 0); sendData(0xC6, 1); sendData(segments[i], 0); _delay_cycles(500000); } } } void sendData(unsigned char data, unsigned char hold) { char c = 0; P1OUT &= ~BIT0; while (c < 8) { (data & 0x01) ? (P1OUT |= BIT6) : (P1OUT &= ~BIT6); data >>= 1; P1OUT &= ~BIT5; P1OUT |= BIT5; c++; } if (!hold) { P1OUT |= BIT0; } }
     

  13. Like
    touch got a reaction from bluehash in SPI 4 digit 7 segment displays   
    From what I can tell so far the decimal points aren't hooked up at all. :cry:
     
    It seems that way, green digits with a red colon...very odd.
     
    I noticed they have another set of displays here:
    http://www.ebay.com/itm/10-New-Oasis-4- ... 2ec116e572
     
    Seems they are the same P/N though. I'm almost tempted to buy another 10... No idea what I'd use them for but I've never seen any displays like these with the driver chip built in, let alone at this price...
     
    Here's what it looks like after I mapped out the segments.

  14. Like
    touch reacted to JWoodrell in SPI 4 digit 7 segment displays   
    http://www.oasistek.com.tw/en/pdf_downl ... 65CB-B.pdf
     
    Here is the datasheet for the Led display itself, but they dont have anything on their site about the "d" version with the driver chip, so it may be custom for someone... Hope this helps
  15. Like
    touch got a reaction from oPossum in SPI 4 digit 7 segment displays   
    I found these on eBay and bought some:
    http://www.ebay.com/itm/NEW-Lot-10-Oasi ... 0807998835
     
    They are 7 segment displays with the PT6961 driver chip built in. Datasheet here: http://www.princeton.com.tw/downloadpro ... PT6961.pdf
     
    I cant seem to find anything out about the Oasis display, it appears to be pretty old with a date code of 0726, I'd assume the 26th week of 07. I did figure out the pinout of the display though. IC requires between 3.3 and 5v so I assume it will work with the launchpad.
     
    Hope to get this working soon, I'll update when I do...
  16. Like
    touch got a reaction from RobG in SPI 4 digit 7 segment displays   
    I found these on eBay and bought some:
    http://www.ebay.com/itm/NEW-Lot-10-Oasi ... 0807998835
     
    They are 7 segment displays with the PT6961 driver chip built in. Datasheet here: http://www.princeton.com.tw/downloadpro ... PT6961.pdf
     
    I cant seem to find anything out about the Oasis display, it appears to be pretty old with a date code of 0726, I'd assume the 26th week of 07. I did figure out the pinout of the display though. IC requires between 3.3 and 5v so I assume it will work with the launchpad.
     
    Hope to get this working soon, I'll update when I do...
  17. Like
    touch got a reaction from thomasss in Convert 2 analog signals into 1 with the msp430   
    I think you could use something like the CD4066 to multiplex-scan the analog signal. the problem is it would half the horizontal resolution of your ADC I think. Take it with a grain of salt though, I'm not 100% sure.
  18. Like
    touch got a reaction from mekanoo in Reading a DHT11 Temperature and Humidity sensor   
    I picked up a few of these sensors awhile back and wanted to get some code working for them.. Finally got the sensor working.
     
    I'm using a MSP430G2553 clocked @ 8mhz, all of it is done in software, no timers, so if you want to change the clock speed, you will need to change the __delay_cycles and the counter that determines if a bit if 1 or 0 (t). I'll eventually update this to work at other frequencies without having to fool with this stuff.
     
    Sensor is hooked to P1.7 in this code, but can be any unused port on P1, could easily be changed to P2 if needed.
     
    ex output, temp is in c, humidity is in %.
    Temperature: 25
    Humidity: 32
     

    #include #include #define TIMEOUT 1000 int temp,humidity; char read(char pin); void UART_Print(char *string); int main( void ) { WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_8MHZ; DCOCTL = CALDCO_8MHZ; P1SEL = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 0x41; // 8MHz 9600 UART UCA0BR1 = 0x03; // 8MHz 9600 UART UCA0MCTL = UCBRS0; UCA0CTL1 &= ~UCSWRST; IE2 |= UCA0RXIE; while(1){ if(read(BIT7)){ char temp2[20]; char humidity2[20]; sprintf(temp2, "Temperature: %d", temp); sprintf(humidity2, "Humidity: %d", humidity); UART_Print(temp2); UART_Print("\r"); UART_Print(humidity2); UART_Print("\r"); }else{ UART_Print("Error reading DHT11 sensor\r"); } //2 second delay __delay_cycles(36000000); } } char read(char pin) { // bit buffers & timeout char bitcount = 7; char byte = 0; char bits[5] = {0,0,0,0,0}; unsigned int timer; // request reading P1DIR |= pin; P1OUT &= ~pin; __delay_cycles(144000); //18ms delay @ 8mhz sysclk P1OUT |= pin; __delay_cycles(320); //40us delay @ 8mhz sysclk P1DIR &= ~pin; // wait for request reply timer = TIMEOUT; while(!(P1IN&pin)){ if (timer-- == 0){ return 0; } } timer = TIMEOUT; while(P1IN&pin){ if (timer-- == 0) { return 0;} } // start receiving 40 bits char i; for (i=0; i < 40; i++) { timer = TIMEOUT; while(!(P1IN&pin)){ if (timer-- == 0){ return 0; } } timer = TIMEOUT; char t = 0; while(P1IN&pin){ t++; if (timer-- == 0){ return 0; } } if (t > 40) bits[byte] |= (1 << bitcount); t = 0; if (bitcount == 0) { bitcount = 7; byte++; }else{ bitcount--; } } // checksum if((bits[0] + bits[2]) == bits[4]){ temp = bits[2]; humidity = bits[0]; return 1; }else{ return 0; } } void UART_Print(char *string) { while (*string) { while (!(IFG2&UCA0TXIFG)); UCA0TXBUF = *string++; } }
  19. Like
    touch got a reaction from RobG in Group Buy request- GPS module for $15-20   
    I've been looking into cheap GPS modules, here's what I've found:
     
    I found these on mouser: http://www.mouser.com/ProductDetail/Mae ... 52bnFHEn8X
     
    I bought a cheap bluetooth module here http://www.ebay.com/itm/New-TomTom-MKII ... 2826026906
     
    Taken apart: http://i.imgur.com/vHQPq.jpg
     
    Found out you could hold CTL low and read the serial output where its fed into the bluetooth module, the only problem is I tried to take the bluetooth module off and well, it didn't work after that, but its a cheap way to get GPS and a case with battery and charger, if only I could get that bluetooth module off and replace it with a MSP430 board in its place and still have the GPS module work. I guess you can re-program the bluetooth modules firmware to do other things but I didnt try it, from factory the bluetooth module acts as a wireless serial port.
     
    Anyways, I'd be in for 1 if we can get them for under 15 & they include antenna.
  20. Like
    touch got a reaction from GeekDoc in Group Buy request- GPS module for $15-20   
    I've been looking into cheap GPS modules, here's what I've found:
     
    I found these on mouser: http://www.mouser.com/ProductDetail/Mae ... 52bnFHEn8X
     
    I bought a cheap bluetooth module here http://www.ebay.com/itm/New-TomTom-MKII ... 2826026906
     
    Taken apart: http://i.imgur.com/vHQPq.jpg
     
    Found out you could hold CTL low and read the serial output where its fed into the bluetooth module, the only problem is I tried to take the bluetooth module off and well, it didn't work after that, but its a cheap way to get GPS and a case with battery and charger, if only I could get that bluetooth module off and replace it with a MSP430 board in its place and still have the GPS module work. I guess you can re-program the bluetooth modules firmware to do other things but I didnt try it, from factory the bluetooth module acts as a wireless serial port.
     
    Anyways, I'd be in for 1 if we can get them for under 15 & they include antenna.
  21. Like
    touch got a reaction from oPossum in Group Buy request- GPS module for $15-20   
    I've been looking into cheap GPS modules, here's what I've found:
     
    I found these on mouser: http://www.mouser.com/ProductDetail/Mae ... 52bnFHEn8X
     
    I bought a cheap bluetooth module here http://www.ebay.com/itm/New-TomTom-MKII ... 2826026906
     
    Taken apart: http://i.imgur.com/vHQPq.jpg
     
    Found out you could hold CTL low and read the serial output where its fed into the bluetooth module, the only problem is I tried to take the bluetooth module off and well, it didn't work after that, but its a cheap way to get GPS and a case with battery and charger, if only I could get that bluetooth module off and replace it with a MSP430 board in its place and still have the GPS module work. I guess you can re-program the bluetooth modules firmware to do other things but I didnt try it, from factory the bluetooth module acts as a wireless serial port.
     
    Anyways, I'd be in for 1 if we can get them for under 15 & they include antenna.
  22. Like
    touch got a reaction from bluehash in Group Buy request- GPS module for $15-20   
    I've been looking into cheap GPS modules, here's what I've found:
     
    I found these on mouser: http://www.mouser.com/ProductDetail/Mae ... 52bnFHEn8X
     
    I bought a cheap bluetooth module here http://www.ebay.com/itm/New-TomTom-MKII ... 2826026906
     
    Taken apart: http://i.imgur.com/vHQPq.jpg
     
    Found out you could hold CTL low and read the serial output where its fed into the bluetooth module, the only problem is I tried to take the bluetooth module off and well, it didn't work after that, but its a cheap way to get GPS and a case with battery and charger, if only I could get that bluetooth module off and replace it with a MSP430 board in its place and still have the GPS module work. I guess you can re-program the bluetooth modules firmware to do other things but I didnt try it, from factory the bluetooth module acts as a wireless serial port.
     
    Anyways, I'd be in for 1 if we can get them for under 15 & they include antenna.
  23. Like
    touch reacted to oPossum in Strange problem with keypad matrix.   
    You must have one diode per row to prevent shorting the micrcontroller outputs.
  24. Like
    touch reacted to oPossum in Interfacing with DHT11 Humidty + Temp sensor   
    I got the DHT11 sensors from SunTek today and wrote some C code to read it. This uses TimerA for accurate time measurement, and polling to allow use on any pin. The internal pullup resistor is used, so an external one is not needed.

    A six byte array passed to read_dht() will be filled with the start bit, 4 data bytes, and the checksum. Return is 0 for success, or a negative value for failure. This code can run at 1 MHz or higher.

    The DHT11 seems to need about 300 ms between readings, less time will result in the sensor sometimes being non-responsive. The fractional bytes are always 0 for the DHT11.

     
    int read_dht(unsigned char *p) { // Note: TimerA must be continuous mode (MC_2) at 1 MHz const unsigned b = BIT4; // I/O bit const unsigned char *end = p + 6; // End of data buffer register unsigned char m = 1; // First byte will have only start bit register unsigned st, et; // Start and end times // p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = 0; // Clear data buffer // P1OUT &= ~b; // Pull low P1DIR |= b; // Output P1REN &= ~b; // Drive low st = TAR; while((TAR - st) < 18000); // Wait 18 ms P1REN |= b; // Pull low P1OUT |= b; // Pull high P1DIR &= ~b; // Input // st = TAR; // Get start time for timeout while(P1IN & if((TAR - st) > 100) return -1; // Wait while high, return if no response et = TAR; // Get start time for timeout do { // st = et; // Start time of this bit is end time of previous bit while(!(P1IN & ) if((TAR - st) > 100) return -2; // Wait while low, return if stuck low while(P1IN & if((TAR - st) > 200) return -3; // Wait while high, return if stuck high et = TAR; // Get end time if((et - st) > 110) *p |= m; // If time > 110 us, then it is a one bit if(!(m >>= 1)) m = 0x80, ++p; // Shift mask, move to next byte when mask is zero } while(p < end); // Do until array is full // p -= 6; // Point to start of buffer if(p[0] != 1) return -4; // No start bit if(((p[1] + p[2] + p[3] + p[4]) & 0xFF) != p[5]) return -5; // Bad checksum // return 0; // Good read }
  25. Like
    touch got a reaction from dacoffey in Reading a DHT11 Temperature and Humidity sensor   
    I picked up a few of these sensors awhile back and wanted to get some code working for them.. Finally got the sensor working.
     
    I'm using a MSP430G2553 clocked @ 8mhz, all of it is done in software, no timers, so if you want to change the clock speed, you will need to change the __delay_cycles and the counter that determines if a bit if 1 or 0 (t). I'll eventually update this to work at other frequencies without having to fool with this stuff.
     
    Sensor is hooked to P1.7 in this code, but can be any unused port on P1, could easily be changed to P2 if needed.
     
    ex output, temp is in c, humidity is in %.
    Temperature: 25
    Humidity: 32
     

    #include #include #define TIMEOUT 1000 int temp,humidity; char read(char pin); void UART_Print(char *string); int main( void ) { WDTCTL = WDTPW + WDTHOLD; BCSCTL1 = CALBC1_8MHZ; DCOCTL = CALDCO_8MHZ; P1SEL = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD P1SEL2 = BIT1 + BIT2; // P1.1 = RXD, P1.2=TXD UCA0CTL1 |= UCSSEL_2; // SMCLK UCA0BR0 = 0x41; // 8MHz 9600 UART UCA0BR1 = 0x03; // 8MHz 9600 UART UCA0MCTL = UCBRS0; UCA0CTL1 &= ~UCSWRST; IE2 |= UCA0RXIE; while(1){ if(read(BIT7)){ char temp2[20]; char humidity2[20]; sprintf(temp2, "Temperature: %d", temp); sprintf(humidity2, "Humidity: %d", humidity); UART_Print(temp2); UART_Print("\r"); UART_Print(humidity2); UART_Print("\r"); }else{ UART_Print("Error reading DHT11 sensor\r"); } //2 second delay __delay_cycles(36000000); } } char read(char pin) { // bit buffers & timeout char bitcount = 7; char byte = 0; char bits[5] = {0,0,0,0,0}; unsigned int timer; // request reading P1DIR |= pin; P1OUT &= ~pin; __delay_cycles(144000); //18ms delay @ 8mhz sysclk P1OUT |= pin; __delay_cycles(320); //40us delay @ 8mhz sysclk P1DIR &= ~pin; // wait for request reply timer = TIMEOUT; while(!(P1IN&pin)){ if (timer-- == 0){ return 0; } } timer = TIMEOUT; while(P1IN&pin){ if (timer-- == 0) { return 0;} } // start receiving 40 bits char i; for (i=0; i < 40; i++) { timer = TIMEOUT; while(!(P1IN&pin)){ if (timer-- == 0){ return 0; } } timer = TIMEOUT; char t = 0; while(P1IN&pin){ t++; if (timer-- == 0){ return 0; } } if (t > 40) bits[byte] |= (1 << bitcount); t = 0; if (bitcount == 0) { bitcount = 7; byte++; }else{ bitcount--; } } // checksum if((bits[0] + bits[2]) == bits[4]){ temp = bits[2]; humidity = bits[0]; return 1; }else{ return 0; } } void UART_Print(char *string) { while (*string) { while (!(IFG2&UCA0TXIFG)); UCA0TXBUF = *string++; } }
×
×
  • Create New...