📜 ⬆️ ⬇️

MSP430 LaunchPad and DHT11

Hello!
I recently acquired the MSP430 LaunchPad and began studying the documentation in the hope of ever using it in everyday life. In this post I will describe getting humidity and temperature from a DHT11 sensor.


The documentation for the DHT11 turned out to be everything you need to connect and write the program.

Wiring diagram

In my case, the sensor was soldered on a scarf and the resistance of the pull-up data line to a high level of 10 kOhm, and not 5 kOhm, as in the documentation.

Diagram explaining the beginning of the survey of the sensor

To request the current data, you need to pull the data line to a low level and hold for 18 ms. After some time, the sensor will indicate that it is ready to transfer data by pulling the data line to a low level of 80µs. Next, 40 bits of data are transmitted with the most significant bit forward. Bits are encoded by pulse duration.
')
Pulse diagram corresponding to 0


Pulse diagram of the corresponding 1


The transfer is completed by pulling the data line sensor to a low level of 50 µs.

The program is written in C in Code Composer Studio v5 and is built as follows, when you press a button on the debug board, the button interrupt is disabled, the leg to which the data line is connected (in my case P2.5) is configured as an output, 0 is fed to it and 20 ms timer By interrupting the timer, the foot is configured as an input, the interrupt is enabled when the signal level on it changes from 1 to 0 and the timer starts. The interruption of this timer during overflow (65 ms) is used to complete the reading process, and the samples for measuring the duration between signal level drops from 1 to 0. In the array entry interruption processing procedure, the timer value is recorded equal to the time interval between signal level 1 to 0 and the timer restarts. In the first element of the array, a number that does not make sense, in the second period from the beginning of the sensor response to the start of data transfer, in the remaining 40 intervals the corresponding 0 (from 76 to 78 μs, in my case turned out to be less than 70) and 1 (120 μs It also turned out to be a little less). When the level drops end the interrupt timer overflow is triggered. For this interrupt, disable interrupts at the input, disable the timer, convert time intervals to information bits (1 byte - humidity, 2 - 0, 3 - temperature, 4 - 0, 5 - checksum, must be equal to the sum of the first 4 bytes), transmit on the UART and enable button interruption. In the terminal on the PC we see about the following

Serial port COM2 opened CheckSum=Ok RH=36 T=28 CheckSum=Ok RH=35 T=28 CheckSum=Ok RH=35 T=28 CheckSum=Ok RH=35 T=28 Serial port COM2 closed 


Source code of the program
Hidden text
 #include <msp430g2553.h> #include <stdio.h> #include <string.h> //     unsigned int signal[42]; //  unsigned short data[5]; //   signal int signalElement = 0; //     // , 10*50000=0,5  int debouncePause = 0; // ,  1     int isDataReading = 0; //     UART void sendString(char*); void main(void) { //   WDTCTL = WDTPW + //      //     WDTPW WDTHOLD; // WDTHOLD     //  //     P1.0 //      // P1.0         P1DIR |= BIT0; P1OUT &= ~BIT0; //     P1.6 //      //     // P1.6         P1DIR |= BIT6; P1OUT |= BIT6; //    P1.3 //        // P1.3  ,   //   ,     //   P1.3      // 1  0 P1DIR &= ~BIT3; P1OUT |= BIT3; P1REN |= BIT3; P1IFG &= ~BIT3; P1IES |= BIT3; P1IE |= BIT3; // P1.2      UART //      P1SEL |= BIT2; P1SEL2 |= BIT2; // UART //     GRACE  // ,..      //    //Baund 9600 UCA0CTL1 |= UCSSEL_2; UCA0BR0 = 104; UCA0BR1 = 0; UCA0MCTL = UCBRS0; UCA0CTL1 &= ~UCSWRST; //    //   Grace  1, ..   //       BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0; if (CALBC1_1MHZ != 0xFF) { DCOCTL = 0x00; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; } BCSCTL1 |= XT2OFF + DIVA_0; BCSCTL3 = XT2S_0 + LFXT1S_2 + XCAP_1; //    while (1) { //     //      //    CPUOFF //(    ) __bis_SR_register(CPUOFF + GIE); unsigned int i = 0; unsigned int j = 0; //      //    for (j = 0; j < 5; j++) { //      data[j] = 0; for (i = 0; i < 8; i++) { int k = i + 2 + j * 8; //     1  0 // 100   120,    //     if (signal[k] > 100 && signal[k] < 120) { data[j] |= (1 << (7 - i)); } } } char buf[30]; //   memset(buf, 0, 30); //  0 //       sprintf(buf, "CheckSum=%s\n", data[0] + data[1] + data[2] + data[3] == data[4] ? "Ok" : "Error"); sendString(buf); //   UART memset(buf, 0, 30); //     sprintf(buf, "RH=%d\n", data[0]); sendString(buf); memset(buf, 0, 30); //     sprintf(buf, "T=%d\n", data[2]); sendString(buf); //  , //     P1OUT |= BIT6; //  , //   P1OUT &= ~BIT0; //        P1.3,    // P1IFG &= ~BIT3; P1IE |= BIT3; //      isDataReading = 0; debouncePause = 0; signalElement = 0; } } //   №0  #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer0_A0(void) { //  10*50000=0,5  if (debouncePause == 9) { //  TA0CTL = TACLR; //  P2.5   //      P2DIR |= BIT5; P2OUT &= ~BIT5; // ,     UP  20 ,    , //    TA0CCR0 = 20000; TA0CTL = TASSEL_2 + MC_1; } // 20      if (debouncePause == 10) { // , //  №0 , TA0CTL = TACLR; TA0CCTL0 &= ~CCIE; // P2.5  //,   ,  , P2DIR &= ~BIT5; P2IFG &= ~BIT5; P2IES |= BIT5; P2IE |= BIT5; //    Continuous   // №1,      - //  ,        TA0CTL = TASSEL_2 + MC_2 + TAIE; } debouncePause++; } //   №1  #pragma vector=TIMER0_A1_VECTOR __interrupt void Timer0_A1(void) { //    switch (TA0IV) { case TA0IV_TAIFG: //  //  P2.5 ,     , //      //   UART P2IE &= ~BIT5; TA0CTL = TACLR; __bic_SR_register_on_exit(CPUOFF); break; default: break; } } //   ,  P1.3 #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { //       , //   P1IFG &= ~BIT3; P1IE &= ~BIT3; //  P1OUT &= ~BIT0; P1OUT &= ~BIT6; //    UP    №0 //     50000 //     TA0CCR0 = 50000; TA0CCTL0 |= CCIE; TA0CTL = TASSEL_2 + MC_1; } //     ,  P2.5 #pragma vector=PORT2_VECTOR __interrupt void Port_2(void) { //   P2IFG &= ~BIT5; //     signal[signalElement] = TA0R; //    TA0CTL = TACLR; TA0CTL = TASSEL_2 + MC_2 + TAIE; //   P1OUT ^= BIT0; //  1    signalElement++; } void sendString(char * text) { int i = 0; for (i = 0; i < strlen(text); i++) { while (!(IFG2 & UCA0TXIFG)) ; //      UCA0TXBUF = text[i]; //      } } 

Source: https://habr.com/ru/post/160769/


All Articles