touch 34 Posted February 23, 2012 Share Posted February 23, 2012 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++; } } dacoffey, bluehash, mekanoo and 1 other 4 Quote Link to post Share on other sites
xKaux 0 Posted April 30, 2014 Share Posted April 30, 2014 I have problem with the program, i don't know how to see the valor of the sensor. I need a program like hiperterminal? My problem is how i can see the valor? Quote Link to post Share on other sites
Newbie 0 Posted January 19, 2015 Share Posted January 19, 2015 I have problem with the program, i don't know how to see the valor of the sensor. I need a program like hiperterminal? My problem is how i can see the valor? I dont know whether is my problem or not. But I am not able to view it through putty or hyperterminal via UART. Sometimes I can see, and sometimes is not. So, I actually added a LCD to view the values and yes, the codes is working fine. Thanks touch. Quote Link to post Share on other sites
Newbie 0 Posted January 19, 2015 Share Posted January 19, 2015 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 <msp430g2553.h> #include <stdio.h> #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++; } } Hi, Touch, thanks for the code, it works. But how can I implement it in 1M Hz. I know how to calculate the appropriate delay cycle for the timing. But how to determine the value of counter? TIMEOUT? in order for it to work in 1MHz? And I also have another problem. I tried by using BIT7, yeah it work perfectly. But when I change to other BIT, be it BIT0, 1,2,3,4,5, or 6, all are not working. Just BIT7 is working. I was scratching my head when this problem pops up and this shouldnt be happened in theory lol. It is really appreciated if you can give some comment Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.