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@3:192d65ce0c19, 2015-07-21 (annotated)
- Committer:
- BaserK
- Date:
- Tue Jul 21 08:04:41 2015 +0000
- Revision:
- 3:192d65ce0c19
- Parent:
- 2:e51a43447d68
MIT license is added
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 | 3:192d65ce0c19 | 5 | * @license: MIT license |
BaserK | 3:192d65ce0c19 | 6 | * |
BaserK | 3:192d65ce0c19 | 7 | * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr |
BaserK | 3:192d65ce0c19 | 8 | * |
BaserK | 3:192d65ce0c19 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
BaserK | 3:192d65ce0c19 | 10 | * of this software and associated documentation files (the "Software"), to deal |
BaserK | 3:192d65ce0c19 | 11 | * in the Software without restriction, including without limitation the rights |
BaserK | 3:192d65ce0c19 | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BaserK | 3:192d65ce0c19 | 13 | * copies of the Software, and to permit persons to whom the Software is |
BaserK | 3:192d65ce0c19 | 14 | * furnished to do so, subject to the following conditions: |
BaserK | 3:192d65ce0c19 | 15 | * |
BaserK | 3:192d65ce0c19 | 16 | * The above copyright notice and this permission notice shall be included in |
BaserK | 3:192d65ce0c19 | 17 | * all copies or substantial portions of the Software. |
BaserK | 3:192d65ce0c19 | 18 | * |
BaserK | 3:192d65ce0c19 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BaserK | 3:192d65ce0c19 | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BaserK | 3:192d65ce0c19 | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BaserK | 3:192d65ce0c19 | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BaserK | 3:192d65ce0c19 | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BaserK | 3:192d65ce0c19 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BaserK | 3:192d65ce0c19 | 25 | * THE SOFTWARE. |
BaserK | 3:192d65ce0c19 | 26 | * |
BaserK | 0:a5ea226b5b26 | 27 | * @description of the program: |
BaserK | 2:e51a43447d68 | 28 | * |
BaserK | 2:e51a43447d68 | 29 | * This program can log data that is read from RTC(real time clock) which |
BaserK | 2:e51a43447d68 | 30 | * is embedded inside LPC1768. It saves the data to the LPC1768’s memory as |
BaserK | 2:e51a43447d68 | 31 | * “log.txt”. For this example program, the data is how many times the button |
BaserK | 2:e51a43447d68 | 32 | * is pressed and when it is pressed in terms of real time. It can also |
BaserK | 2:e51a43447d68 | 33 | * display the button count as 4-bit binary number using 4 leds on the LPC1768. |
BaserK | 2:e51a43447d68 | 34 | * There are two different time settings on the code. One can set the time from |
BaserK | 2:e51a43447d68 | 35 | * the terminal or manually by uncommenting the corresponding portion of code. |
BaserK | 2:e51a43447d68 | 36 | * Once the time is set, if there is a battery connected to real-time clock, |
BaserK | 2:e51a43447d68 | 37 | * RTC will continue to keep track of current time. In order not to reset the |
BaserK | 2:e51a43447d68 | 38 | * real-time to its initial value every time microcontroller is reset, it is |
BaserK | 2:e51a43447d68 | 39 | * neccessary to set the time once and delete the portion of code which sets |
BaserK | 2:e51a43447d68 | 40 | * the time and keep the rest. |
BaserK | 0:a5ea226b5b26 | 41 | * |
BaserK | 0:a5ea226b5b26 | 42 | * @connections: |
BaserK | 0:a5ea226b5b26 | 43 | *-------------------------------------------------------------- |
BaserK | 2:e51a43447d68 | 44 | * |LPC1768| |Peripherals| |
BaserK | 0:a5ea226b5b26 | 45 | * Pin 13 --------> (TX) RX pin of the FTDI or Bluetooth etc. |
BaserK | 0:a5ea226b5b26 | 46 | * Pin 14 --------> (RX) TX pin of the FTDI or Bluetooth etc. |
BaserK | 0:a5ea226b5b26 | 47 | * Pin 20 --------> Pull-up connected button |
BaserK | 0:a5ea226b5b26 | 48 | * GND -----------> GND of any peripheral |
BaserK | 0:a5ea226b5b26 | 49 | * VOUT (3.3 V) --> Pull-up resistor |
BaserK | 0:a5ea226b5b26 | 50 | *--------------------------------------------------------------- |
BaserK | 0:a5ea226b5b26 | 51 | * |
BaserK | 0:a5ea226b5b26 | 52 | */ |
BaserK | 0:a5ea226b5b26 | 53 | |
BaserK | 0:a5ea226b5b26 | 54 | #include "mbed.h" |
BaserK | 0:a5ea226b5b26 | 55 | #include "ledCounter.h" |
BaserK | 0:a5ea226b5b26 | 56 | |
BaserK | 0:a5ea226b5b26 | 57 | Serial ftdi(p13, p14); // setup terminal link |
BaserK | 0:a5ea226b5b26 | 58 | LocalFileSystem local("local"); // define local file system |
BaserK | 0:a5ea226b5b26 | 59 | DigitalIn button(p20); // pull-up button is connected to p20 |
BaserK | 0:a5ea226b5b26 | 60 | |
BaserK | 0:a5ea226b5b26 | 61 | int count=0; // button count |
BaserK | 0:a5ea226b5b26 | 62 | struct tm t; // current time will be stored here |
BaserK | 0:a5ea226b5b26 | 63 | |
BaserK | 0:a5ea226b5b26 | 64 | int main() |
BaserK | 0:a5ea226b5b26 | 65 | { |
BaserK | 0:a5ea226b5b26 | 66 | /* UNCOMMENT BELOW if you want to set the time from the terminal. */ |
BaserK | 0:a5ea226b5b26 | 67 | /* |
BaserK | 0:a5ea226b5b26 | 68 | ftdi.printf("Enter current date and time:\r\n"); |
BaserK | 0:a5ea226b5b26 | 69 | ftdi.printf("YYYY MM DD HH MM SS[enter]\r\n\r\n"); |
BaserK | 0:a5ea226b5b26 | 70 | 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 | 71 | ftdi.printf("-------------------------- \r\n"); |
BaserK | 0:a5ea226b5b26 | 72 | |
BaserK | 0:a5ea226b5b26 | 73 | t.tm_year = t.tm_year - 1900; // adjust for tm structure required values |
BaserK | 0:a5ea226b5b26 | 74 | t.tm_mon = t.tm_mon - 1; |
BaserK | 0:a5ea226b5b26 | 75 | |
BaserK | 0:a5ea226b5b26 | 76 | set_time(mktime(&t)); // set the time |
BaserK | 0:a5ea226b5b26 | 77 | */ |
BaserK | 0:a5ea226b5b26 | 78 | |
BaserK | 0:a5ea226b5b26 | 79 | /* UNCOMMENT BELOW if you want to set time manually */ |
BaserK | 0:a5ea226b5b26 | 80 | /* |
BaserK | 0:a5ea226b5b26 | 81 | t.tm_year = 2015; // current year |
BaserK | 0:a5ea226b5b26 | 82 | t.tm_mon = 7; // current month |
BaserK | 0:a5ea226b5b26 | 83 | t.tm_mday = 7; // current day |
BaserK | 0:a5ea226b5b26 | 84 | t.tm_hour = 15; // current hour |
BaserK | 0:a5ea226b5b26 | 85 | t.tm_min = 16; // current minute |
BaserK | 0:a5ea226b5b26 | 86 | t.tm_sec = 0; // current second |
BaserK | 0:a5ea226b5b26 | 87 | |
BaserK | 0:a5ea226b5b26 | 88 | t.tm_year = t.tm_year - 1900; // adjust for tm structure required values |
BaserK | 0:a5ea226b5b26 | 89 | t.tm_mon = t.tm_mon - 1; |
BaserK | 0:a5ea226b5b26 | 90 | |
BaserK | 0:a5ea226b5b26 | 91 | set_time(mktime(&t)); // set the time |
BaserK | 0:a5ea226b5b26 | 92 | */ |
BaserK | 0:a5ea226b5b26 | 93 | |
BaserK | 0:a5ea226b5b26 | 94 | /* 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 | 95 | |
BaserK | 0:a5ea226b5b26 | 96 | while(1) |
BaserK | 0:a5ea226b5b26 | 97 | { |
BaserK | 0:a5ea226b5b26 | 98 | time_t seconds = time(NULL); |
BaserK | 0:a5ea226b5b26 | 99 | |
BaserK | 0:a5ea226b5b26 | 100 | if(button.read()==0) // if the button is pressed |
BaserK | 0:a5ea226b5b26 | 101 | { |
BaserK | 0:a5ea226b5b26 | 102 | while(button.read()==0); // wait until release |
BaserK | 0:a5ea226b5b26 | 103 | wait_ms(20); // button debounce |
BaserK | 0:a5ea226b5b26 | 104 | count++; // count up |
BaserK | 0:a5ea226b5b26 | 105 | |
BaserK | 0:a5ea226b5b26 | 106 | FILE* Logfile = fopen ("/local/log.txt","a"); // open file for appending |
BaserK | 0:a5ea226b5b26 | 107 | |
BaserK | 0:a5ea226b5b26 | 108 | ftdi.printf("Time: %s \rButton count: %d \r\n",ctime(&seconds),count); // send data to terminal |
BaserK | 0:a5ea226b5b26 | 109 | ftdi.printf("-------------------------- \r\n"); |
BaserK | 0:a5ea226b5b26 | 110 | |
BaserK | 0:a5ea226b5b26 | 111 | fprintf(Logfile, "Time: %s \rButton count: %d \r\n",ctime(&seconds),count); // save data to log.txt file |
BaserK | 0:a5ea226b5b26 | 112 | fprintf(Logfile, "-------------------------- \r\n"); |
BaserK | 0:a5ea226b5b26 | 113 | fclose(Logfile); |
BaserK | 0:a5ea226b5b26 | 114 | } |
BaserK | 0:a5ea226b5b26 | 115 | ledCounter(count); // display the count as binary |
BaserK | 0:a5ea226b5b26 | 116 | } |
BaserK | 0:a5ea226b5b26 | 117 | } |