InetrfaceProducts NXP / Mbed 2 deprecated PCF2127_Hello

Dependencies:   PCF2127 mbed

Committer:
nxp_ip
Date:
Tue Dec 09 07:23:50 2014 +0000
Revision:
0:658f351580f0
Child:
1:86dc8dfe6a20
A sample code for PCF2127 library. Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxp_ip 0:658f351580f0 1 /*
nxp_ip 0:658f351580f0 2 * A sample code for PCF2127 library
nxp_ip 0:658f351580f0 3 *
nxp_ip 0:658f351580f0 4 * @author Akifumi (Tedd) OKANO, NXP Semiconductors
nxp_ip 0:658f351580f0 5 * @version 1.6
nxp_ip 0:658f351580f0 6 * @date 09-Dec-2014
nxp_ip 0:658f351580f0 7 *
nxp_ip 0:658f351580f0 8 * PCF2127 is a "real time clock (RTC)" module which is including a Xtal and TCXO
nxp_ip 0:658f351580f0 9 * http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html
nxp_ip 0:658f351580f0 10 *
nxp_ip 0:658f351580f0 11 * RTC initializing part is ported from..
nxp_ip 0:658f351580f0 12 * http://mbed.org/users/roen/notebook/real-time/
nxp_ip 0:658f351580f0 13 *
nxp_ip 0:658f351580f0 14 * This code is refined version of..
nxp_ip 0:658f351580f0 15 * http://developer.mbed.org/users/okano/code/NXP_PCF2127A/
nxp_ip 0:658f351580f0 16 */
nxp_ip 0:658f351580f0 17 */
nxp_ip 0:658f351580f0 18
nxp_ip 0:658f351580f0 19 #include "mbed.h"
nxp_ip 0:658f351580f0 20 #include "PCF2127.h"
nxp_ip 0:658f351580f0 21
nxp_ip 0:658f351580f0 22 PCF2127 rtc( p28, p27 );
nxp_ip 0:658f351580f0 23
nxp_ip 0:658f351580f0 24 void show_time( void );
nxp_ip 0:658f351580f0 25 void set_time( void );
nxp_ip 0:658f351580f0 26
nxp_ip 0:658f351580f0 27 int main()
nxp_ip 0:658f351580f0 28 {
nxp_ip 0:658f351580f0 29 printf( "PCF2127 demo started.\r\n" );
nxp_ip 0:658f351580f0 30
nxp_ip 0:658f351580f0 31 if ( rtc.is_init_required() ) {
nxp_ip 0:658f351580f0 32 set_time();
nxp_ip 0:658f351580f0 33 }
nxp_ip 0:658f351580f0 34
nxp_ip 0:658f351580f0 35 while ( 1 ) {
nxp_ip 0:658f351580f0 36 show_time();
nxp_ip 0:658f351580f0 37 wait( 0.25 );
nxp_ip 0:658f351580f0 38 }
nxp_ip 0:658f351580f0 39 }
nxp_ip 0:658f351580f0 40
nxp_ip 0:658f351580f0 41 void show_time( void )
nxp_ip 0:658f351580f0 42 {
nxp_ip 0:658f351580f0 43 struct tm dt, *dtp;
nxp_ip 0:658f351580f0 44 time_t t;
nxp_ip 0:658f351580f0 45 char s[ 30 ];
nxp_ip 0:658f351580f0 46
nxp_ip 0:658f351580f0 47 dtp = &dt;
nxp_ip 0:658f351580f0 48
nxp_ip 0:658f351580f0 49 rtc.clear_intr();
nxp_ip 0:658f351580f0 50
nxp_ip 0:658f351580f0 51 // get time information from PCF2127
nxp_ip 0:658f351580f0 52 if ( PCF2127::TIME_FUNC_ERROR == (t = rtc.time( NULL )) )
nxp_ip 0:658f351580f0 53 {
nxp_ip 0:658f351580f0 54 printf( "error at reading time\r\n" );
nxp_ip 0:658f351580f0 55 return;
nxp_ip 0:658f351580f0 56 }
nxp_ip 0:658f351580f0 57
nxp_ip 0:658f351580f0 58 dtp = localtime( &t );
nxp_ip 0:658f351580f0 59
nxp_ip 0:658f351580f0 60 // print time and date on terminal
nxp_ip 0:658f351580f0 61 strftime( s, 30, "%H:%M:%S, %Y/%b/%d %a", dtp );
nxp_ip 0:658f351580f0 62 printf( "%s\r\n", s );
nxp_ip 0:658f351580f0 63 }
nxp_ip 0:658f351580f0 64
nxp_ip 0:658f351580f0 65 void set_time( void )
nxp_ip 0:658f351580f0 66 {
nxp_ip 0:658f351580f0 67 #define MAX_CHAR_LENGTH 21
nxp_ip 0:658f351580f0 68 char s[ MAX_CHAR_LENGTH ];
nxp_ip 0:658f351580f0 69
nxp_ip 0:658f351580f0 70 printf( "Enter current date and time:\r\n" );
nxp_ip 0:658f351580f0 71 printf( "YYYY MM DD HH MM SS[enter]\r\n" );
nxp_ip 0:658f351580f0 72
nxp_ip 0:658f351580f0 73 fgets( s, MAX_CHAR_LENGTH, stdin );
nxp_ip 0:658f351580f0 74 printf( "user input: \"%s\"\r\n", s );
nxp_ip 0:658f351580f0 75
nxp_ip 0:658f351580f0 76 rtc.set_time( s );
nxp_ip 0:658f351580f0 77 }