This program can log data that is read from RTC(real time clock) which is embedded inside LPC1768. It saves the data to the LPC1768’s memory as “log.txt”.
main.cpp@2:e51a43447d68, 2015-07-08 (annotated)
- Committer:
- BaserK
- Date:
- Wed Jul 08 17:13:03 2015 +0000
- Revision:
- 2:e51a43447d68
- Parent:
- 0:a5ea226b5b26
- Child:
- 3:192d65ce0c19
Comment updates, code still same
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BaserK | 0:a5ea226b5b26 | 1 | /* Datalogging with Real Time Clock on LPC1768 |
BaserK | 0:a5ea226b5b26 | 2 | * |
BaserK | 0:a5ea226b5b26 | 3 | * @author: Baser Kandehir |
BaserK | 0:a5ea226b5b26 | 4 | * @date: July 8, 2015 |
BaserK | 0:a5ea226b5b26 | 5 | * @license: Use this code however you'd like |
BaserK | 0:a5ea226b5b26 | 6 | * |
BaserK | 0:a5ea226b5b26 | 7 | * @description of the program: |
BaserK | 2:e51a43447d68 | 8 | * |
BaserK | 2:e51a43447d68 | 9 | * This program can log data that is read from RTC(real time clock) which |
BaserK | 2:e51a43447d68 | 10 | * is embedded inside LPC1768. It saves the data to the LPC1768’s memory as |
BaserK | 2:e51a43447d68 | 11 | * “log.txt”. For this example program, the data is how many times the button |
BaserK | 2:e51a43447d68 | 12 | * is pressed and when it is pressed in terms of real time. It can also |
BaserK | 2:e51a43447d68 | 13 | * display the button count as 4-bit binary number using 4 leds on the LPC1768. |
BaserK | 2:e51a43447d68 | 14 | * There are two different time settings on the code. One can set the time from |
BaserK | 2:e51a43447d68 | 15 | * the terminal or manually by uncommenting the corresponding portion of code. |
BaserK | 2:e51a43447d68 | 16 | * Once the time is set, if there is a battery connected to real-time clock, |
BaserK | 2:e51a43447d68 | 17 | * RTC will continue to keep track of current time. In order not to reset the |
BaserK | 2:e51a43447d68 | 18 | * real-time to its initial value every time microcontroller is reset, it is |
BaserK | 2:e51a43447d68 | 19 | * neccessary to set the time once and delete the portion of code which sets |
BaserK | 2:e51a43447d68 | 20 | * the time and keep the rest. |
BaserK | 0:a5ea226b5b26 | 21 | * |
BaserK | 0:a5ea226b5b26 | 22 | * @connections: |
BaserK | 0:a5ea226b5b26 | 23 | *-------------------------------------------------------------- |
BaserK | 2:e51a43447d68 | 24 | * |LPC1768| |Peripherals| |
BaserK | 0:a5ea226b5b26 | 25 | * Pin 13 --------> (TX) RX pin of the FTDI or Bluetooth etc. |
BaserK | 0:a5ea226b5b26 | 26 | * Pin 14 --------> (RX) TX pin of the FTDI or Bluetooth etc. |
BaserK | 0:a5ea226b5b26 | 27 | * Pin 20 --------> Pull-up connected button |
BaserK | 0:a5ea226b5b26 | 28 | * GND -----------> GND of any peripheral |
BaserK | 0:a5ea226b5b26 | 29 | * VOUT (3.3 V) --> Pull-up resistor |
BaserK | 0:a5ea226b5b26 | 30 | *--------------------------------------------------------------- |
BaserK | 0:a5ea226b5b26 | 31 | * |
BaserK | 0:a5ea226b5b26 | 32 | */ |
BaserK | 0:a5ea226b5b26 | 33 | |
BaserK | 0:a5ea226b5b26 | 34 | #include "mbed.h" |
BaserK | 0:a5ea226b5b26 | 35 | #include "ledCounter.h" |
BaserK | 0:a5ea226b5b26 | 36 | |
BaserK | 0:a5ea226b5b26 | 37 | Serial ftdi(p13, p14); // setup terminal link |
BaserK | 0:a5ea226b5b26 | 38 | LocalFileSystem local("local"); // define local file system |
BaserK | 0:a5ea226b5b26 | 39 | DigitalIn button(p20); // pull-up button is connected to p20 |
BaserK | 0:a5ea226b5b26 | 40 | |
BaserK | 0:a5ea226b5b26 | 41 | int count=0; // button count |
BaserK | 0:a5ea226b5b26 | 42 | struct tm t; // current time will be stored here |
BaserK | 0:a5ea226b5b26 | 43 | |
BaserK | 0:a5ea226b5b26 | 44 | int main() |
BaserK | 0:a5ea226b5b26 | 45 | { |
BaserK | 0:a5ea226b5b26 | 46 | /* UNCOMMENT BELOW if you want to set the time from the terminal. */ |
BaserK | 0:a5ea226b5b26 | 47 | /* |
BaserK | 0:a5ea226b5b26 | 48 | ftdi.printf("Enter current date and time:\r\n"); |
BaserK | 0:a5ea226b5b26 | 49 | ftdi.printf("YYYY MM DD HH MM SS[enter]\r\n\r\n"); |
BaserK | 0:a5ea226b5b26 | 50 | ftdi.scanf("%d %d %d %d %d %d", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec); |
BaserK | 0:a5ea226b5b26 | 51 | ftdi.printf("-------------------------- \r\n"); |
BaserK | 0:a5ea226b5b26 | 52 | |
BaserK | 0:a5ea226b5b26 | 53 | t.tm_year = t.tm_year - 1900; // adjust for tm structure required values |
BaserK | 0:a5ea226b5b26 | 54 | t.tm_mon = t.tm_mon - 1; |
BaserK | 0:a5ea226b5b26 | 55 | |
BaserK | 0:a5ea226b5b26 | 56 | set_time(mktime(&t)); // set the time |
BaserK | 0:a5ea226b5b26 | 57 | */ |
BaserK | 0:a5ea226b5b26 | 58 | |
BaserK | 0:a5ea226b5b26 | 59 | /* UNCOMMENT BELOW if you want to set time manually */ |
BaserK | 0:a5ea226b5b26 | 60 | /* |
BaserK | 0:a5ea226b5b26 | 61 | t.tm_year = 2015; // current year |
BaserK | 0:a5ea226b5b26 | 62 | t.tm_mon = 7; // current month |
BaserK | 0:a5ea226b5b26 | 63 | t.tm_mday = 7; // current day |
BaserK | 0:a5ea226b5b26 | 64 | t.tm_hour = 15; // current hour |
BaserK | 0:a5ea226b5b26 | 65 | t.tm_min = 16; // current minute |
BaserK | 0:a5ea226b5b26 | 66 | t.tm_sec = 0; // current second |
BaserK | 0:a5ea226b5b26 | 67 | |
BaserK | 0:a5ea226b5b26 | 68 | t.tm_year = t.tm_year - 1900; // adjust for tm structure required values |
BaserK | 0:a5ea226b5b26 | 69 | t.tm_mon = t.tm_mon - 1; |
BaserK | 0:a5ea226b5b26 | 70 | |
BaserK | 0:a5ea226b5b26 | 71 | set_time(mktime(&t)); // set the time |
BaserK | 0:a5ea226b5b26 | 72 | */ |
BaserK | 0:a5ea226b5b26 | 73 | |
BaserK | 0:a5ea226b5b26 | 74 | /* Once the time is set, if there is a battery (3V) connected to VB pin and GND, RTC will remember the exact time. */ |
BaserK | 0:a5ea226b5b26 | 75 | |
BaserK | 0:a5ea226b5b26 | 76 | while(1) |
BaserK | 0:a5ea226b5b26 | 77 | { |
BaserK | 0:a5ea226b5b26 | 78 | time_t seconds = time(NULL); |
BaserK | 0:a5ea226b5b26 | 79 | |
BaserK | 0:a5ea226b5b26 | 80 | if(button.read()==0) // if the button is pressed |
BaserK | 0:a5ea226b5b26 | 81 | { |
BaserK | 0:a5ea226b5b26 | 82 | while(button.read()==0); // wait until release |
BaserK | 0:a5ea226b5b26 | 83 | wait_ms(20); // button debounce |
BaserK | 0:a5ea226b5b26 | 84 | count++; // count up |
BaserK | 0:a5ea226b5b26 | 85 | |
BaserK | 0:a5ea226b5b26 | 86 | FILE* Logfile = fopen ("/local/log.txt","a"); // open file for appending |
BaserK | 0:a5ea226b5b26 | 87 | |
BaserK | 0:a5ea226b5b26 | 88 | ftdi.printf("Time: %s \rButton count: %d \r\n",ctime(&seconds),count); // send data to terminal |
BaserK | 0:a5ea226b5b26 | 89 | ftdi.printf("-------------------------- \r\n"); |
BaserK | 0:a5ea226b5b26 | 90 | |
BaserK | 0:a5ea226b5b26 | 91 | fprintf(Logfile, "Time: %s \rButton count: %d \r\n",ctime(&seconds),count); // save data to log.txt file |
BaserK | 0:a5ea226b5b26 | 92 | fprintf(Logfile, "-------------------------- \r\n"); |
BaserK | 0:a5ea226b5b26 | 93 | fclose(Logfile); |
BaserK | 0:a5ea226b5b26 | 94 | } |
BaserK | 0:a5ea226b5b26 | 95 | ledCounter(count); // display the count as binary |
BaserK | 0:a5ea226b5b26 | 96 | } |
BaserK | 0:a5ea226b5b26 | 97 | } |