PCF2127 and PCF2129 are high accuracy real-time-clock (RTC) module. This library provides simple interface to accessing clock information.

Dependents:   PCF2127_Demo PCF2127_Hello

PCF2127 and PCF2129

PCF2127T PCF2127T is in SO16 package

The PCF2127 and the PCF2129 are a CMOS Real Time Clock (RTC) and calendar with an integrated Temperature Compensated Crystal (Xtal) Oscillator (TCXO) and a 32.768 kHz quartz crystal optimized for very high accuracy and very low power consumption.
Both of PCF2127 and PCF2129 have a selectable I2C-bus or SPI-bus, a backup battery switch-over circuit, a programmable watchdog function, a timestamp function, and many other features.
On addition to this, the PCF2127 has 512 bytes of general-purpose static RAM.

These 4 types of RTC modules are software compatible. So this library "PCF2127" can be used all of those.
This library only supports I2C to communicate with the PCF2127/PCF2129.

Type variations

Main feature difference

type+/-3ppm accuracy range512 bytes RAMpackage
PCF2127T-30℃ to +80℃yesSO16
PCF2127AT-30℃ to +60℃yesSO20
PCF2129T-30℃ to +80℃not availableSO16
PCF2129AT-15℃ to +60℃not availableSO20

Pin assign

/media/uploads/nxp_ip/pcf2127_pcf2129_pin_assign.png

PCF2127T
PCF2127T

Connection between MCU and PCF2127/PCF2129

These examples show how the RTC module can be connected via I2C bus.

http://developer.mbed.org/media/components/pinouts/both_types_connection3.png

References

Committer:
nxp_ip
Date:
Thu Dec 04 04:58:24 2014 +0000
Revision:
0:1377bcf1455e
Child:
1:700e0285cfd8
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nxp_ip 0:1377bcf1455e 1 /*
nxp_ip 0:1377bcf1455e 2 * PCF2127 library
nxp_ip 0:1377bcf1455e 3 *
nxp_ip 0:1377bcf1455e 4 * @author Akifumi (Tedd) OKANO, NXP Semiconductors
nxp_ip 0:1377bcf1455e 5 * @version 1.5
nxp_ip 0:1377bcf1455e 6 * @date 04-Dec-2014
nxp_ip 0:1377bcf1455e 7 *
nxp_ip 0:1377bcf1455e 8 * PCF2127 is a "real time clock (RTC)" module which is including a Xtal and TCXO
nxp_ip 0:1377bcf1455e 9 * http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html
nxp_ip 0:1377bcf1455e 10 *
nxp_ip 0:1377bcf1455e 11 * RTC initializing part is ported from..
nxp_ip 0:1377bcf1455e 12 * http://mbed.org/users/roen/notebook/real-time/
nxp_ip 0:1377bcf1455e 13 *
nxp_ip 0:1377bcf1455e 14 * This code is refined version of..
nxp_ip 0:1377bcf1455e 15 * http://developer.mbed.org/users/okano/code/NXP_PCF2127A/
nxp_ip 0:1377bcf1455e 16 */
nxp_ip 0:1377bcf1455e 17
nxp_ip 0:1377bcf1455e 18 #include "mbed.h"
nxp_ip 0:1377bcf1455e 19 #include "PCF2127.h"
nxp_ip 0:1377bcf1455e 20
nxp_ip 0:1377bcf1455e 21 #define PCF2127_I2C_SLAVE_ADDRESS 0xA2
nxp_ip 0:1377bcf1455e 22
nxp_ip 0:1377bcf1455e 23 PCF2127::PCF2127( PinName sda, PinName sdl, char vControl_1, char vControl_2, char vControl_3 )
nxp_ip 0:1377bcf1455e 24 : i2c( sda, sdl ), device_address( PCF2127_I2C_SLAVE_ADDRESS )
nxp_ip 0:1377bcf1455e 25 {
nxp_ip 0:1377bcf1455e 26 set_register( Control_1, vControl_1 );
nxp_ip 0:1377bcf1455e 27 set_register( Control_2, vControl_2 );
nxp_ip 0:1377bcf1455e 28 set_register( Control_3, vControl_3 );
nxp_ip 0:1377bcf1455e 29 }
nxp_ip 0:1377bcf1455e 30
nxp_ip 0:1377bcf1455e 31 PCF2127::~PCF2127()
nxp_ip 0:1377bcf1455e 32 {
nxp_ip 0:1377bcf1455e 33 }
nxp_ip 0:1377bcf1455e 34
nxp_ip 0:1377bcf1455e 35 int PCF2127::is_init_required( void )
nxp_ip 0:1377bcf1455e 36 {
nxp_ip 0:1377bcf1455e 37 return ( read_register( Seconds ) & 0x80 ? true : false );
nxp_ip 0:1377bcf1455e 38 }
nxp_ip 0:1377bcf1455e 39
nxp_ip 0:1377bcf1455e 40 void PCF2127::set_time( struct tm *dtp )
nxp_ip 0:1377bcf1455e 41 {
nxp_ip 0:1377bcf1455e 42 char buf[ 8 ];
nxp_ip 0:1377bcf1455e 43 char c;
nxp_ip 0:1377bcf1455e 44
nxp_ip 0:1377bcf1455e 45 buf[ 0 ] = Seconds;
nxp_ip 0:1377bcf1455e 46 buf[ 1 ] = i2bcd( dtp->tm_sec );
nxp_ip 0:1377bcf1455e 47 buf[ 2 ] = i2bcd( dtp->tm_min );
nxp_ip 0:1377bcf1455e 48 buf[ 3 ] = i2bcd( dtp->tm_hour );
nxp_ip 0:1377bcf1455e 49 buf[ 4 ] = i2bcd( dtp->tm_mday );
nxp_ip 0:1377bcf1455e 50 buf[ 5 ] = i2bcd( dtp->tm_wday );
nxp_ip 0:1377bcf1455e 51 buf[ 6 ] = i2bcd( dtp->tm_mon + 1 );
nxp_ip 0:1377bcf1455e 52 buf[ 7 ] = i2bcd( dtp->tm_year - 100 );
nxp_ip 0:1377bcf1455e 53
nxp_ip 0:1377bcf1455e 54 c = read_register( Seconds );
nxp_ip 0:1377bcf1455e 55 while ( c == read_register( Seconds ) )
nxp_ip 0:1377bcf1455e 56 ;
nxp_ip 0:1377bcf1455e 57
nxp_ip 0:1377bcf1455e 58 i2c.write( device_address, buf, 8 );
nxp_ip 0:1377bcf1455e 59 }
nxp_ip 0:1377bcf1455e 60
nxp_ip 0:1377bcf1455e 61 void PCF2127::set_time( char *s )
nxp_ip 0:1377bcf1455e 62 {
nxp_ip 0:1377bcf1455e 63 // The time information should be given in format of "YYYY MM DD HH MM SS"
nxp_ip 0:1377bcf1455e 64
nxp_ip 0:1377bcf1455e 65 struct tm dt, *dtp;
nxp_ip 0:1377bcf1455e 66
nxp_ip 0:1377bcf1455e 67 dtp = &dt;
nxp_ip 0:1377bcf1455e 68
nxp_ip 0:1377bcf1455e 69 sscanf( s, "%d %d %d %d %d %d", &(dtp->tm_year), &(dtp->tm_mon), &(dtp->tm_mday), &(dtp->tm_hour), &(dtp->tm_min), &(dtp->tm_sec) );
nxp_ip 0:1377bcf1455e 70 printf( "%d/%d/%d - %d:%d:%d\r\n", (dtp->tm_year), (dtp->tm_mon), (dtp->tm_mday), (dtp->tm_hour), (dtp->tm_min), (dtp->tm_sec) );
nxp_ip 0:1377bcf1455e 71
nxp_ip 0:1377bcf1455e 72 // adjust for tm structure required values
nxp_ip 0:1377bcf1455e 73 dtp->tm_year = dtp->tm_year - 1900;
nxp_ip 0:1377bcf1455e 74 dtp->tm_mon = dtp->tm_mon - 1;
nxp_ip 0:1377bcf1455e 75
nxp_ip 0:1377bcf1455e 76 set_time( dtp );
nxp_ip 0:1377bcf1455e 77 }
nxp_ip 0:1377bcf1455e 78
nxp_ip 0:1377bcf1455e 79 time_t PCF2127::time( time_t *tp )
nxp_ip 0:1377bcf1455e 80 {
nxp_ip 0:1377bcf1455e 81 struct tm dt, *dtp;
nxp_ip 0:1377bcf1455e 82 time_t t;
nxp_ip 0:1377bcf1455e 83 char buf[ 8 ] = { Seconds };
nxp_ip 0:1377bcf1455e 84
nxp_ip 0:1377bcf1455e 85 dtp = &dt;
nxp_ip 0:1377bcf1455e 86
nxp_ip 0:1377bcf1455e 87 i2c.write( device_address, buf, 1 );
nxp_ip 0:1377bcf1455e 88 i2c.read( device_address, buf, 7 );
nxp_ip 0:1377bcf1455e 89
nxp_ip 0:1377bcf1455e 90 dtp->tm_sec = bcd2i( buf[ 0 ] );
nxp_ip 0:1377bcf1455e 91 dtp->tm_min = bcd2i( buf[ 1 ] );
nxp_ip 0:1377bcf1455e 92 dtp->tm_hour = bcd2i( buf[ 2 ] );
nxp_ip 0:1377bcf1455e 93 dtp->tm_mday = bcd2i( buf[ 3 ] );
nxp_ip 0:1377bcf1455e 94 dtp->tm_wday = bcd2i( buf[ 4 ] );
nxp_ip 0:1377bcf1455e 95 dtp->tm_mon = bcd2i( buf[ 5 ] ) - 1;
nxp_ip 0:1377bcf1455e 96 dtp->tm_year = bcd2i( buf[ 6 ] ) + 100;
nxp_ip 0:1377bcf1455e 97
nxp_ip 0:1377bcf1455e 98 t = mktime( dtp );
nxp_ip 0:1377bcf1455e 99
nxp_ip 0:1377bcf1455e 100 if ( tp )
nxp_ip 0:1377bcf1455e 101 *tp = t;
nxp_ip 0:1377bcf1455e 102
nxp_ip 0:1377bcf1455e 103 return( t );
nxp_ip 0:1377bcf1455e 104 }
nxp_ip 0:1377bcf1455e 105
nxp_ip 0:1377bcf1455e 106 void PCF2127::set_alarm( char addr, char s )
nxp_ip 0:1377bcf1455e 107 {
nxp_ip 0:1377bcf1455e 108 char v;
nxp_ip 0:1377bcf1455e 109
nxp_ip 0:1377bcf1455e 110 v = i2bcd( s );
nxp_ip 0:1377bcf1455e 111 set_register( addr, v );
nxp_ip 0:1377bcf1455e 112 }
nxp_ip 0:1377bcf1455e 113
nxp_ip 0:1377bcf1455e 114 void PCF2127::clear_intr( void )
nxp_ip 0:1377bcf1455e 115 {
nxp_ip 0:1377bcf1455e 116 set_register( Control_2, 0x00 );
nxp_ip 0:1377bcf1455e 117 }
nxp_ip 0:1377bcf1455e 118
nxp_ip 0:1377bcf1455e 119 void PCF2127::set_register( char addr, char data )
nxp_ip 0:1377bcf1455e 120 {
nxp_ip 0:1377bcf1455e 121 char b[ 2 ];
nxp_ip 0:1377bcf1455e 122
nxp_ip 0:1377bcf1455e 123 b[ 0 ] = addr;
nxp_ip 0:1377bcf1455e 124 b[ 1 ] = data;
nxp_ip 0:1377bcf1455e 125
nxp_ip 0:1377bcf1455e 126 i2c.write( device_address, b, 2 );
nxp_ip 0:1377bcf1455e 127 }
nxp_ip 0:1377bcf1455e 128
nxp_ip 0:1377bcf1455e 129 char PCF2127::read_register( char addr )
nxp_ip 0:1377bcf1455e 130 {
nxp_ip 0:1377bcf1455e 131 char data;
nxp_ip 0:1377bcf1455e 132
nxp_ip 0:1377bcf1455e 133 data = addr;
nxp_ip 0:1377bcf1455e 134 i2c.write( device_address, &data, 1 );
nxp_ip 0:1377bcf1455e 135 i2c.read( device_address, &data, 1 );
nxp_ip 0:1377bcf1455e 136
nxp_ip 0:1377bcf1455e 137 return ( data );
nxp_ip 0:1377bcf1455e 138 }
nxp_ip 0:1377bcf1455e 139
nxp_ip 0:1377bcf1455e 140 char PCF2127::i2bcd( char n )
nxp_ip 0:1377bcf1455e 141 {
nxp_ip 0:1377bcf1455e 142 return ( ((n / 10) << 4) | (n % 10) );
nxp_ip 0:1377bcf1455e 143 }
nxp_ip 0:1377bcf1455e 144
nxp_ip 0:1377bcf1455e 145 char PCF2127::bcd2i( char bcd )
nxp_ip 0:1377bcf1455e 146 {
nxp_ip 0:1377bcf1455e 147 return ( ((bcd >> 4) * 10) + (bcd & 0x0F) );
nxp_ip 0:1377bcf1455e 148 }