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@1:37f2341e763b, 2010-06-16 (annotated)
- Committer:
- tyger23
- Date:
- Wed Jun 16 16:08:29 2010 +0000
- Revision:
- 1:37f2341e763b
- Parent:
- 0:e05fd3c9c4b3
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 | 1:37f2341e763b | 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. Make sure J55 pins 1&2 (A) are disconnected! |
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 | Timeout timeout; //timeout used for calculation of frequency |
tyger23 | 1:37f2341e763b | 19 | int edgecount, on, et; |
tyger23 | 0:e05fd3c9c4b3 | 20 | float temp, freq, tempf; |
tyger23 | 0:e05fd3c9c4b3 | 21 | |
tyger23 | 0:e05fd3c9c4b3 | 22 | void edge(){ |
tyger23 | 0:e05fd3c9c4b3 | 23 | edgecount = edgecount++; |
tyger23 | 0:e05fd3c9c4b3 | 24 | } |
tyger23 | 0:e05fd3c9c4b3 | 25 | |
tyger23 | 0:e05fd3c9c4b3 | 26 | void attimeout(){ |
tyger23 | 0:e05fd3c9c4b3 | 27 | on = 0; |
tyger23 | 0:e05fd3c9c4b3 | 28 | } |
tyger23 | 0:e05fd3c9c4b3 | 29 | |
tyger23 | 0:e05fd3c9c4b3 | 30 | int main() { |
tyger23 | 0:e05fd3c9c4b3 | 31 | // oled.cls(); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 32 | // oled.locate(0,0); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 33 | // oled.printf("Frequency:"); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 34 | // oled.locate(0,2); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 35 | // oled.printf("Temperature:"); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 36 | myled1 = 1; //indicates the program is running |
tyger23 | 0:e05fd3c9c4b3 | 37 | FILE *fp = fopen("/sd/temperat.csv", "w"); //formats the file header. Comment this out if using the OLED. |
tyger23 | 0:e05fd3c9c4b3 | 38 | fprintf(fp,"Time, Freq, TempC, TempF\n"); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 39 | fclose(fp); //Comment this out if using the OLED |
tyger23 | 1:37f2341e763b | 40 | et = 0; //Sets elapsed time to 0. Can comment this out if using OLED. |
tyger23 | 0:e05fd3c9c4b3 | 41 | |
tyger23 | 0:e05fd3c9c4b3 | 42 | while(1){ |
tyger23 | 1:37f2341e763b | 43 | myled2 = 0; //indicates temperature is not being monitored. Can be commented out if using OLED. |
tyger23 | 1:37f2341e763b | 44 | myled3 = 1; //indicates safe to remove power or SD card. Can be commented out if using OLED. |
tyger23 | 1:37f2341e763b | 45 | wait(1); //wait time to allow for safe card removal or powerdown. Can be commented out if using OLED. |
tyger23 | 1:37f2341e763b | 46 | myled2 = 1; //indicates the temperature is being monitored. Can be commented out if using OLED. |
tyger23 | 1:37f2341e763b | 47 | myled3 = 0; //indicates not safe to remove SD card or power. Can be commented out if using OLED. |
tyger23 | 1:37f2341e763b | 48 | |
tyger23 | 1:37f2341e763b | 49 | FILE *fp = fopen("/sd/temperat.csv", "a"); //comment this out if using the OLED |
tyger23 | 1:37f2341e763b | 50 | |
tyger23 | 0:e05fd3c9c4b3 | 51 | on=1; |
tyger23 | 0:e05fd3c9c4b3 | 52 | edgecount = 0; |
tyger23 | 0:e05fd3c9c4b3 | 53 | timeout.attach(&attimeout, 5); //sets the amount of time used to calcuate the frequency of the temp sensor |
tyger23 | 0:e05fd3c9c4b3 | 54 | while(on){ |
tyger23 | 0:e05fd3c9c4b3 | 55 | sense.rise(&edge); //tracks the rising edge of the temp sensor |
tyger23 | 0:e05fd3c9c4b3 | 56 | } |
tyger23 | 0:e05fd3c9c4b3 | 57 | |
tyger23 | 0:e05fd3c9c4b3 | 58 | freq = (float)(edgecount)/5; //calculates the frequency. Change the divisior if changing the timeout.attach() |
tyger23 | 0:e05fd3c9c4b3 | 59 | // oled.locate(0,1); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 60 | // oled.printf("%4.3f", freq); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 61 | temp = freq; |
tyger23 | 0:e05fd3c9c4b3 | 62 | temp = 1/temp; //changes frequency into period |
tyger23 | 0:e05fd3c9c4b3 | 63 | temp = temp*100000; //provides appropriate modifier if J26 is set to GND, GND. |
tyger23 | 0:e05fd3c9c4b3 | 64 | temp = temp-273.15; //calculates the temperature in degrees C |
tyger23 | 0:e05fd3c9c4b3 | 65 | // oled.locate(0,3); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 66 | // oled.printf("%2.3f", temp); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 67 | tempf = 9*temp; |
tyger23 | 0:e05fd3c9c4b3 | 68 | tempf = tempf/5; |
tyger23 | 0:e05fd3c9c4b3 | 69 | tempf = tempf + 32; //calcualtes the temperature in degrees F |
tyger23 | 0:e05fd3c9c4b3 | 70 | // oled.locate(0,4); //*** make sure this is not commented out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 71 | // oled.printf("%2.3f", tempf); //*** make sure this is not commented out if using the OLED |
tyger23 | 1:37f2341e763b | 72 | et = et+6; //Calculates the elaped time in seconds. If changing the timeout or the wait time for SD safe removal, you must change this accordingly. Comment this out if using the OLED |
tyger23 | 1:37f2341e763b | 73 | fprintf(fp,"%d, %4.3f, %2.3f, %2.3f\n", et, freq, temp, tempf); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 74 | fclose(fp); //Comment this out if using the OLED |
tyger23 | 0:e05fd3c9c4b3 | 75 | } |
tyger23 | 0:e05fd3c9c4b3 | 76 | } |