Temperature Data Logger or Display. Program uses the EA LPCXpresso Board's on-board temp sensor and SD card to constantly monitor the temperature. Optionally, the temp can be displayed on the EA OLED display.
Dependencies: mbed SDFileSystem
main.cpp@0:e05fd3c9c4b3, 2010-06-15 (annotated)
- Committer:
- tyger23
- Date:
- Tue Jun 15 20:21:07 2010 +0000
- Revision:
- 0:e05fd3c9c4b3
- Child:
- 1:37f2341e763b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tyger23 | 0:e05fd3c9c4b3 | 1 | // Program monitors the temperature recorded on the EA LPCXpresso board's U7 |
tyger23 | 0:e05fd3c9c4b3 | 2 | // There is an option to either display the temperature on the OLED or write to an SD card |
tyger23 | 0:e05fd3c9c4b3 | 3 | // If writing to an SD card, be sure to only unplug the MBED or remove the SD card when LED3 is illuminated |
tyger23 | 0:e05fd3c9c4b3 | 4 | // Removing power or the SD card when LED3 is not illuminated will result in a loss of data |
tyger23 | 0:e05fd3c9c4b3 | 5 | // Temperature is monitored every 6 seconds in the current code |
tyger23 | 0:e05fd3c9c4b3 | 6 | |
tyger23 | 0:e05fd3c9c4b3 | 7 | #include "mbed.h" |
tyger23 | 0:e05fd3c9c4b3 | 8 | #include "EAOLED.h" |
tyger23 | 0:e05fd3c9c4b3 | 9 | #include "SDFileSystem.h" |
tyger23 | 0:e05fd3c9c4b3 | 10 | |
tyger23 | 0:e05fd3c9c4b3 | 11 | SDFileSystem sd(p5, p6, p7, p24, "sd"); //Used for file system writes to the EA SD card. Comment this out if displaying on OLED. |
tyger23 | 0:e05fd3c9c4b3 | 12 | DigitalOut myled1(LED1); |
tyger23 | 0:e05fd3c9c4b3 | 13 | DigitalOut myled2(LED2); |
tyger23 | 0:e05fd3c9c4b3 | 14 | DigitalOut myled3(LED3); |
tyger23 | 0:e05fd3c9c4b3 | 15 | //EAOLED oled(p5, p6, p7, p8, p25); // mosi, dnc, sclk, cs, power ***this is only used if displaying temp on OLED instead of file writing*** |
tyger23 | 0:e05fd3c9c4b3 | 16 | InterruptIn sense(p8); //make sure EA board J25 is set to PIO0_2 ***if using the OLED, must change this to pin 10 and connect wire from J25 center pin to pin10 on mBed, make sure J25 does not have a jumper populated*** |
tyger23 | 0:e05fd3c9c4b3 | 17 | |
tyger23 | 0:e05fd3c9c4b3 | 18 | Timer timer; //timer used for keeping track of elapesd time |
tyger23 | 0:e05fd3c9c4b3 | 19 | Timeout timeout; //timeout used for calculation of frequency |
tyger23 | 0:e05fd3c9c4b3 | 20 | int edgecount, on, seconds; |
tyger23 | 0:e05fd3c9c4b3 | 21 | float temp, freq, tempf; |
tyger23 | 0:e05fd3c9c4b3 | 22 | |
tyger23 | 0:e05fd3c9c4b3 | 23 | void edge(){ |
tyger23 | 0:e05fd3c9c4b3 | 24 | edgecount = edgecount++; |
tyger23 | 0:e05fd3c9c4b3 | 25 | } |
tyger23 | 0:e05fd3c9c4b3 | 26 | |
tyger23 | 0:e05fd3c9c4b3 | 27 | void attimeout(){ |
tyger23 | 0:e05fd3c9c4b3 | 28 | on = 0; |
tyger23 | 0:e05fd3c9c4b3 | 29 | } |
tyger23 | 0:e05fd3c9c4b3 | 30 | |
tyger23 | 0:e05fd3c9c4b3 | 31 | int main() { |
tyger23 | 0:e05fd3c9c4b3 | 32 | // oled.cls(); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 33 | // oled.locate(0,0); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 34 | // oled.printf("Frequency:"); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 35 | // oled.locate(0,2); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 36 | // oled.printf("Temperature:"); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 37 | myled1 = 1; //indicates the program is running |
tyger23 | 0:e05fd3c9c4b3 | 38 | timer.start(); //starts the tracking timer. Can comment this out if using the OLED. |
tyger23 | 0:e05fd3c9c4b3 | 39 | FILE *fp = fopen("/sd/temperat.csv", "w"); //formats the file header. Comment this out if using the OLED. |
tyger23 | 0:e05fd3c9c4b3 | 40 | fprintf(fp,"Time, Freq, TempC, TempF\n"); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 41 | fclose(fp); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 42 | |
tyger23 | 0:e05fd3c9c4b3 | 43 | while(1){ |
tyger23 | 0:e05fd3c9c4b3 | 44 | on=1; |
tyger23 | 0:e05fd3c9c4b3 | 45 | edgecount = 0; |
tyger23 | 0:e05fd3c9c4b3 | 46 | myled2 = 1; //indicates the temperature is being monitored |
tyger23 | 0:e05fd3c9c4b3 | 47 | myled3 = 0; |
tyger23 | 0:e05fd3c9c4b3 | 48 | FILE *fp = fopen("/sd/temperat.csv", "a"); //comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 49 | |
tyger23 | 0:e05fd3c9c4b3 | 50 | timeout.attach(&attimeout, 5); //sets the amount of time used to calcuate the frequency of the temp sensor |
tyger23 | 0:e05fd3c9c4b3 | 51 | while(on){ |
tyger23 | 0:e05fd3c9c4b3 | 52 | sense.rise(&edge); //tracks the rising edge of the temp sensor |
tyger23 | 0:e05fd3c9c4b3 | 53 | } |
tyger23 | 0:e05fd3c9c4b3 | 54 | |
tyger23 | 0:e05fd3c9c4b3 | 55 | freq = (float)(edgecount)/5; //calculates the frequency. Change the divisior if changing the timeout.attach() |
tyger23 | 0:e05fd3c9c4b3 | 56 | // oled.locate(0,1); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 57 | // oled.printf("%4.3f", freq); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 58 | temp = freq; |
tyger23 | 0:e05fd3c9c4b3 | 59 | temp = 1/temp; //changes frequency into period |
tyger23 | 0:e05fd3c9c4b3 | 60 | temp = temp*100000; //provides appropriate modifier if J26 is set to GND, GND. |
tyger23 | 0:e05fd3c9c4b3 | 61 | temp = temp-273.15; //calculates the temperature in degrees C |
tyger23 | 0:e05fd3c9c4b3 | 62 | // oled.locate(0,3); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 63 | // oled.printf("%2.3f", temp); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 64 | tempf = 9*temp; |
tyger23 | 0:e05fd3c9c4b3 | 65 | tempf = tempf/5; |
tyger23 | 0:e05fd3c9c4b3 | 66 | tempf = tempf + 32; //calcualtes the temperature in degrees F |
tyger23 | 0:e05fd3c9c4b3 | 67 | // oled.locate(0,4); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 68 | // oled.printf("%2.3f", tempf); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 69 | seconds = timer.read(); //Reads the elaped time |
tyger23 | 0:e05fd3c9c4b3 | 70 | fprintf(fp, "%d, ", seconds); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 71 | fprintf(fp,"%4.3f, %2.3f, %2.3f\n", freq, temp, tempf); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 72 | fclose(fp); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 73 | myled2 = 0; //indicates temperature monitoring is over |
tyger23 | 0:e05fd3c9c4b3 | 74 | myled3 = 1; //indicates safe to remove power or SD card |
tyger23 | 0:e05fd3c9c4b3 | 75 | wait(1); //wait time to allow for safe card removal or powerdown. Can be commented out if using OLED. |
tyger23 | 0:e05fd3c9c4b3 | 76 | } |
tyger23 | 0:e05fd3c9c4b3 | 77 | } |