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:
Wed Dec 10 02:07:35 2014 +0000
Revision:
3:e2a6ac61fcbd
Parent:
2:db76c68f998f
online document (class description) updated

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 3:e2a6ac61fcbd 5 * @version 1.8
nxp_ip 2:db76c68f998f 6 * @date 10-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 1:700e0285cfd8 23 PCF2127::PCF2127( PinName sda, PinName sdl, char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl )
nxp_ip 1:700e0285cfd8 24 : i2c_p( new I2C( sda, sdl ) ), i2c( *i2c_p ), device_address( PCF2127_I2C_SLAVE_ADDRESS )
nxp_ip 0:1377bcf1455e 25 {
nxp_ip 1:700e0285cfd8 26 init( vControl_1, vControl_2, vControl_3, vCLKOUT_ctl );
nxp_ip 1:700e0285cfd8 27 }
nxp_ip 1:700e0285cfd8 28
nxp_ip 1:700e0285cfd8 29 PCF2127::PCF2127( I2C &i2c_, char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl )
nxp_ip 1:700e0285cfd8 30 : i2c_p( NULL ), i2c( i2c_ ), device_address( PCF2127_I2C_SLAVE_ADDRESS )
nxp_ip 1:700e0285cfd8 31 {
nxp_ip 1:700e0285cfd8 32 init( vControl_1, vControl_2, vControl_3, vCLKOUT_ctl );
nxp_ip 0:1377bcf1455e 33 }
nxp_ip 0:1377bcf1455e 34
nxp_ip 0:1377bcf1455e 35 PCF2127::~PCF2127()
nxp_ip 0:1377bcf1455e 36 {
nxp_ip 1:700e0285cfd8 37 if ( NULL != i2c_p )
nxp_ip 1:700e0285cfd8 38 delete i2c_p;
nxp_ip 1:700e0285cfd8 39 }
nxp_ip 1:700e0285cfd8 40
nxp_ip 1:700e0285cfd8 41 int PCF2127::init( char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl )
nxp_ip 1:700e0285cfd8 42 {
nxp_ip 1:700e0285cfd8 43 char data[ 4 ];
nxp_ip 1:700e0285cfd8 44 int err;
nxp_ip 2:db76c68f998f 45
nxp_ip 1:700e0285cfd8 46 data[ 0 ] = Control_1; // access start register address
nxp_ip 1:700e0285cfd8 47 data[ 1 ] = vControl_1;
nxp_ip 1:700e0285cfd8 48 data[ 2 ] = vControl_2;
nxp_ip 1:700e0285cfd8 49 data[ 3 ] = vControl_3;
nxp_ip 2:db76c68f998f 50
nxp_ip 1:700e0285cfd8 51 err = i2c.write( device_address, data, sizeof( data ) );
nxp_ip 1:700e0285cfd8 52 err |= set_register( CLKOUT_ctl, vCLKOUT_ctl );
nxp_ip 1:700e0285cfd8 53
nxp_ip 1:700e0285cfd8 54 return ( err ? I2C_ACCESS_FAIL : NO_ERROR );
nxp_ip 0:1377bcf1455e 55 }
nxp_ip 0:1377bcf1455e 56
nxp_ip 0:1377bcf1455e 57 int PCF2127::is_init_required( void )
nxp_ip 0:1377bcf1455e 58 {
nxp_ip 0:1377bcf1455e 59 return ( read_register( Seconds ) & 0x80 ? true : false );
nxp_ip 0:1377bcf1455e 60 }
nxp_ip 0:1377bcf1455e 61
nxp_ip 1:700e0285cfd8 62 int PCF2127::set_time( struct tm *dtp )
nxp_ip 0:1377bcf1455e 63 {
nxp_ip 0:1377bcf1455e 64 char buf[ 8 ];
nxp_ip 1:700e0285cfd8 65 char err;
nxp_ip 0:1377bcf1455e 66
nxp_ip 0:1377bcf1455e 67 buf[ 0 ] = Seconds;
nxp_ip 0:1377bcf1455e 68 buf[ 1 ] = i2bcd( dtp->tm_sec );
nxp_ip 0:1377bcf1455e 69 buf[ 2 ] = i2bcd( dtp->tm_min );
nxp_ip 0:1377bcf1455e 70 buf[ 3 ] = i2bcd( dtp->tm_hour );
nxp_ip 0:1377bcf1455e 71 buf[ 4 ] = i2bcd( dtp->tm_mday );
nxp_ip 0:1377bcf1455e 72 buf[ 5 ] = i2bcd( dtp->tm_wday );
nxp_ip 0:1377bcf1455e 73 buf[ 6 ] = i2bcd( dtp->tm_mon + 1 );
nxp_ip 0:1377bcf1455e 74 buf[ 7 ] = i2bcd( dtp->tm_year - 100 );
nxp_ip 0:1377bcf1455e 75
nxp_ip 1:700e0285cfd8 76 err = i2c.write( device_address, buf, 8 );
nxp_ip 2:db76c68f998f 77
nxp_ip 1:700e0285cfd8 78 return ( err ? I2C_ACCESS_FAIL : NO_ERROR );
nxp_ip 0:1377bcf1455e 79 }
nxp_ip 0:1377bcf1455e 80
nxp_ip 1:700e0285cfd8 81 int PCF2127::set_time( time_t *tp )
nxp_ip 1:700e0285cfd8 82 {
nxp_ip 1:700e0285cfd8 83 return ( set_time( localtime( tp ) ) );
nxp_ip 1:700e0285cfd8 84 }
nxp_ip 1:700e0285cfd8 85
nxp_ip 1:700e0285cfd8 86 int PCF2127::set_time( char *s )
nxp_ip 0:1377bcf1455e 87 {
nxp_ip 0:1377bcf1455e 88 // The time information should be given in format of "YYYY MM DD HH MM SS"
nxp_ip 0:1377bcf1455e 89
nxp_ip 0:1377bcf1455e 90 struct tm dt, *dtp;
nxp_ip 0:1377bcf1455e 91
nxp_ip 0:1377bcf1455e 92 dtp = &dt;
nxp_ip 0:1377bcf1455e 93
nxp_ip 0:1377bcf1455e 94 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 1:700e0285cfd8 95 printf( "%02d/%02d/%02d - %02d:%02d:%02d\r\n", (dtp->tm_year), (dtp->tm_mon), (dtp->tm_mday), (dtp->tm_hour), (dtp->tm_min), (dtp->tm_sec) );
nxp_ip 0:1377bcf1455e 96
nxp_ip 0:1377bcf1455e 97 // adjust for tm structure required values
nxp_ip 0:1377bcf1455e 98 dtp->tm_year = dtp->tm_year - 1900;
nxp_ip 0:1377bcf1455e 99 dtp->tm_mon = dtp->tm_mon - 1;
nxp_ip 0:1377bcf1455e 100
nxp_ip 1:700e0285cfd8 101 return ( set_time( dtp ) );
nxp_ip 0:1377bcf1455e 102 }
nxp_ip 0:1377bcf1455e 103
nxp_ip 0:1377bcf1455e 104 time_t PCF2127::time( time_t *tp )
nxp_ip 0:1377bcf1455e 105 {
nxp_ip 0:1377bcf1455e 106 struct tm dt, *dtp;
nxp_ip 0:1377bcf1455e 107 time_t t;
nxp_ip 1:700e0285cfd8 108 char buf[ 8 ];
nxp_ip 0:1377bcf1455e 109
nxp_ip 0:1377bcf1455e 110 dtp = &dt;
nxp_ip 0:1377bcf1455e 111
nxp_ip 1:700e0285cfd8 112 buf[ 0 ] = Seconds; // read start register address
nxp_ip 1:700e0285cfd8 113
nxp_ip 1:700e0285cfd8 114 if ( i2c.write( device_address, buf, 1 ) )
nxp_ip 1:700e0285cfd8 115 return ( TIME_FUNC_ERROR );
nxp_ip 1:700e0285cfd8 116
nxp_ip 1:700e0285cfd8 117 if ( i2c.read( device_address, buf, 7 ) )
nxp_ip 1:700e0285cfd8 118 return ( TIME_FUNC_ERROR );
nxp_ip 0:1377bcf1455e 119
nxp_ip 0:1377bcf1455e 120 dtp->tm_sec = bcd2i( buf[ 0 ] );
nxp_ip 0:1377bcf1455e 121 dtp->tm_min = bcd2i( buf[ 1 ] );
nxp_ip 0:1377bcf1455e 122 dtp->tm_hour = bcd2i( buf[ 2 ] );
nxp_ip 0:1377bcf1455e 123 dtp->tm_mday = bcd2i( buf[ 3 ] );
nxp_ip 0:1377bcf1455e 124 dtp->tm_wday = bcd2i( buf[ 4 ] );
nxp_ip 0:1377bcf1455e 125 dtp->tm_mon = bcd2i( buf[ 5 ] ) - 1;
nxp_ip 0:1377bcf1455e 126 dtp->tm_year = bcd2i( buf[ 6 ] ) + 100;
nxp_ip 0:1377bcf1455e 127
nxp_ip 0:1377bcf1455e 128 t = mktime( dtp );
nxp_ip 0:1377bcf1455e 129
nxp_ip 0:1377bcf1455e 130 if ( tp )
nxp_ip 0:1377bcf1455e 131 *tp = t;
nxp_ip 0:1377bcf1455e 132
nxp_ip 0:1377bcf1455e 133 return( t );
nxp_ip 0:1377bcf1455e 134 }
nxp_ip 0:1377bcf1455e 135
nxp_ip 1:700e0285cfd8 136 int PCF2127::set_alarm( char addr, char s )
nxp_ip 0:1377bcf1455e 137 {
nxp_ip 0:1377bcf1455e 138 char v;
nxp_ip 0:1377bcf1455e 139
nxp_ip 0:1377bcf1455e 140 v = i2bcd( s );
nxp_ip 2:db76c68f998f 141
nxp_ip 1:700e0285cfd8 142 return ( set_register( addr, v ) );
nxp_ip 0:1377bcf1455e 143 }
nxp_ip 0:1377bcf1455e 144
nxp_ip 1:700e0285cfd8 145 int PCF2127::clear_intr( void )
nxp_ip 0:1377bcf1455e 146 {
nxp_ip 1:700e0285cfd8 147 return ( set_register( Control_2, 0x00 ) );
nxp_ip 0:1377bcf1455e 148 }
nxp_ip 0:1377bcf1455e 149
nxp_ip 2:db76c68f998f 150 int PCF2127::RAM_write( int address, char *p, int size )
nxp_ip 2:db76c68f998f 151 {
nxp_ip 2:db76c68f998f 152 char b[ size + 1 ];
nxp_ip 2:db76c68f998f 153
nxp_ip 2:db76c68f998f 154 b[ 0 ] = RAM_wrt_cmd;
nxp_ip 2:db76c68f998f 155
nxp_ip 2:db76c68f998f 156 for ( int i = 1; i <= size; i++ )
nxp_ip 2:db76c68f998f 157 b[ i ] = *p++;
nxp_ip 2:db76c68f998f 158
nxp_ip 2:db76c68f998f 159 if ( set_RAM_address( address ) )
nxp_ip 2:db76c68f998f 160 return ( I2C_ACCESS_FAIL );
nxp_ip 2:db76c68f998f 161
nxp_ip 2:db76c68f998f 162 return ( i2c.write( device_address, b, sizeof( b ) ) ? I2C_ACCESS_FAIL : NO_ERROR );
nxp_ip 2:db76c68f998f 163 }
nxp_ip 2:db76c68f998f 164
nxp_ip 2:db76c68f998f 165 int PCF2127::RAM_read( int address, char *p, int size )
nxp_ip 2:db76c68f998f 166 {
nxp_ip 2:db76c68f998f 167 char b = RAM_rd_cmd;
nxp_ip 2:db76c68f998f 168
nxp_ip 2:db76c68f998f 169 if ( set_RAM_address( address ) )
nxp_ip 2:db76c68f998f 170 return ( I2C_ACCESS_FAIL );
nxp_ip 2:db76c68f998f 171
nxp_ip 2:db76c68f998f 172 if ( i2c.write( device_address, &b, 1 ) )
nxp_ip 2:db76c68f998f 173 return ( I2C_ACCESS_FAIL );
nxp_ip 2:db76c68f998f 174
nxp_ip 2:db76c68f998f 175 return ( i2c.read( device_address, p, size ) ? I2C_ACCESS_FAIL : NO_ERROR );
nxp_ip 2:db76c68f998f 176 }
nxp_ip 2:db76c68f998f 177
nxp_ip 2:db76c68f998f 178
nxp_ip 2:db76c68f998f 179 int PCF2127::set_RAM_address( char address )
nxp_ip 2:db76c68f998f 180 {
nxp_ip 2:db76c68f998f 181 char b[ 3 ];
nxp_ip 2:db76c68f998f 182
nxp_ip 2:db76c68f998f 183 b[ 0 ] = RAM_addr_MSB;
nxp_ip 2:db76c68f998f 184 b[ 1 ] = (address >> 8) & 0x1;
nxp_ip 2:db76c68f998f 185 b[ 2 ] = address & 0xFF;
nxp_ip 2:db76c68f998f 186
nxp_ip 2:db76c68f998f 187 return ( i2c.write( device_address, b, sizeof( b ) ) );
nxp_ip 2:db76c68f998f 188 }
nxp_ip 2:db76c68f998f 189
nxp_ip 1:700e0285cfd8 190 int PCF2127::set_register( char addr, char data )
nxp_ip 0:1377bcf1455e 191 {
nxp_ip 0:1377bcf1455e 192 char b[ 2 ];
nxp_ip 0:1377bcf1455e 193
nxp_ip 0:1377bcf1455e 194 b[ 0 ] = addr;
nxp_ip 0:1377bcf1455e 195 b[ 1 ] = data;
nxp_ip 0:1377bcf1455e 196
nxp_ip 2:db76c68f998f 197 return ( i2c.write( device_address, b, sizeof( b ) ) );
nxp_ip 0:1377bcf1455e 198 }
nxp_ip 0:1377bcf1455e 199
nxp_ip 1:700e0285cfd8 200 int PCF2127::read_register( char addr )
nxp_ip 0:1377bcf1455e 201 {
nxp_ip 0:1377bcf1455e 202 char data;
nxp_ip 0:1377bcf1455e 203
nxp_ip 0:1377bcf1455e 204 data = addr;
nxp_ip 0:1377bcf1455e 205 i2c.write( device_address, &data, 1 );
nxp_ip 0:1377bcf1455e 206 i2c.read( device_address, &data, 1 );
nxp_ip 0:1377bcf1455e 207
nxp_ip 0:1377bcf1455e 208 return ( data );
nxp_ip 0:1377bcf1455e 209 }
nxp_ip 0:1377bcf1455e 210
nxp_ip 0:1377bcf1455e 211 char PCF2127::i2bcd( char n )
nxp_ip 0:1377bcf1455e 212 {
nxp_ip 0:1377bcf1455e 213 return ( ((n / 10) << 4) | (n % 10) );
nxp_ip 0:1377bcf1455e 214 }
nxp_ip 0:1377bcf1455e 215
nxp_ip 0:1377bcf1455e 216 char PCF2127::bcd2i( char bcd )
nxp_ip 0:1377bcf1455e 217 {
nxp_ip 0:1377bcf1455e 218 return ( ((bcd >> 4) * 10) + (bcd & 0x0F) );
nxp_ip 0:1377bcf1455e 219 }