Demo code for PCF2127 (and PCF2129) library. This code can be executed with LCD screen on AppBoard. PCF2127 and PCF2129 are high accuracy real-time-clock (RTC) module. This library provides simple interface to accessing clock information.

Dependencies:   C12832 PCF2127 mbed

Demo code for PCF2127 library

Information

This demo code works with PC terminal and/or LCD screen on "mbed application board"

Information

The "PCF2127" library works for PCF2127T, PCF2127AT, PCF2129T and PCF2129AT.

The device information is available on the component page and the library page.

PCF2127T
PCF2127T is in SO16 package

Committer:
nxp_ip
Date:
Thu Dec 04 04:59:20 2014 +0000
Revision:
0:8de826ed7578
initial version

Who changed what in which revision?

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