![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
lpc824break mbed sd文件系统
Dependencies: DS1302 SDFileSystem mbed
Fork of temp_humid_time_DS1302_LM35_DHT11 by
main.cpp@0:8f0934e41a57, 2014-07-04 (annotated)
- Committer:
- Clovis
- Date:
- Fri Jul 04 08:08:38 2014 +0000
- Revision:
- 0:8f0934e41a57
- Child:
- 1:b201895769bd
Datalogger for temperature, humidity and light level, using a FRDM-K64F development board by Freescale and storing data on a microSD card. It keeps track of the time by means of a DS1302 timekeeping chip.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Clovis | 0:8f0934e41a57 | 1 | #include "mbed.h" |
Clovis | 0:8f0934e41a57 | 2 | #include "SDFileSystem.h" |
Clovis | 0:8f0934e41a57 | 3 | #include "DHT.h" |
Clovis | 0:8f0934e41a57 | 4 | #include "DS1302.h" |
Clovis | 0:8f0934e41a57 | 5 | |
Clovis | 0:8f0934e41a57 | 6 | // Defines for the DS1302 timer module |
Clovis | 0:8f0934e41a57 | 7 | #define SCLK PTC1 |
Clovis | 0:8f0934e41a57 | 8 | #define IO PTB19 |
Clovis | 0:8f0934e41a57 | 9 | #define CE PTB18 |
Clovis | 0:8f0934e41a57 | 10 | |
Clovis | 0:8f0934e41a57 | 11 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. (SD Card) Tested on K64F, correct pins. |
Clovis | 0:8f0934e41a57 | 12 | AnalogIn LM35(PTB2); |
Clovis | 0:8f0934e41a57 | 13 | AnalogIn LDR(PTB3); |
Clovis | 0:8f0934e41a57 | 14 | int count=0; |
Clovis | 0:8f0934e41a57 | 15 | float ldrcalc= 0.00; |
Clovis | 0:8f0934e41a57 | 16 | DigitalOut RedLed(LED1); // Error indication |
Clovis | 0:8f0934e41a57 | 17 | DigitalOut GreenLed(LED2); // LM35 read indication |
Clovis | 0:8f0934e41a57 | 18 | DigitalOut Blueled(LED3); // DHT11 read indication |
Clovis | 0:8f0934e41a57 | 19 | DHT sensor(PTB20,DHT11); // Use the DHT11 sensor, on pin |
Clovis | 0:8f0934e41a57 | 20 | DS1302 clk(SCLK, IO, PTB18); // ports for the DS1302 time keeper |
Clovis | 0:8f0934e41a57 | 21 | |
Clovis | 0:8f0934e41a57 | 22 | int main() { |
Clovis | 0:8f0934e41a57 | 23 | // the 6 lines below are for the time keeper chip |
Clovis | 0:8f0934e41a57 | 24 | #ifdef INITIAL_RUN |
Clovis | 0:8f0934e41a57 | 25 | clk.set_time(1403949720); |
Clovis | 0:8f0934e41a57 | 26 | #endif |
Clovis | 0:8f0934e41a57 | 27 | |
Clovis | 0:8f0934e41a57 | 28 | char storedByte = clk.recallByte(0); |
Clovis | 0:8f0934e41a57 | 29 | //printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte); |
Clovis | 0:8f0934e41a57 | 30 | clk.storeByte(0, storedByte + 1); |
Clovis | 0:8f0934e41a57 | 31 | // |
Clovis | 0:8f0934e41a57 | 32 | |
Clovis | 0:8f0934e41a57 | 33 | int err; |
Clovis | 0:8f0934e41a57 | 34 | wait(1); // wait 1 second for DHT11 to stabilyze |
Clovis | 0:8f0934e41a57 | 35 | |
Clovis | 0:8f0934e41a57 | 36 | //printf("Hello World!\n"); |
Clovis | 0:8f0934e41a57 | 37 | |
Clovis | 0:8f0934e41a57 | 38 | mkdir("/sd/dados", 0777); |
Clovis | 0:8f0934e41a57 | 39 | |
Clovis | 0:8f0934e41a57 | 40 | FILE *fp = fopen("/sd/dados/data001.csv", "w"); |
Clovis | 0:8f0934e41a57 | 41 | if(fp == NULL) { |
Clovis | 0:8f0934e41a57 | 42 | error("Could not open file for write\n"); |
Clovis | 0:8f0934e41a57 | 43 | RedLed= 0; |
Clovis | 0:8f0934e41a57 | 44 | |
Clovis | 0:8f0934e41a57 | 45 | } |
Clovis | 0:8f0934e41a57 | 46 | while (1) { |
Clovis | 0:8f0934e41a57 | 47 | |
Clovis | 0:8f0934e41a57 | 48 | Blueled= 1; |
Clovis | 0:8f0934e41a57 | 49 | RedLed= 1; |
Clovis | 0:8f0934e41a57 | 50 | GreenLed= 1; |
Clovis | 0:8f0934e41a57 | 51 | |
Clovis | 0:8f0934e41a57 | 52 | if (count < 12000000){ |
Clovis | 0:8f0934e41a57 | 53 | count++; |
Clovis | 0:8f0934e41a57 | 54 | } else{ |
Clovis | 0:8f0934e41a57 | 55 | // -------------------------- |
Clovis | 0:8f0934e41a57 | 56 | |
Clovis | 0:8f0934e41a57 | 57 | err = sensor.readData(); |
Clovis | 0:8f0934e41a57 | 58 | if (err == 0) { |
Clovis | 0:8f0934e41a57 | 59 | GreenLed= 0; |
Clovis | 0:8f0934e41a57 | 60 | printf("Temperature is %4.2f \r\n",sensor.ReadTemperature(CELCIUS)); |
Clovis | 0:8f0934e41a57 | 61 | //printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT)); |
Clovis | 0:8f0934e41a57 | 62 | //printf("Temperature is %4.2f K \r\n",sensor.ReadTemperature(KELVIN)); |
Clovis | 0:8f0934e41a57 | 63 | printf("Humidity is %4.2f \r\n",sensor.ReadHumidity()); |
Clovis | 0:8f0934e41a57 | 64 | //printf("Dew point is %4.2f \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
Clovis | 0:8f0934e41a57 | 65 | //printf("Dew point (fast) is %4.2f \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity())); |
Clovis | 0:8f0934e41a57 | 66 | |
Clovis | 0:8f0934e41a57 | 67 | } else |
Clovis | 0:8f0934e41a57 | 68 | printf("\r\nErr %i \n",err); |
Clovis | 0:8f0934e41a57 | 69 | |
Clovis | 0:8f0934e41a57 | 70 | // ---------------------------- |
Clovis | 0:8f0934e41a57 | 71 | FILE *fp = fopen("/sd/dados/data001.csv", "a"); |
Clovis | 0:8f0934e41a57 | 72 | if(fp == NULL) { |
Clovis | 0:8f0934e41a57 | 73 | error("Could not open file for write\n"); |
Clovis | 0:8f0934e41a57 | 74 | |
Clovis | 0:8f0934e41a57 | 75 | } |
Clovis | 0:8f0934e41a57 | 76 | |
Clovis | 0:8f0934e41a57 | 77 | ldrcalc= LDR.read(); |
Clovis | 0:8f0934e41a57 | 78 | ldrcalc= (1/ldrcalc)-1; |
Clovis | 0:8f0934e41a57 | 79 | time_t seconds = clk.time(NULL); |
Clovis | 0:8f0934e41a57 | 80 | //fprintf(fp, "%s\r,%f,%f,%f\n", "LM35", "DHT11", "Humid", "Month", "Day", "Hour", "Year"); |
Clovis | 0:8f0934e41a57 | 81 | fprintf(fp, "%s\r,%f,%f,%f,%f", ctime(&seconds), 333.333*LM35.read(), sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity(), ldrcalc); |
Clovis | 0:8f0934e41a57 | 82 | fclose(fp); |
Clovis | 0:8f0934e41a57 | 83 | //printf("Goodbye World!\n"); |
Clovis | 0:8f0934e41a57 | 84 | count=0; |
Clovis | 0:8f0934e41a57 | 85 | Blueled= 0; |
Clovis | 0:8f0934e41a57 | 86 | } |
Clovis | 0:8f0934e41a57 | 87 | } |
Clovis | 0:8f0934e41a57 | 88 | } |