Xu Zicheng / Mbed 2 deprecated MSproject__mutifunctional__FAN

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 DigitalInOut data_pin(p6); // Activate digital in as dht11 Pin
00004 DigitalOut pin5(p5); // Switch the fan on and off
00005 TextLCD lcd(p10, p12, p15, p16, p29, p30); // rs, e, d4-d7
00006 Timer tmr; //initialize timer
00007 uint64_t adat; // 64 bit variable for temporary data
00008 int i;
00009 void dht_read(void) {
00010     data_pin.output(); // Set p6 as output
00011     // Initialize measurement > 18 ms low
00012     data_pin = 0;
00013     wait_ms(20);
00014     // After high and release the pin switch input mode
00015     data_pin = 1;
00016     data_pin.input();
00017     // Wait until the end of 80 us low
00018     while(!data_pin.read()) {}
00019     // Wait until end of 80 us high
00020     while(data_pin.read()) {}
00021     // 40 bit, 40 read out cycle
00022     for(i=0; i<40; i++) {
00023         adat = adat << 1; // Shift for new number
00024         tmr.stop(); // Stop timer if runs
00025         tmr.reset();  // Reset timer
00026         // Wait until pin
00027         while(!data_pin.read()) {}          
00028         tmr.start();            
00029         while(data_pin.read()) {}
00030         // If DHT11 HIGH longer than 40 micro seconds (hopefully 70 us)
00031         if(tmr.read_us() > 40) {
00032             // bit is 1
00033             adat++;
00034         }
00035     }
00036 }
00037 int main() {
00038     while(1) {
00039             adat = 0;
00040             dht_read(); // Call the function   
00041             int abc = (adat & 0x0000000000ff0000) >> 16;            
00042             lcd.printf("Temperature:\n");
00043             lcd.printf("%d",abc);     // Temperature       
00044             if(abc > 20) pin5 = 1;   // Fan is ON
00045             else pin5 = 0;   //Fan is OFF
00046             wait(2); // Wait 2 sec till continue.
00047             lcd.cls(); // Clear the screen
00048     }
00049 }