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
PCF2127.cpp@1:700e0285cfd8, 2014-12-09 (annotated)
- Committer:
- nxp_ip
- Date:
- Tue Dec 09 07:21:13 2014 +0000
- Revision:
- 1:700e0285cfd8
- Parent:
- 0:1377bcf1455e
- Child:
- 2:db76c68f998f
version 1.6:; changed: "battery switch-over function" as standard mode; changed: "CLKOUT" frequency is 1Hz; added: constructor by I2C object; added: set_time( time_t *tp ) function
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:700e0285cfd8 | 5 | * @version 1.6 |
nxp_ip | 1:700e0285cfd8 | 6 | * @date 09-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 | 1:700e0285cfd8 | 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 | 1:700e0285cfd8 | 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 | 1:700e0285cfd8 | 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 | 1:700e0285cfd8 | 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 | 1:700e0285cfd8 | 150 | int PCF2127::set_register( char addr, char data ) |
nxp_ip | 0:1377bcf1455e | 151 | { |
nxp_ip | 0:1377bcf1455e | 152 | char b[ 2 ]; |
nxp_ip | 0:1377bcf1455e | 153 | |
nxp_ip | 0:1377bcf1455e | 154 | b[ 0 ] = addr; |
nxp_ip | 0:1377bcf1455e | 155 | b[ 1 ] = data; |
nxp_ip | 0:1377bcf1455e | 156 | |
nxp_ip | 1:700e0285cfd8 | 157 | return ( i2c.write( device_address, b, 2 ) ); |
nxp_ip | 0:1377bcf1455e | 158 | } |
nxp_ip | 0:1377bcf1455e | 159 | |
nxp_ip | 1:700e0285cfd8 | 160 | int PCF2127::read_register( char addr ) |
nxp_ip | 0:1377bcf1455e | 161 | { |
nxp_ip | 0:1377bcf1455e | 162 | char data; |
nxp_ip | 0:1377bcf1455e | 163 | |
nxp_ip | 0:1377bcf1455e | 164 | data = addr; |
nxp_ip | 0:1377bcf1455e | 165 | i2c.write( device_address, &data, 1 ); |
nxp_ip | 0:1377bcf1455e | 166 | i2c.read( device_address, &data, 1 ); |
nxp_ip | 0:1377bcf1455e | 167 | |
nxp_ip | 0:1377bcf1455e | 168 | return ( data ); |
nxp_ip | 0:1377bcf1455e | 169 | } |
nxp_ip | 0:1377bcf1455e | 170 | |
nxp_ip | 0:1377bcf1455e | 171 | char PCF2127::i2bcd( char n ) |
nxp_ip | 0:1377bcf1455e | 172 | { |
nxp_ip | 0:1377bcf1455e | 173 | return ( ((n / 10) << 4) | (n % 10) ); |
nxp_ip | 0:1377bcf1455e | 174 | } |
nxp_ip | 0:1377bcf1455e | 175 | |
nxp_ip | 0:1377bcf1455e | 176 | char PCF2127::bcd2i( char bcd ) |
nxp_ip | 0:1377bcf1455e | 177 | { |
nxp_ip | 0:1377bcf1455e | 178 | return ( ((bcd >> 4) * 10) + (bcd & 0x0F) ); |
nxp_ip | 0:1377bcf1455e | 179 | } |