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 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 range | 512 bytes RAM | package |
|---|---|---|---|
| PCF2127T | -30℃ to +80℃ | yes | SO16 |
| PCF2127AT | -30℃ to +60℃ | yes | SO20 |
| PCF2129T | -30℃ to +80℃ | not available | SO16 |
| PCF2129AT | -15℃ to +60℃ | not available | SO20 |
Pin assign


PCF2127T
Connection between MCU and PCF2127/PCF2129
These examples show how the RTC module can be connected via I2C bus.

References
- Datasheet
- User manual
- Other information PCF2127
Diff: PCF2127.cpp
- Revision:
- 0:1377bcf1455e
- Child:
- 1:700e0285cfd8
diff -r 000000000000 -r 1377bcf1455e PCF2127.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF2127.cpp Thu Dec 04 04:58:24 2014 +0000
@@ -0,0 +1,148 @@
+/*
+ * PCF2127 library
+ *
+ * @author Akifumi (Tedd) OKANO, NXP Semiconductors
+ * @version 1.5
+ * @date 04-Dec-2014
+ *
+ * PCF2127 is a "real time clock (RTC)" module which is including a Xtal and TCXO
+ * http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html
+ *
+ * RTC initializing part is ported from..
+ * http://mbed.org/users/roen/notebook/real-time/
+ *
+ * This code is refined version of..
+ * http://developer.mbed.org/users/okano/code/NXP_PCF2127A/
+ */
+
+#include "mbed.h"
+#include "PCF2127.h"
+
+#define PCF2127_I2C_SLAVE_ADDRESS 0xA2
+
+PCF2127::PCF2127( PinName sda, PinName sdl, char vControl_1, char vControl_2, char vControl_3 )
+ : i2c( sda, sdl ), device_address( PCF2127_I2C_SLAVE_ADDRESS )
+{
+ set_register( Control_1, vControl_1 );
+ set_register( Control_2, vControl_2 );
+ set_register( Control_3, vControl_3 );
+}
+
+PCF2127::~PCF2127()
+{
+}
+
+int PCF2127::is_init_required( void )
+{
+ return ( read_register( Seconds ) & 0x80 ? true : false );
+}
+
+void PCF2127::set_time( struct tm *dtp )
+{
+ char buf[ 8 ];
+ char c;
+
+ buf[ 0 ] = Seconds;
+ buf[ 1 ] = i2bcd( dtp->tm_sec );
+ buf[ 2 ] = i2bcd( dtp->tm_min );
+ buf[ 3 ] = i2bcd( dtp->tm_hour );
+ buf[ 4 ] = i2bcd( dtp->tm_mday );
+ buf[ 5 ] = i2bcd( dtp->tm_wday );
+ buf[ 6 ] = i2bcd( dtp->tm_mon + 1 );
+ buf[ 7 ] = i2bcd( dtp->tm_year - 100 );
+
+ c = read_register( Seconds );
+ while ( c == read_register( Seconds ) )
+ ;
+
+ i2c.write( device_address, buf, 8 );
+}
+
+void PCF2127::set_time( char *s )
+{
+ // The time information should be given in format of "YYYY MM DD HH MM SS"
+
+ struct tm dt, *dtp;
+
+ dtp = &dt;
+
+ 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) );
+ 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) );
+
+ // adjust for tm structure required values
+ dtp->tm_year = dtp->tm_year - 1900;
+ dtp->tm_mon = dtp->tm_mon - 1;
+
+ set_time( dtp );
+}
+
+time_t PCF2127::time( time_t *tp )
+{
+ struct tm dt, *dtp;
+ time_t t;
+ char buf[ 8 ] = { Seconds };
+
+ dtp = &dt;
+
+ i2c.write( device_address, buf, 1 );
+ i2c.read( device_address, buf, 7 );
+
+ dtp->tm_sec = bcd2i( buf[ 0 ] );
+ dtp->tm_min = bcd2i( buf[ 1 ] );
+ dtp->tm_hour = bcd2i( buf[ 2 ] );
+ dtp->tm_mday = bcd2i( buf[ 3 ] );
+ dtp->tm_wday = bcd2i( buf[ 4 ] );
+ dtp->tm_mon = bcd2i( buf[ 5 ] ) - 1;
+ dtp->tm_year = bcd2i( buf[ 6 ] ) + 100;
+
+ t = mktime( dtp );
+
+ if ( tp )
+ *tp = t;
+
+ return( t );
+}
+
+void PCF2127::set_alarm( char addr, char s )
+{
+ char v;
+
+ v = i2bcd( s );
+ set_register( addr, v );
+}
+
+void PCF2127::clear_intr( void )
+{
+ set_register( Control_2, 0x00 );
+}
+
+void PCF2127::set_register( char addr, char data )
+{
+ char b[ 2 ];
+
+ b[ 0 ] = addr;
+ b[ 1 ] = data;
+
+ i2c.write( device_address, b, 2 );
+}
+
+char PCF2127::read_register( char addr )
+{
+ char data;
+
+ data = addr;
+ i2c.write( device_address, &data, 1 );
+ i2c.read( device_address, &data, 1 );
+
+ return ( data );
+}
+
+char PCF2127::i2bcd( char n )
+{
+ return ( ((n / 10) << 4) | (n % 10) );
+}
+
+char PCF2127::bcd2i( char bcd )
+{
+ return ( ((bcd >> 4) * 10) + (bcd & 0x0F) );
+}
PCF2127 and PCF2129 High-accuracy RTC module