Jump to content
43oh

mekanoo

Members
  • Content Count

    7
  • Joined

  • Last visited

Reputation Activity

  1. Like
    mekanoo reacted to oPossum in Interfacing with DHT11 Humidty + Temp sensor   
    This code will display the DHT11 hex data, and humidity and temperature in decimal.
    It uses software UART so it will work on all versions of the Launchpad.
    DHT11 is connect to P1.4



     
    #include <msp430.h> #include <stdlib.h> volatile unsigned txdata = 0; #pragma vector = TIMER0_A0_VECTOR __interrupt void Timer0_A0(void) { if(txdata) { (txdata & 1) ? (CCTL0 &= ~OUTMOD2) : (CCTL0 |= OUTMOD2); txdata >>= 1; CCR0 += 104; } else { CCTL0 &= ~CCIE; } } void putc(const char c) { while(CCTL0 & CCIE); txdata = 0x0200 | (c << 1); CCR0 = TAR + 16; CCTL0 = OUTMOD0 | CCIE; } void print(const char *s) { while(*s) putc(*s++); } void print_hex_digit(unsigned n) { putc("0123456789ABCDEF"[n & 0x0F]); } void print_hex_byte(unsigned n) { print_hex_digit(n >> 4); print_hex_digit(n); } void print_dec2(int n) { // Print 2 decimal digits 0 to 99 const div_t d = div(n, 10); if(d.quot) putc('0' + d.quot); putc('0' + d.rem); } 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 } void main(void) { unsigned char dht[6]; // Data from DHT11 int err; // unsigned n; // // WDTCTL = WDTPW | WDTHOLD; // Disable watchdog reset DCOCTL = 0; // Run at 1 MHz BCSCTL1 = CALBC1_1MHZ; // DCOCTL = CALDCO_1MHZ; // CCTL0 = OUT; // Setup serial tx I/O P1SEL = BIT1; // P1DIR = BIT1; // TACTL = TASSEL_2 | MC_2; // TimerA SMCLK, continuous __enable_interrupt(); // // for(; { // for-ever while(CCTL0 & CCIE); // Wait for any serial tx to finish err = read_dht(dht); // Read DHT11 if(err) { // If error... print("Error -"); // putc('0' - err); // print("\r\n"); // } else { // No error... for(n = 0; n < sizeof(dht); ++n) { // Show hex data print_hex_byte(dht[n]); // putc(' '); // } // print(" "); // print_dec2(dht[1]); // Show humimidy print(" RH "); // print_dec2(dht[3]); // Show temperature print(" C\r\n"); // } // __delay_cycles(1000000); // Wait a while } // }  
     
  2. Like
    mekanoo reacted to touch 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++; } }
  3. Like
    mekanoo reacted to oPossum in Read two DS1820 sensors from MSP430 usb serial port   
    Digital Picture Frame Thermometer
     
    It sends temperature in C and F over serial port. Just strip out the picture frame control code.
×
×
  • Create New...