Jim Mueller / Mbed OS Nucleo_dht11

Dependencies:   LCD_i2c_GSOE

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "LCD.h"
00004 
00005 
00006 
00007 #define DHTLIB_OK                0
00008 #define DHTLIB_ERROR_CHECKSUM   -1
00009 #define DHTLIB_ERROR_TIMEOUT    -2
00010 
00011 Timer tmr;
00012 lcd mylcd;
00013 
00014 DigitalInOut data_pin(PB_0);  //Anschluss S (Data)
00015 //Anschluss - an GND
00016 //mittlerer Anschluss an 3,3V
00017 
00018 int humidity;
00019 int temperature;
00020 
00021 //########################################
00022 // DHT11 Library
00023 //########################################
00024 int dht_read(void){
00025     
00026     // BUFFER TO RECEIVE
00027     uint8_t bits[5];
00028     uint8_t cnt = 7;
00029     uint8_t idx = 0;
00030     
00031     tmr.stop();
00032     tmr.reset();
00033 
00034     // EMPTY BUFFER
00035     for(int i=0; i< 5; i++) bits[i] = 0;
00036 
00037     // REQUEST SAMPLE
00038     data_pin.output();
00039     data_pin.write(0);
00040     wait_us(18000);
00041     data_pin.write(1);
00042     wait_us(40);
00043     data_pin.input();
00044 
00045     // ACKNOWLEDGE or TIMEOUT
00046     unsigned int loopCnt = 20000;
00047     
00048     while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00049 
00050     loopCnt = 20000;
00051     
00052     while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00053 
00054     // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
00055     for(int i=0; i<40; i++){
00056         
00057         loopCnt = 20000;
00058         
00059         while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT;
00060 
00061         //unsigned long t = micros();
00062         tmr.start();
00063 
00064         loopCnt = 20000;
00065         
00066         while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT;
00067 
00068         if(tmr.read_us() > 40) bits[idx] |= (1 << cnt);
00069         
00070         tmr.stop();
00071         tmr.reset();
00072         
00073         if(cnt == 0){   // next byte?
00074         
00075             cnt = 7;    // restart at MSB
00076             idx++;      // next byte!
00077             
00078         }else cnt--;
00079         
00080     }
00081 
00082     // WRITE TO RIGHT VARS
00083     // as bits[1] and bits[3] are allways zero they are omitted in formulas.
00084     humidity    = bits[0]; 
00085     temperature = bits[2]; 
00086 
00087     uint8_t sum = bits[0] + bits[2];  
00088 
00089     if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM;
00090     
00091     return DHTLIB_OK;
00092     
00093 }
00094 
00095 char buffer[17];
00096 
00097 //########################################
00098 // End of DHT11 Library
00099 //########################################
00100 
00101 int main(void){
00102     
00103     mylcd.clear();
00104     mylcd.cursorpos(0);
00105     
00106     mylcd.printf("Nucleo - DHT11");
00107     
00108     for(;;){
00109        int fehler=dht_read();
00110         
00111             
00112 
00113             mylcd.cursorpos(0x40);
00114             mylcd.printf("Hum %2d%%  Tmp %2dc  ", humidity, temperature);
00115             wait_us(500000);
00116             
00117 
00118     
00119     }
00120     
00121 }