Hotboards MX / Mbed 2 deprecated Hotboards_rtcc_timeSpan

Dependencies:   Hotboards_rtcc mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002   Hotboards_rtcc Library - setting time and date
00003  Demonstrates the use a MCP7941x clock calendar.  The Hotboards_rtcc
00004  
00005  library works with this microchip real time clock
00006  (http://www.hotboards.org).
00007  
00008  This sketch demonstrates how add and substract time values using
00009  the time span objects
00010  
00011   The circuit:
00012  *  VDD  -->  3.3v
00013  *  GND  -->  GND
00014  *  SDA  -->  SDA
00015  *  SCL  -->  SCL
00016 */
00017 #include "mbed.h"
00018 #include "Hotboards_rtcc.h"
00019 
00020 /* days of the week */
00021 const char *week[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
00022 /* months of the year */
00023 const char *months[] = {"JAN","FEB","MAR","APR","MAY","JUN", "JUL", "AUG","SEPT","OCT","NOV","DEC"};
00024 
00025 /*serial port init*/
00026 Serial pc(USBTX,USBRX);
00027 /*i2c instance delaration for use with the rtcc library*/
00028 I2C device(PB_9, PB_8);
00029 /*lets declare and rtcc instance */
00030 Hotboards_rtcc rtcc(device);
00031 
00032 /*function for display date time */
00033 void display_DateTime( DateTime &dt )
00034 {
00035  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());
00036 }
00037 
00038 
00039 int main() 
00040 {
00041     /*i2c bus clock set to 100khz*/
00042    device.frequency(100000);
00043    /* init the rtcc, just enable the clock if not already enable */
00044    rtcc.begin();
00045    /* set the time (15:30:00) and date 1/MAR/2001 */
00046    rtcc.adjust( DateTime( 2001, 2, 1, 15, 30, 0 ) );
00047    
00048     while(1) 
00049     {
00050        /* get the actual time and date */
00051        DateTime time = rtcc.now( );
00052        /* display through serial port*/
00053        printf( "Actual time   ");
00054        display_DateTime( time);
00055       
00056        DateTime delta = time + TimeSpan(1, 0, 0, 0); // One day later with TimeSpan addition.
00057        printf( "1 day later   ");
00058        display_DateTime( delta);
00059        
00060        delta = time + TimeSpan(7, 0, 0, 0); // One week later with TimeSpan addition.
00061        printf( "one week later");
00062        display_DateTime( delta);
00063       
00064        delta = time + TimeSpan(0, 0, 30, 10); // Fourty two minutes and fourty two seconds later.
00065        printf( "00:42:42 later");
00066        display_DateTime( delta);
00067       
00068        delta = time - TimeSpan(7, 0, 0, 0);  // One week ago.
00069        printf( "one week ago  ");
00070        display_DateTime( delta);
00071        
00072        printf( "\n");
00073        /* wait ten seconds */
00074        wait( 10 );
00075     }
00076 }