This sketch demonstrates how add and substract time values using the time span objects

Dependencies:   Hotboards_rtcc mbed

Committer:
Hotboards
Date:
Wed Feb 10 15:49:52 2016 +0000
Revision:
0:5e1b09cde0a8
first release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Hotboards 0:5e1b09cde0a8 1 /*
Hotboards 0:5e1b09cde0a8 2 Hotboards_rtcc Library - setting time and date
Hotboards 0:5e1b09cde0a8 3 Demonstrates the use a MCP7941x clock calendar. The Hotboards_rtcc
Hotboards 0:5e1b09cde0a8 4
Hotboards 0:5e1b09cde0a8 5 library works with this microchip real time clock
Hotboards 0:5e1b09cde0a8 6 (http://www.hotboards.org).
Hotboards 0:5e1b09cde0a8 7
Hotboards 0:5e1b09cde0a8 8 This sketch demonstrates how add and substract time values using
Hotboards 0:5e1b09cde0a8 9 the time span objects
Hotboards 0:5e1b09cde0a8 10
Hotboards 0:5e1b09cde0a8 11 The circuit:
Hotboards 0:5e1b09cde0a8 12 * VDD --> 3.3v
Hotboards 0:5e1b09cde0a8 13 * GND --> GND
Hotboards 0:5e1b09cde0a8 14 * SDA --> SDA
Hotboards 0:5e1b09cde0a8 15 * SCL --> SCL
Hotboards 0:5e1b09cde0a8 16 */
Hotboards 0:5e1b09cde0a8 17 #include "mbed.h"
Hotboards 0:5e1b09cde0a8 18 #include "Hotboards_rtcc.h"
Hotboards 0:5e1b09cde0a8 19
Hotboards 0:5e1b09cde0a8 20 /* days of the week */
Hotboards 0:5e1b09cde0a8 21 const char *week[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Hotboards 0:5e1b09cde0a8 22 /* months of the year */
Hotboards 0:5e1b09cde0a8 23 const char *months[] = {"JAN","FEB","MAR","APR","MAY","JUN", "JUL", "AUG","SEPT","OCT","NOV","DEC"};
Hotboards 0:5e1b09cde0a8 24
Hotboards 0:5e1b09cde0a8 25 /*serial port init*/
Hotboards 0:5e1b09cde0a8 26 Serial pc(USBTX,USBRX);
Hotboards 0:5e1b09cde0a8 27 /*i2c instance delaration for use with the rtcc library*/
Hotboards 0:5e1b09cde0a8 28 I2C device(PB_9, PB_8);
Hotboards 0:5e1b09cde0a8 29 /*lets declare and rtcc instance */
Hotboards 0:5e1b09cde0a8 30 Hotboards_rtcc rtcc(device);
Hotboards 0:5e1b09cde0a8 31
Hotboards 0:5e1b09cde0a8 32 /*function for display date time */
Hotboards 0:5e1b09cde0a8 33 void display_DateTime( DateTime &dt )
Hotboards 0:5e1b09cde0a8 34 {
Hotboards 0:5e1b09cde0a8 35 printf( " Time- %d:%d:%d Date- %s/%d/%s/%d \n",dt.hour( ),dt.minute( ),dt.second( ),week[dt.dayOfTheWeek( )],dt.day( ),months[dt.month( )],dt.year());
Hotboards 0:5e1b09cde0a8 36 }
Hotboards 0:5e1b09cde0a8 37
Hotboards 0:5e1b09cde0a8 38
Hotboards 0:5e1b09cde0a8 39 int main()
Hotboards 0:5e1b09cde0a8 40 {
Hotboards 0:5e1b09cde0a8 41 /*i2c bus clock set to 100khz*/
Hotboards 0:5e1b09cde0a8 42 device.frequency(100000);
Hotboards 0:5e1b09cde0a8 43 /* init the rtcc, just enable the clock if not already enable */
Hotboards 0:5e1b09cde0a8 44 rtcc.begin();
Hotboards 0:5e1b09cde0a8 45 /* set the time (15:30:00) and date 1/MAR/2001 */
Hotboards 0:5e1b09cde0a8 46 rtcc.adjust( DateTime( 2001, 2, 1, 15, 30, 0 ) );
Hotboards 0:5e1b09cde0a8 47
Hotboards 0:5e1b09cde0a8 48 while(1)
Hotboards 0:5e1b09cde0a8 49 {
Hotboards 0:5e1b09cde0a8 50 /* get the actual time and date */
Hotboards 0:5e1b09cde0a8 51 DateTime time = rtcc.now( );
Hotboards 0:5e1b09cde0a8 52 /* display through serial port*/
Hotboards 0:5e1b09cde0a8 53 printf( "Actual time ");
Hotboards 0:5e1b09cde0a8 54 display_DateTime( time);
Hotboards 0:5e1b09cde0a8 55
Hotboards 0:5e1b09cde0a8 56 DateTime delta = time + TimeSpan(1, 0, 0, 0); // One day later with TimeSpan addition.
Hotboards 0:5e1b09cde0a8 57 printf( "1 day later ");
Hotboards 0:5e1b09cde0a8 58 display_DateTime( delta);
Hotboards 0:5e1b09cde0a8 59
Hotboards 0:5e1b09cde0a8 60 delta = time + TimeSpan(7, 0, 0, 0); // One week later with TimeSpan addition.
Hotboards 0:5e1b09cde0a8 61 printf( "one week later");
Hotboards 0:5e1b09cde0a8 62 display_DateTime( delta);
Hotboards 0:5e1b09cde0a8 63
Hotboards 0:5e1b09cde0a8 64 delta = time + TimeSpan(0, 0, 30, 10); // Fourty two minutes and fourty two seconds later.
Hotboards 0:5e1b09cde0a8 65 printf( "00:42:42 later");
Hotboards 0:5e1b09cde0a8 66 display_DateTime( delta);
Hotboards 0:5e1b09cde0a8 67
Hotboards 0:5e1b09cde0a8 68 delta = time - TimeSpan(7, 0, 0, 0); // One week ago.
Hotboards 0:5e1b09cde0a8 69 printf( "one week ago ");
Hotboards 0:5e1b09cde0a8 70 display_DateTime( delta);
Hotboards 0:5e1b09cde0a8 71
Hotboards 0:5e1b09cde0a8 72 printf( "\n");
Hotboards 0:5e1b09cde0a8 73 /* wait ten seconds */
Hotboards 0:5e1b09cde0a8 74 wait( 10 );
Hotboards 0:5e1b09cde0a8 75 }
Hotboards 0:5e1b09cde0a8 76 }