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