InetrfaceProducts NXP / Mbed 2 deprecated PCF2127_Hello

Dependencies:   PCF2127 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  *  A sample code for PCF2127 library
00003  *
00004  *  @author     Akifumi (Tedd) OKANO, NXP Semiconductors
00005  *  @version    1.8
00006  *  @date       10-Dec-2014
00007  *
00008  *  PCF2127 is a "real time clock (RTC)" module which is including a Xtal and TCXO
00009  *  http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html
00010  *
00011  *  RTC initializing part is ported from..
00012  *    http://mbed.org/users/roen/notebook/real-time/
00013  *
00014  *  This code is refined version of..
00015  *    http://developer.mbed.org/users/okano/code/NXP_PCF2127A/
00016  */
00017 
00018 #include "mbed.h"
00019 #include "PCF2127.h"
00020 
00021 PCF2127 rtc( p28, p27 );
00022 
00023 void    show_time( void );
00024 void    set_time( void );
00025 
00026 void    RAM_dump( void );       //  for PCF2127 only
00027 void    RAM_read_test( void );  //  for PCF2127 only
00028 void    RAM_write_test( void ); //  for PCF2127 only
00029 void    RAM_test( int last_RAM_content_invalid );       //  for PCF2127 only
00030 
00031 int main()
00032 {
00033     printf( "PCF2127 demo started.\r\n" );
00034 
00035     int clock_integrity_fail    = rtc.is_init_required();
00036 
00037     if ( clock_integrity_fail ) {
00038         set_time();
00039     }
00040 
00041     /*
00042      *  Next line can be enabled when using PCF2127 only
00043      */
00044     //RAM_test( clock_integrity_fail );
00045 
00046     while ( 1 ) {
00047         show_time();
00048         wait( 0.25 );
00049     }
00050 }
00051 
00052 void show_time( void )
00053 {
00054     struct tm   dt, *dtp;
00055     time_t      t;
00056     char        s[ 30 ];
00057 
00058     dtp     = &dt;
00059 
00060     rtc.clear_intr();
00061 
00062     //  get time information from PCF2127
00063     if ( PCF2127::TIME_FUNC_ERROR == (t   = rtc.time( NULL )) ) {
00064         printf( "error at reading time\r\n" );
00065         return;
00066     }
00067 
00068     dtp     = localtime( &t );
00069 
00070     //  print time and date on terminal
00071     strftime( s, 30, "%H:%M:%S, %Y/%b/%d %a", dtp );
00072     printf( "%s\r\n", s );
00073 }
00074 
00075 void set_time( void )
00076 {
00077 #define MAX_CHAR_LENGTH 21
00078     char    s[ MAX_CHAR_LENGTH ];
00079 
00080     printf( "Enter current date and time:\r\n" );
00081     printf( "YYYY MM DD HH MM SS[enter]\r\n" );
00082 
00083     fgets( s, MAX_CHAR_LENGTH, stdin );
00084     printf( "user input: \"%s\"\r\n", s );
00085 
00086     rtc.set_time( s );
00087 }
00088 
00089 void RAM_function_test( void )
00090 {
00091 }
00092 
00093 void RAM_dump( void )
00094 {
00095     char    b[ 512 ];
00096 
00097     if ( rtc.RAM_read( 0, b, 512 ) ) {
00098         printf( "error at reading RAM\r\n" );
00099     }
00100 
00101     for ( int i = 0; i < 512; i++ ) {
00102         if ( !(i % 16) ) {
00103             printf( "\r\n  0x%03X(%03d): 0x", i, i );
00104         }
00105         printf( " %02X", b[ i ] );
00106     }
00107 }
00108 
00109 const char *texts[] = {
00110     "Love the life you live. Live the life you love.",
00111     "It always seems impossible until it's done.",
00112     "You'll never find a rainbow if you're looking down.",
00113     "There is always light behind the clouds."
00114 };
00115 
00116 void RAM_test( int last_RAM_content_invalid )
00117 {
00118     char    txt[ 64 ];
00119     time_t  t;
00120 
00121     if ( !last_RAM_content_invalid ) {
00122         rtc.RAM_read( 0, (char *)&t, sizeof( t ) );
00123         rtc.RAM_read( 16, txt, sizeof( txt ) );
00124         printf( "RAM test read\r\n" );
00125         printf( "  last program start was on %s\r", ctime( &t ) );
00126         printf( "  last saved text was \"%s\"\r\n", txt );
00127     }
00128 
00129     t   = rtc.time( NULL );
00130     rtc.RAM_write( 0, (char *)&t, sizeof( t ) );
00131     rtc.RAM_write( 16, (char *)texts[ t % 4 ], strlen( texts[ t % 4 ] ) + 1 );
00132 }
00133