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@3:e2a6ac61fcbd, 2014-12-10 (annotated)
- 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?
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 | 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 | #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 | 3:e2a6ac61fcbd | 23 | /** PCF2127 class |
nxp_ip | 3:e2a6ac61fcbd | 24 | * |
nxp_ip | 3:e2a6ac61fcbd | 25 | * This is a driver code for the PPCF2127: "real time clock (RTC)" module which is including a Xtal and TCXO |
nxp_ip | 3:e2a6ac61fcbd | 26 | * This class provides interface for PCF2127 operation and accessing its registers. |
nxp_ip | 3:e2a6ac61fcbd | 27 | * Detail information is available on next URL. |
nxp_ip | 3:e2a6ac61fcbd | 28 | * http://www.nxp.com/products/interface_and_connectivity/real_time_clocks/rtcs_with_temp_compensation/series/PCF2127.html |
nxp_ip | 3:e2a6ac61fcbd | 29 | * |
nxp_ip | 3:e2a6ac61fcbd | 30 | * Example: |
nxp_ip | 3:e2a6ac61fcbd | 31 | * @code |
nxp_ip | 3:e2a6ac61fcbd | 32 | * #include "mbed.h" |
nxp_ip | 3:e2a6ac61fcbd | 33 | * #include "PCF2127.h" |
nxp_ip | 3:e2a6ac61fcbd | 34 | * |
nxp_ip | 3:e2a6ac61fcbd | 35 | * PCF2127 rtc( p28, p27 ); |
nxp_ip | 3:e2a6ac61fcbd | 36 | * |
nxp_ip | 3:e2a6ac61fcbd | 37 | * int main() |
nxp_ip | 3:e2a6ac61fcbd | 38 | * { |
nxp_ip | 3:e2a6ac61fcbd | 39 | * time_t t; |
nxp_ip | 3:e2a6ac61fcbd | 40 | * |
nxp_ip | 3:e2a6ac61fcbd | 41 | * printf( "PCF2127 demo started.\r\n" ); |
nxp_ip | 3:e2a6ac61fcbd | 42 | * |
nxp_ip | 3:e2a6ac61fcbd | 43 | * if ( rtc.is_init_required() ) { |
nxp_ip | 3:e2a6ac61fcbd | 44 | * rtc.set_time( "2014 12 10 12 00 00" ); |
nxp_ip | 3:e2a6ac61fcbd | 45 | * } |
nxp_ip | 3:e2a6ac61fcbd | 46 | * |
nxp_ip | 3:e2a6ac61fcbd | 47 | * while ( 1 ) { |
nxp_ip | 3:e2a6ac61fcbd | 48 | * t = rtc.time( NULL ); |
nxp_ip | 3:e2a6ac61fcbd | 49 | * printf( "%s\r", ctime( &t ) ); |
nxp_ip | 3:e2a6ac61fcbd | 50 | * wait( 0.25 ); |
nxp_ip | 3:e2a6ac61fcbd | 51 | * } |
nxp_ip | 3:e2a6ac61fcbd | 52 | * } |
nxp_ip | 3:e2a6ac61fcbd | 53 | * @endcode |
nxp_ip | 3:e2a6ac61fcbd | 54 | */ |
nxp_ip | 3:e2a6ac61fcbd | 55 | |
nxp_ip | 0:1377bcf1455e | 56 | class PCF2127 |
nxp_ip | 0:1377bcf1455e | 57 | { |
nxp_ip | 0:1377bcf1455e | 58 | public: |
nxp_ip | 0:1377bcf1455e | 59 | |
nxp_ip | 0:1377bcf1455e | 60 | /** name of the PCF2127 registers */ |
nxp_ip | 0:1377bcf1455e | 61 | typedef enum { |
nxp_ip | 0:1377bcf1455e | 62 | Control_1, |
nxp_ip | 0:1377bcf1455e | 63 | Control_2, |
nxp_ip | 0:1377bcf1455e | 64 | Control_3, |
nxp_ip | 0:1377bcf1455e | 65 | Seconds, |
nxp_ip | 0:1377bcf1455e | 66 | Minutes, |
nxp_ip | 0:1377bcf1455e | 67 | Hours, |
nxp_ip | 0:1377bcf1455e | 68 | Days, |
nxp_ip | 0:1377bcf1455e | 69 | Weekdays, |
nxp_ip | 0:1377bcf1455e | 70 | Months, |
nxp_ip | 0:1377bcf1455e | 71 | Years, |
nxp_ip | 0:1377bcf1455e | 72 | Second_alarm, |
nxp_ip | 0:1377bcf1455e | 73 | Minute_alarm, |
nxp_ip | 0:1377bcf1455e | 74 | Hour_alarm, |
nxp_ip | 0:1377bcf1455e | 75 | Day_alarm, |
nxp_ip | 0:1377bcf1455e | 76 | Weekday_alarm, |
nxp_ip | 0:1377bcf1455e | 77 | CLKOUT_ctl, |
nxp_ip | 0:1377bcf1455e | 78 | Watchdg_tim_ctl, |
nxp_ip | 0:1377bcf1455e | 79 | Watchdg_tim_val, |
nxp_ip | 0:1377bcf1455e | 80 | Timestp_ctl, |
nxp_ip | 0:1377bcf1455e | 81 | Sec_timestp, |
nxp_ip | 0:1377bcf1455e | 82 | Min_timestp, |
nxp_ip | 0:1377bcf1455e | 83 | Hour_timestp, |
nxp_ip | 0:1377bcf1455e | 84 | Day_timestp, |
nxp_ip | 0:1377bcf1455e | 85 | Mon_timestp, |
nxp_ip | 0:1377bcf1455e | 86 | Year_timestp, |
nxp_ip | 2:db76c68f998f | 87 | Aging_offset, |
nxp_ip | 2:db76c68f998f | 88 | RAM_addr_MSB, |
nxp_ip | 2:db76c68f998f | 89 | RAM_addr_LSB, |
nxp_ip | 2:db76c68f998f | 90 | RAM_wrt_cmd, |
nxp_ip | 2:db76c68f998f | 91 | RAM_rd_cmd |
nxp_ip | 0:1377bcf1455e | 92 | } |
nxp_ip | 0:1377bcf1455e | 93 | RegisterName; |
nxp_ip | 2:db76c68f998f | 94 | |
nxp_ip | 1:700e0285cfd8 | 95 | /** Error code */ |
nxp_ip | 1:700e0285cfd8 | 96 | typedef enum { |
nxp_ip | 1:700e0285cfd8 | 97 | NO_ERROR = 0, |
nxp_ip | 1:700e0285cfd8 | 98 | CLOCK_INTEGRITY_FAIL = 1, |
nxp_ip | 1:700e0285cfd8 | 99 | I2C_ACCESS_FAIL = 2, |
nxp_ip | 1:700e0285cfd8 | 100 | TIME_FUNC_ERROR = ((time_t)-1) |
nxp_ip | 1:700e0285cfd8 | 101 | } |
nxp_ip | 1:700e0285cfd8 | 102 | ErrorNum; |
nxp_ip | 2:db76c68f998f | 103 | |
nxp_ip | 0:1377bcf1455e | 104 | /** Create a PCF2127 instance connected to specified I2C pins with specified address |
nxp_ip | 0:1377bcf1455e | 105 | * |
nxp_ip | 2:db76c68f998f | 106 | * @param I2C_sda I2C-bus SDA pin |
nxp_ip | 2:db76c68f998f | 107 | * @param I2C_scl I2C-bus SCL pin |
nxp_ip | 2:db76c68f998f | 108 | * @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute) |
nxp_ip | 2:db76c68f998f | 109 | * @param vControl_2 (option) data for Control_2 register |
nxp_ip | 2:db76c68f998f | 110 | * @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode) |
nxp_ip | 2:db76c68f998f | 111 | * @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin) |
nxp_ip | 1:700e0285cfd8 | 112 | CLKOUT_ctl |
nxp_ip | 0:1377bcf1455e | 113 | */ |
nxp_ip | 1:700e0285cfd8 | 114 | 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 | 115 | |
nxp_ip | 1:700e0285cfd8 | 116 | /** Create a PCF2127 instance connected to specified I2C pins with specified address |
nxp_ip | 1:700e0285cfd8 | 117 | * |
nxp_ip | 2:db76c68f998f | 118 | * @param i2c I2C object (instance) |
nxp_ip | 2:db76c68f998f | 119 | * @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute) |
nxp_ip | 2:db76c68f998f | 120 | * @param vControl_2 (option) data for Control_2 register |
nxp_ip | 2:db76c68f998f | 121 | * @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode) |
nxp_ip | 2:db76c68f998f | 122 | * @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin) |
nxp_ip | 1:700e0285cfd8 | 123 | */ |
nxp_ip | 1:700e0285cfd8 | 124 | PCF2127( I2C &i2c, char vControl_1 = Cntl1, char vControl_2 = Cntl2, char vControl_3 = Cntl1, char vCLKOUT_ctl = ClkOut ); |
nxp_ip | 2:db76c68f998f | 125 | |
nxp_ip | 0:1377bcf1455e | 126 | /** Destractor |
nxp_ip | 0:1377bcf1455e | 127 | */ |
nxp_ip | 0:1377bcf1455e | 128 | ~PCF2127(); |
nxp_ip | 0:1377bcf1455e | 129 | |
nxp_ip | 0:1377bcf1455e | 130 | /** Clock integrity check |
nxp_ip | 0:1377bcf1455e | 131 | * |
nxp_ip | 2:db76c68f998f | 132 | * @return non-zero value if the clock was stopped (means need to set the time) |
nxp_ip | 0:1377bcf1455e | 133 | */ |
nxp_ip | 0:1377bcf1455e | 134 | int is_init_required( void ); |
nxp_ip | 0:1377bcf1455e | 135 | |
nxp_ip | 0:1377bcf1455e | 136 | /** Set the time |
nxp_ip | 0:1377bcf1455e | 137 | * |
nxp_ip | 2:db76c68f998f | 138 | * @param dtp Pointer to struct tm |
nxp_ip | 2:db76c68f998f | 139 | * @return Error code (NO_ERROR==0) |
nxp_ip | 0:1377bcf1455e | 140 | */ |
nxp_ip | 1:700e0285cfd8 | 141 | int set_time( struct tm *dtp ); |
nxp_ip | 2:db76c68f998f | 142 | |
nxp_ip | 1:700e0285cfd8 | 143 | /** Set the time |
nxp_ip | 1:700e0285cfd8 | 144 | * |
nxp_ip | 2:db76c68f998f | 145 | * @param tp pointer to time_t |
nxp_ip | 2:db76c68f998f | 146 | * @return Error code (NO_ERROR==0) |
nxp_ip | 1:700e0285cfd8 | 147 | */ |
nxp_ip | 1:700e0285cfd8 | 148 | int set_time( time_t *tp ); |
nxp_ip | 0:1377bcf1455e | 149 | |
nxp_ip | 0:1377bcf1455e | 150 | /** Set the time |
nxp_ip | 2:db76c68f998f | 151 | * |
nxp_ip | 2:db76c68f998f | 152 | * @param s String data: The time information should be given in format of "YYYY MM DD HH MM SS" |
nxp_ip | 2:db76c68f998f | 153 | * @return Error code (NO_ERROR==0) |
nxp_ip | 0:1377bcf1455e | 154 | */ |
nxp_ip | 1:700e0285cfd8 | 155 | int set_time( char *s ); |
nxp_ip | 0:1377bcf1455e | 156 | |
nxp_ip | 0:1377bcf1455e | 157 | /** Get time of day |
nxp_ip | 0:1377bcf1455e | 158 | * |
nxp_ip | 0:1377bcf1455e | 159 | * This function works similar to "time()" in standard-C-library |
nxp_ip | 0:1377bcf1455e | 160 | * |
nxp_ip | 2:db76c68f998f | 161 | * @param tp Pointer to time_t |
nxp_ip | 2:db76c68f998f | 162 | * @return Error code (NO_ERROR==0) |
nxp_ip | 0:1377bcf1455e | 163 | */ |
nxp_ip | 0:1377bcf1455e | 164 | time_t time( time_t *tp ); |
nxp_ip | 0:1377bcf1455e | 165 | |
nxp_ip | 0:1377bcf1455e | 166 | /** Register access interface with integer to BCD conversion |
nxp_ip | 0:1377bcf1455e | 167 | * |
nxp_ip | 0:1377bcf1455e | 168 | * @param addr Register address |
nxp_ip | 2:db76c68f998f | 169 | * @param s Integer data. Converted to BCD before writing inot the register. |
nxp_ip | 2:db76c68f998f | 170 | * @return Error code (NO_ERROR==0) |
nxp_ip | 0:1377bcf1455e | 171 | */ |
nxp_ip | 1:700e0285cfd8 | 172 | int set_alarm( char addr, char s ); |
nxp_ip | 0:1377bcf1455e | 173 | |
nxp_ip | 0:1377bcf1455e | 174 | /** Clear interrupt flag |
nxp_ip | 0:1377bcf1455e | 175 | * |
nxp_ip | 0:1377bcf1455e | 176 | * Clears interrupt flags by writing 0x00 into Control_2 register |
nxp_ip | 0:1377bcf1455e | 177 | * |
nxp_ip | 2:db76c68f998f | 178 | * @return Error code (NO_ERROR==0) |
nxp_ip | 0:1377bcf1455e | 179 | */ |
nxp_ip | 1:700e0285cfd8 | 180 | int clear_intr( void ); |
nxp_ip | 0:1377bcf1455e | 181 | |
nxp_ip | 2:db76c68f998f | 182 | /** Writing data into RAM (for PCF2127 only) |
nxp_ip | 2:db76c68f998f | 183 | * |
nxp_ip | 2:db76c68f998f | 184 | * Write data into PCF2127 internal RAM |
nxp_ip | 2:db76c68f998f | 185 | * |
nxp_ip | 2:db76c68f998f | 186 | * @param address target address of internal RAM |
nxp_ip | 2:db76c68f998f | 187 | * @param *p pointer to write data buffer |
nxp_ip | 2:db76c68f998f | 188 | * @param size size of writing data |
nxp_ip | 2:db76c68f998f | 189 | * @return Error code (NO_ERROR==0) |
nxp_ip | 2:db76c68f998f | 190 | */ |
nxp_ip | 2:db76c68f998f | 191 | int RAM_write( int address, char *p, int size ); |
nxp_ip | 2:db76c68f998f | 192 | |
nxp_ip | 2:db76c68f998f | 193 | /** Reading data from RAM (for PCF2127 only) |
nxp_ip | 2:db76c68f998f | 194 | * |
nxp_ip | 2:db76c68f998f | 195 | * Read data from PCF2127 internal RAM |
nxp_ip | 2:db76c68f998f | 196 | * |
nxp_ip | 2:db76c68f998f | 197 | * @param address target address of internal RAM |
nxp_ip | 2:db76c68f998f | 198 | * @param *p pointer to read data buffer |
nxp_ip | 2:db76c68f998f | 199 | * @param size size of writing data |
nxp_ip | 2:db76c68f998f | 200 | * @return Error code (NO_ERROR==0) |
nxp_ip | 2:db76c68f998f | 201 | */ |
nxp_ip | 2:db76c68f998f | 202 | int RAM_read( int address, char *p, int size ); |
nxp_ip | 2:db76c68f998f | 203 | |
nxp_ip | 0:1377bcf1455e | 204 | private: |
nxp_ip | 0:1377bcf1455e | 205 | |
nxp_ip | 1:700e0285cfd8 | 206 | typedef enum { |
nxp_ip | 1:700e0285cfd8 | 207 | Cntl1 = 0x03, |
nxp_ip | 1:700e0285cfd8 | 208 | Cntl2 = 0x00, |
nxp_ip | 1:700e0285cfd8 | 209 | Cntl3 = 0x00, |
nxp_ip | 1:700e0285cfd8 | 210 | ClkOut = 0x46 |
nxp_ip | 1:700e0285cfd8 | 211 | } |
nxp_ip | 1:700e0285cfd8 | 212 | DefaultRegParam; |
nxp_ip | 2:db76c68f998f | 213 | |
nxp_ip | 1:700e0285cfd8 | 214 | int init( char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl ); |
nxp_ip | 1:700e0285cfd8 | 215 | int set_register( char addr, char data ); |
nxp_ip | 1:700e0285cfd8 | 216 | int read_register( char addr ); |
nxp_ip | 1:700e0285cfd8 | 217 | char i2bcd( char n ); |
nxp_ip | 1:700e0285cfd8 | 218 | char bcd2i( char bcd ); |
nxp_ip | 2:db76c68f998f | 219 | int set_RAM_address( char address ); |
nxp_ip | 2:db76c68f998f | 220 | |
nxp_ip | 1:700e0285cfd8 | 221 | I2C *i2c_p; |
nxp_ip | 1:700e0285cfd8 | 222 | I2C &i2c; |
nxp_ip | 0:1377bcf1455e | 223 | char device_address; |
nxp_ip | 0:1377bcf1455e | 224 | } |
nxp_ip | 0:1377bcf1455e | 225 | ; |
nxp_ip | 0:1377bcf1455e | 226 | #endif // end of "#ifndef MBED_PCF2127" |