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.h@2:db76c68f998f, 2014-12-10 (annotated)
- Committer:
- nxp_ip
- Date:
- Wed Dec 10 01:43:30 2014 +0000
- Revision:
- 2:db76c68f998f
- Parent:
- 1:700e0285cfd8
- Child:
- 3:e2a6ac61fcbd
internal RAM access API has been added (for PCF2127 only)
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 | 2:db76c68f998f | 5 | * @version 1.7 |
| 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 | #ifndef MBED_PCF2127 |
| nxp_ip | 0:1377bcf1455e | 19 | #define MBED_PCF2127 |
| nxp_ip | 0:1377bcf1455e | 20 | |
| nxp_ip | 0:1377bcf1455e | 21 | #include "mbed.h" |
| nxp_ip | 0:1377bcf1455e | 22 | |
| nxp_ip | 0:1377bcf1455e | 23 | class PCF2127 |
| nxp_ip | 0:1377bcf1455e | 24 | { |
| nxp_ip | 0:1377bcf1455e | 25 | public: |
| nxp_ip | 0:1377bcf1455e | 26 | |
| nxp_ip | 0:1377bcf1455e | 27 | /** name of the PCF2127 registers */ |
| nxp_ip | 0:1377bcf1455e | 28 | typedef enum { |
| nxp_ip | 0:1377bcf1455e | 29 | Control_1, |
| nxp_ip | 0:1377bcf1455e | 30 | Control_2, |
| nxp_ip | 0:1377bcf1455e | 31 | Control_3, |
| nxp_ip | 0:1377bcf1455e | 32 | Seconds, |
| nxp_ip | 0:1377bcf1455e | 33 | Minutes, |
| nxp_ip | 0:1377bcf1455e | 34 | Hours, |
| nxp_ip | 0:1377bcf1455e | 35 | Days, |
| nxp_ip | 0:1377bcf1455e | 36 | Weekdays, |
| nxp_ip | 0:1377bcf1455e | 37 | Months, |
| nxp_ip | 0:1377bcf1455e | 38 | Years, |
| nxp_ip | 0:1377bcf1455e | 39 | Second_alarm, |
| nxp_ip | 0:1377bcf1455e | 40 | Minute_alarm, |
| nxp_ip | 0:1377bcf1455e | 41 | Hour_alarm, |
| nxp_ip | 0:1377bcf1455e | 42 | Day_alarm, |
| nxp_ip | 0:1377bcf1455e | 43 | Weekday_alarm, |
| nxp_ip | 0:1377bcf1455e | 44 | CLKOUT_ctl, |
| nxp_ip | 0:1377bcf1455e | 45 | Watchdg_tim_ctl, |
| nxp_ip | 0:1377bcf1455e | 46 | Watchdg_tim_val, |
| nxp_ip | 0:1377bcf1455e | 47 | Timestp_ctl, |
| nxp_ip | 0:1377bcf1455e | 48 | Sec_timestp, |
| nxp_ip | 0:1377bcf1455e | 49 | Min_timestp, |
| nxp_ip | 0:1377bcf1455e | 50 | Hour_timestp, |
| nxp_ip | 0:1377bcf1455e | 51 | Day_timestp, |
| nxp_ip | 0:1377bcf1455e | 52 | Mon_timestp, |
| nxp_ip | 0:1377bcf1455e | 53 | Year_timestp, |
| nxp_ip | 2:db76c68f998f | 54 | Aging_offset, |
| nxp_ip | 2:db76c68f998f | 55 | RAM_addr_MSB, |
| nxp_ip | 2:db76c68f998f | 56 | RAM_addr_LSB, |
| nxp_ip | 2:db76c68f998f | 57 | RAM_wrt_cmd, |
| nxp_ip | 2:db76c68f998f | 58 | RAM_rd_cmd |
| nxp_ip | 0:1377bcf1455e | 59 | } |
| nxp_ip | 0:1377bcf1455e | 60 | RegisterName; |
| nxp_ip | 2:db76c68f998f | 61 | |
| nxp_ip | 1:700e0285cfd8 | 62 | /** Error code */ |
| nxp_ip | 1:700e0285cfd8 | 63 | typedef enum { |
| nxp_ip | 1:700e0285cfd8 | 64 | NO_ERROR = 0, |
| nxp_ip | 1:700e0285cfd8 | 65 | CLOCK_INTEGRITY_FAIL = 1, |
| nxp_ip | 1:700e0285cfd8 | 66 | I2C_ACCESS_FAIL = 2, |
| nxp_ip | 1:700e0285cfd8 | 67 | TIME_FUNC_ERROR = ((time_t)-1) |
| nxp_ip | 1:700e0285cfd8 | 68 | } |
| nxp_ip | 1:700e0285cfd8 | 69 | ErrorNum; |
| nxp_ip | 2:db76c68f998f | 70 | |
| nxp_ip | 0:1377bcf1455e | 71 | /** Create a PCF2127 instance connected to specified I2C pins with specified address |
| nxp_ip | 0:1377bcf1455e | 72 | * |
| nxp_ip | 2:db76c68f998f | 73 | * @param I2C_sda I2C-bus SDA pin |
| nxp_ip | 2:db76c68f998f | 74 | * @param I2C_scl I2C-bus SCL pin |
| nxp_ip | 2:db76c68f998f | 75 | * @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute) |
| nxp_ip | 2:db76c68f998f | 76 | * @param vControl_2 (option) data for Control_2 register |
| nxp_ip | 2:db76c68f998f | 77 | * @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode) |
| nxp_ip | 2:db76c68f998f | 78 | * @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin) |
| nxp_ip | 1:700e0285cfd8 | 79 | CLKOUT_ctl |
| nxp_ip | 0:1377bcf1455e | 80 | */ |
| nxp_ip | 1:700e0285cfd8 | 81 | PCF2127( PinName sda, PinName sdl, char vControl_1 = Cntl1, char vControl_2 = Cntl2, char vControl_3 = Cntl1, char vCLKOUT_ctl = ClkOut ); |
| nxp_ip | 0:1377bcf1455e | 82 | |
| nxp_ip | 1:700e0285cfd8 | 83 | /** Create a PCF2127 instance connected to specified I2C pins with specified address |
| nxp_ip | 1:700e0285cfd8 | 84 | * |
| nxp_ip | 2:db76c68f998f | 85 | * @param i2c I2C object (instance) |
| nxp_ip | 2:db76c68f998f | 86 | * @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute) |
| nxp_ip | 2:db76c68f998f | 87 | * @param vControl_2 (option) data for Control_2 register |
| nxp_ip | 2:db76c68f998f | 88 | * @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode) |
| nxp_ip | 2:db76c68f998f | 89 | * @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin) |
| nxp_ip | 1:700e0285cfd8 | 90 | */ |
| nxp_ip | 1:700e0285cfd8 | 91 | PCF2127( I2C &i2c, char vControl_1 = Cntl1, char vControl_2 = Cntl2, char vControl_3 = Cntl1, char vCLKOUT_ctl = ClkOut ); |
| nxp_ip | 2:db76c68f998f | 92 | |
| nxp_ip | 0:1377bcf1455e | 93 | /** Destractor |
| nxp_ip | 0:1377bcf1455e | 94 | */ |
| nxp_ip | 0:1377bcf1455e | 95 | ~PCF2127(); |
| nxp_ip | 0:1377bcf1455e | 96 | |
| nxp_ip | 0:1377bcf1455e | 97 | /** Clock integrity check |
| nxp_ip | 0:1377bcf1455e | 98 | * |
| nxp_ip | 2:db76c68f998f | 99 | * @return non-zero value if the clock was stopped (means need to set the time) |
| nxp_ip | 0:1377bcf1455e | 100 | */ |
| nxp_ip | 0:1377bcf1455e | 101 | int is_init_required( void ); |
| nxp_ip | 0:1377bcf1455e | 102 | |
| nxp_ip | 0:1377bcf1455e | 103 | /** Set the time |
| nxp_ip | 0:1377bcf1455e | 104 | * |
| nxp_ip | 2:db76c68f998f | 105 | * @param dtp Pointer to struct tm |
| nxp_ip | 2:db76c68f998f | 106 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 0:1377bcf1455e | 107 | */ |
| nxp_ip | 1:700e0285cfd8 | 108 | int set_time( struct tm *dtp ); |
| nxp_ip | 2:db76c68f998f | 109 | |
| nxp_ip | 1:700e0285cfd8 | 110 | /** Set the time |
| nxp_ip | 1:700e0285cfd8 | 111 | * |
| nxp_ip | 2:db76c68f998f | 112 | * @param tp pointer to time_t |
| nxp_ip | 2:db76c68f998f | 113 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 1:700e0285cfd8 | 114 | */ |
| nxp_ip | 1:700e0285cfd8 | 115 | int set_time( time_t *tp ); |
| nxp_ip | 0:1377bcf1455e | 116 | |
| nxp_ip | 0:1377bcf1455e | 117 | /** Set the time |
| nxp_ip | 2:db76c68f998f | 118 | * |
| nxp_ip | 2:db76c68f998f | 119 | * @param s String data: The time information should be given in format of "YYYY MM DD HH MM SS" |
| nxp_ip | 2:db76c68f998f | 120 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 0:1377bcf1455e | 121 | */ |
| nxp_ip | 1:700e0285cfd8 | 122 | int set_time( char *s ); |
| nxp_ip | 0:1377bcf1455e | 123 | |
| nxp_ip | 0:1377bcf1455e | 124 | /** Get time of day |
| nxp_ip | 0:1377bcf1455e | 125 | * |
| nxp_ip | 0:1377bcf1455e | 126 | * This function works similar to "time()" in standard-C-library |
| nxp_ip | 0:1377bcf1455e | 127 | * |
| nxp_ip | 2:db76c68f998f | 128 | * @param tp Pointer to time_t |
| nxp_ip | 2:db76c68f998f | 129 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 0:1377bcf1455e | 130 | */ |
| nxp_ip | 0:1377bcf1455e | 131 | time_t time( time_t *tp ); |
| nxp_ip | 0:1377bcf1455e | 132 | |
| nxp_ip | 0:1377bcf1455e | 133 | /** Register access interface with integer to BCD conversion |
| nxp_ip | 0:1377bcf1455e | 134 | * |
| nxp_ip | 0:1377bcf1455e | 135 | * @param addr Register address |
| nxp_ip | 2:db76c68f998f | 136 | * @param s Integer data. Converted to BCD before writing inot the register. |
| nxp_ip | 2:db76c68f998f | 137 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 0:1377bcf1455e | 138 | */ |
| nxp_ip | 1:700e0285cfd8 | 139 | int set_alarm( char addr, char s ); |
| nxp_ip | 0:1377bcf1455e | 140 | |
| nxp_ip | 0:1377bcf1455e | 141 | /** Clear interrupt flag |
| nxp_ip | 0:1377bcf1455e | 142 | * |
| nxp_ip | 0:1377bcf1455e | 143 | * Clears interrupt flags by writing 0x00 into Control_2 register |
| nxp_ip | 0:1377bcf1455e | 144 | * |
| nxp_ip | 2:db76c68f998f | 145 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 0:1377bcf1455e | 146 | */ |
| nxp_ip | 1:700e0285cfd8 | 147 | int clear_intr( void ); |
| nxp_ip | 0:1377bcf1455e | 148 | |
| nxp_ip | 2:db76c68f998f | 149 | /** Writing data into RAM (for PCF2127 only) |
| nxp_ip | 2:db76c68f998f | 150 | * |
| nxp_ip | 2:db76c68f998f | 151 | * Write data into PCF2127 internal RAM |
| nxp_ip | 2:db76c68f998f | 152 | * |
| nxp_ip | 2:db76c68f998f | 153 | * @param address target address of internal RAM |
| nxp_ip | 2:db76c68f998f | 154 | * @param *p pointer to write data buffer |
| nxp_ip | 2:db76c68f998f | 155 | * @param size size of writing data |
| nxp_ip | 2:db76c68f998f | 156 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 2:db76c68f998f | 157 | */ |
| nxp_ip | 2:db76c68f998f | 158 | int RAM_write( int address, char *p, int size ); |
| nxp_ip | 2:db76c68f998f | 159 | |
| nxp_ip | 2:db76c68f998f | 160 | /** Reading data from RAM (for PCF2127 only) |
| nxp_ip | 2:db76c68f998f | 161 | * |
| nxp_ip | 2:db76c68f998f | 162 | * Read data from PCF2127 internal RAM |
| nxp_ip | 2:db76c68f998f | 163 | * |
| nxp_ip | 2:db76c68f998f | 164 | * @param address target address of internal RAM |
| nxp_ip | 2:db76c68f998f | 165 | * @param *p pointer to read data buffer |
| nxp_ip | 2:db76c68f998f | 166 | * @param size size of writing data |
| nxp_ip | 2:db76c68f998f | 167 | * @return Error code (NO_ERROR==0) |
| nxp_ip | 2:db76c68f998f | 168 | */ |
| nxp_ip | 2:db76c68f998f | 169 | int RAM_read( int address, char *p, int size ); |
| nxp_ip | 2:db76c68f998f | 170 | |
| nxp_ip | 0:1377bcf1455e | 171 | private: |
| nxp_ip | 0:1377bcf1455e | 172 | |
| nxp_ip | 1:700e0285cfd8 | 173 | typedef enum { |
| nxp_ip | 1:700e0285cfd8 | 174 | Cntl1 = 0x03, |
| nxp_ip | 1:700e0285cfd8 | 175 | Cntl2 = 0x00, |
| nxp_ip | 1:700e0285cfd8 | 176 | Cntl3 = 0x00, |
| nxp_ip | 1:700e0285cfd8 | 177 | ClkOut = 0x46 |
| nxp_ip | 1:700e0285cfd8 | 178 | } |
| nxp_ip | 1:700e0285cfd8 | 179 | DefaultRegParam; |
| nxp_ip | 2:db76c68f998f | 180 | |
| nxp_ip | 1:700e0285cfd8 | 181 | int init( char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl ); |
| nxp_ip | 1:700e0285cfd8 | 182 | int set_register( char addr, char data ); |
| nxp_ip | 1:700e0285cfd8 | 183 | int read_register( char addr ); |
| nxp_ip | 1:700e0285cfd8 | 184 | char i2bcd( char n ); |
| nxp_ip | 1:700e0285cfd8 | 185 | char bcd2i( char bcd ); |
| nxp_ip | 2:db76c68f998f | 186 | int set_RAM_address( char address ); |
| nxp_ip | 2:db76c68f998f | 187 | |
| nxp_ip | 1:700e0285cfd8 | 188 | I2C *i2c_p; |
| nxp_ip | 1:700e0285cfd8 | 189 | I2C &i2c; |
| nxp_ip | 0:1377bcf1455e | 190 | char device_address; |
| nxp_ip | 0:1377bcf1455e | 191 | } |
| nxp_ip | 0:1377bcf1455e | 192 | ; |
| nxp_ip | 0:1377bcf1455e | 193 | #endif // end of "#ifndef MBED_PCF2127" |
PCF2127 and PCF2129 High-accuracy RTC module