han back / Mbed OS CLEO_UART_DHT11
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 struct UART_buf
00004 { 
00005     uint8_t STA;
00006     uint8_t MODE; 
00007     uint8_t CMD;
00008     uint8_t LEN;
00009     uint8_t DATA[32];
00010     uint8_t END; 
00011      
00012 };
00013  
00014 
00015 #define DHTLIB_OK                0
00016 #define DHTLIB_ERROR_CHECKSUM   -1
00017 #define DHTLIB_ERROR_TIMEOUT    -2
00018 
00019 PinName pin_DHT11 = PA_15;
00020 
00021 Serial SerialUART(PA_2, PA_3);
00022 
00023 uint8_t Buffer[37];
00024 
00025 DigitalInOut data_pin(pin_DHT11);
00026 
00027 UART_buf RX_BUF;
00028 
00029 Timer tmr;
00030 Timer tmr1;
00031 
00032 int humi;
00033 int temp;
00034 
00035 void SerialUARTRX_ISR(void);
00036 void Timer_setting(uint8_t cmd, uint8_t value);
00037 void Sensor_Read(void);
00038 int dht_read(void);
00039 
00040 uint32_t T_period = 2000;
00041 uint32_t timer_check = 0;
00042 
00043 int main() {
00044     
00045     SerialUART.baud(115200);
00046     
00047     SerialUART.attach(&SerialUARTRX_ISR);
00048     
00049     //Timer_setting(0x06, 2);
00050     
00051     while(1)
00052     {
00053         if((timer_check + T_period) < tmr1.read_ms())
00054         {
00055             Sensor_Read();
00056             timer_check += T_period;
00057         }
00058     }
00059 }
00060 
00061 void SerialUARTRX_ISR(void)
00062 {
00063     static uint8_t RX_count = 0, RX_Len = 32, RX_Status = 0;
00064     uint8_t rx_da = SerialUART.getc();
00065     switch(RX_Status)
00066     {
00067         case 0:
00068             if(rx_da == 0x76)
00069             {
00070                 RX_BUF.STA = rx_da;
00071                 RX_Status++;
00072             }
00073             break;
00074         case 1:
00075             RX_BUF.MODE = rx_da;
00076             RX_Status++;
00077             break;
00078         case 2:
00079             RX_BUF.CMD = rx_da;
00080             RX_Status++;
00081             break;
00082         case 3:
00083             RX_BUF.LEN = rx_da;
00084             RX_Len = RX_BUF.LEN;
00085             RX_Status++;
00086             if(RX_Len == 0)
00087                 RX_Status++;
00088             break;
00089         case 4:
00090             RX_BUF.DATA[RX_count] = rx_da;
00091             RX_count++;
00092             if(RX_count == RX_Len)
00093             {
00094                 RX_Status++;
00095                 RX_count = 0;
00096                 RX_Len = 32;
00097             }
00098             break;
00099         case 5:
00100             if(rx_da == 0x3E)
00101             {
00102                 RX_BUF.END = rx_da;
00103                 RX_Status = 0;
00104                 switch(RX_BUF.MODE)
00105                 {
00106                     case 0x04:
00107                         Timer_setting(RX_BUF.CMD, RX_BUF.DATA[0]);
00108                         break;
00109                 }
00110             }
00111             break;
00112     }
00113 }
00114 
00115 void Timer_setting(uint8_t cmd, uint8_t value)
00116 {
00117     double Time_value = 0;
00118     tmr1.stop();
00119     tmr1.reset();
00120     switch(cmd)
00121     {
00122         case 0x01:
00123             Time_value = 30;
00124             break;
00125         case 0x02:
00126             Time_value = 60;
00127             break;
00128         case 0x03:
00129             Time_value = 120;
00130             break;
00131         case 0x04:
00132             Time_value = 300;
00133             break;
00134         case 0x05:
00135             Time_value = 600;
00136             break;
00137         case 0x06:
00138             Time_value = value;
00139             Time_value = 1.0/Time_value;
00140             break;
00141     }
00142     tmr1.start();
00143     timer_check = tmr.read_ms();
00144     T_period = Time_value * 1000;
00145 }
00146 
00147 void Sensor_Read(void)
00148 {
00149     Buffer[0] = 0x76;
00150     Buffer[1] = 0x01;
00151     Buffer[2] = 0x05;
00152     Buffer[3] = 0x02;
00153     if(dht_read() == 0)
00154     {
00155         Buffer[4] = temp;
00156         Buffer[5] = humi;
00157     }
00158     else
00159     {
00160         Buffer[4] = 0;
00161         Buffer[5] = 0;
00162     }
00163     Buffer[6] = 0x3E;
00164     for(int i=0; i<7; i++)
00165         SerialUART.putc(Buffer[i]);
00166 }
00167 
00168 int dht_read(void){
00169     
00170     // BUFFER TO RECEIVE
00171     uint8_t bits[5];
00172     uint8_t cnt = 7;
00173     uint8_t idx = 0;
00174     
00175     tmr.stop();
00176     tmr.reset();
00177 
00178     // EMPTY BUFFER
00179     for(int i=0; i< 5; i++) bits[i] = 0;
00180 
00181     // REQUEST SAMPLE
00182     data_pin.output();
00183     data_pin.write(0);
00184     wait_ms(18);
00185     data_pin.write(1);
00186     wait_us(10);
00187     data_pin.input();
00188     wait_us(40);
00189 
00190     // ACKNOWLEDGE or TIMEOUT
00191     unsigned long loopCnt = 10000;
00192     
00193     while(data_pin.read() == 0)if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00194  
00195     loopCnt = 10000;
00196     
00197     while(data_pin.read() == 1)if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00198 
00199     // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
00200 
00201     for(int i=0; i<40; i++){
00202         
00203         loopCnt = 10000;
00204         
00205         while(data_pin.read() == 0)if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
00206 
00207         //unsigned long t = micros();
00208         //time_count = tmr.read_us();
00209         
00210         tmr.start();
00211 
00212         loopCnt = 10000;
00213         
00214         while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00215 
00216         if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);
00217         
00218         tmr.stop();
00219         tmr.reset();
00220         
00221         if(cnt == 0){   // next byte?
00222         
00223             cnt = 7;    // restart at MSB
00224             idx++;      // next byte!
00225             
00226         }else cnt--;
00227         
00228     }
00229 
00230     // WRITE TO RIGHT VARS
00231     // as bits[1] and bits[3] are allways zero they are omitted in formulas.
00232     humi = bits[0]; 
00233     temp = bits[2]; 
00234 
00235     uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
00236     
00237     if(bits[4] != sum){return DHTLIB_ERROR_CHECKSUM;}
00238     
00239     return DHTLIB_OK;
00240     
00241 }