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