"PCF2127A" : RTC chip with TCXO and quartz crystal demo Please refer >> http://mbed.org/users/okano/notebook/nxp_pcf2172a-demo-code/

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  *  PCF2127A (Integrated RTC, TCXO and quartz crystal) demo
00003  *
00004  *  PCF2127A is a "real time clock (RTC)" module which is including a Xtal and TCXO 
00005  *  http://www.nxp.com/pip/PCF2127A_2.html
00006  *
00007  *  This is a just simple operation sample of the PCF2127A.
00008  *  In this sample, the PCF2127A is interfaced by I2C through pin9 and 10 of mbed. 
00009  *  And also the mbed-pin8 is connected to RTC's /INT pin. 
00010  *  The RTC chip is set to generate periodical interrupt in every seconds. 
00011  *  This interrupt triggers the update of the terminal and LCD screen. 
00012  *  
00013  *  RTC initializing part is ported from...
00014  *    http://mbed.org/users/roen/notebook/real-time/
00015  *
00016  *  Released under the MIT License: http://mbed.org/license/mit
00017  *
00018  *  revision 1.0    05-Jun-2010     (a) 1st release
00019  *  revision 1.1    05-Jun-2010     (a) class name changed
00020  *                              
00021  */
00022 
00023 #include "mbed.h"
00024 #include "TextLCD.h"
00025 #include "NXP_PCF2127A.h"
00026 
00027 TextLCD         lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3
00028 NXP_PCF2127A    rtc( p9, p10 );
00029 InterruptIn     intr( p8 );
00030 BusOut          leds( LED4, LED3, LED2, LED1 );
00031 
00032 void time_intr( void );
00033 void led_ctrl( void );
00034 
00035 int main() {
00036     printf( "----------------- PCF2127 demo started.\r\n" );
00037 
00038     leds    = 1;
00039 
00040     if ( rtc.is_init_required() ) {
00041         lcd.locate( 0, 0 );
00042         lcd.printf( "please set time from terminal" );
00043         
00044         rtc.set_time();
00045         
00046         lcd.cls();
00047     }
00048 
00049     intr.fall( &time_intr );
00050 
00051     while ( 1 )
00052         ;
00053 }
00054 
00055 
00056 void time_intr( void ) {
00057     struct tm   dt, *dtp;
00058     time_t      t;
00059     char        s[ 30 ];
00060     dtp = &dt;
00061 
00062     rtc.clear_intr();
00063 
00064     t       = rtc.time( NULL );
00065     dtp     = localtime( &t );
00066 
00067     strftime( s, 30, "%H:%M:%S, %Y/%b/%d %a", dtp );
00068     printf( "%s\r\n", s );
00069 
00070     strftime( s, 20, "%H:%M:%S PCF2127", dtp );
00071     lcd.locate( 0, 0 );
00072     lcd.printf( "%s", s );
00073 
00074     strftime( s, 20, "%Y/%b/%d(%a)", dtp );
00075     lcd.locate( 0, 1 );
00076     lcd.printf( "%s", s );
00077     
00078     led_ctrl();
00079 }
00080 
00081 
00082 void led_ctrl( void )
00083 {
00084     static char direction   = true;
00085     
00086     leds        = direction ? leds << 1 : leds >> 1;
00087     direction   = (leds & 0x9) ? !direction : direction;
00088 }
00089