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@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 | #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 | 0:1377bcf1455e | 54 | Aging_offset |
nxp_ip | 0:1377bcf1455e | 55 | } |
nxp_ip | 0:1377bcf1455e | 56 | RegisterName; |
nxp_ip | 1:700e0285cfd8 | 57 | |
nxp_ip | 1:700e0285cfd8 | 58 | /** Error code */ |
nxp_ip | 1:700e0285cfd8 | 59 | typedef enum { |
nxp_ip | 1:700e0285cfd8 | 60 | NO_ERROR = 0, |
nxp_ip | 1:700e0285cfd8 | 61 | CLOCK_INTEGRITY_FAIL = 1, |
nxp_ip | 1:700e0285cfd8 | 62 | I2C_ACCESS_FAIL = 2, |
nxp_ip | 1:700e0285cfd8 | 63 | TIME_FUNC_ERROR = ((time_t)-1) |
nxp_ip | 1:700e0285cfd8 | 64 | } |
nxp_ip | 1:700e0285cfd8 | 65 | ErrorNum; |
nxp_ip | 1:700e0285cfd8 | 66 | |
nxp_ip | 0:1377bcf1455e | 67 | /** Create a PCF2127 instance connected to specified I2C pins with specified address |
nxp_ip | 0:1377bcf1455e | 68 | * |
nxp_ip | 0:1377bcf1455e | 69 | * @param I2C_sda I2C-bus SDA pin |
nxp_ip | 0:1377bcf1455e | 70 | * @param I2C_scl I2C-bus SCL pin |
nxp_ip | 1:700e0285cfd8 | 71 | * @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute) |
nxp_ip | 0:1377bcf1455e | 72 | * @param vControl_2 (option) data for Control_2 register |
nxp_ip | 1:700e0285cfd8 | 73 | * @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode) |
nxp_ip | 1:700e0285cfd8 | 74 | * @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin) |
nxp_ip | 1:700e0285cfd8 | 75 | CLKOUT_ctl |
nxp_ip | 0:1377bcf1455e | 76 | */ |
nxp_ip | 1:700e0285cfd8 | 77 | 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 | 78 | |
nxp_ip | 1:700e0285cfd8 | 79 | /** Create a PCF2127 instance connected to specified I2C pins with specified address |
nxp_ip | 1:700e0285cfd8 | 80 | * |
nxp_ip | 1:700e0285cfd8 | 81 | * @param i2c I2C object (instance) |
nxp_ip | 1:700e0285cfd8 | 82 | * @param vControl_1 (option) data for Control_1 register (default setting generates interrupts by second and minute) |
nxp_ip | 1:700e0285cfd8 | 83 | * @param vControl_2 (option) data for Control_2 register |
nxp_ip | 1:700e0285cfd8 | 84 | * @param vControl_3 (option) data for Control_3 register (default setting of battery switch-over function as standard mode) |
nxp_ip | 1:700e0285cfd8 | 85 | * @param vCLKOUT_ctl (option) data for CLKOUT_ctl register (default setting 1Hz output on CLKOUT pin) |
nxp_ip | 1:700e0285cfd8 | 86 | */ |
nxp_ip | 1:700e0285cfd8 | 87 | PCF2127( I2C &i2c, char vControl_1 = Cntl1, char vControl_2 = Cntl2, char vControl_3 = Cntl1, char vCLKOUT_ctl = ClkOut ); |
nxp_ip | 1:700e0285cfd8 | 88 | |
nxp_ip | 0:1377bcf1455e | 89 | /** Destractor |
nxp_ip | 0:1377bcf1455e | 90 | */ |
nxp_ip | 0:1377bcf1455e | 91 | ~PCF2127(); |
nxp_ip | 0:1377bcf1455e | 92 | |
nxp_ip | 0:1377bcf1455e | 93 | /** Clock integrity check |
nxp_ip | 0:1377bcf1455e | 94 | * |
nxp_ip | 0:1377bcf1455e | 95 | * @return non-zero value if the clock was stopped (means need to set the time) |
nxp_ip | 0:1377bcf1455e | 96 | */ |
nxp_ip | 0:1377bcf1455e | 97 | int is_init_required( void ); |
nxp_ip | 0:1377bcf1455e | 98 | |
nxp_ip | 0:1377bcf1455e | 99 | /** Set the time |
nxp_ip | 0:1377bcf1455e | 100 | * |
nxp_ip | 0:1377bcf1455e | 101 | * @param dtp Pointer to struct tm |
nxp_ip | 0:1377bcf1455e | 102 | */ |
nxp_ip | 1:700e0285cfd8 | 103 | int set_time( struct tm *dtp ); |
nxp_ip | 1:700e0285cfd8 | 104 | |
nxp_ip | 1:700e0285cfd8 | 105 | /** Set the time |
nxp_ip | 1:700e0285cfd8 | 106 | * |
nxp_ip | 1:700e0285cfd8 | 107 | * @param tp pointer to time_t |
nxp_ip | 1:700e0285cfd8 | 108 | */ |
nxp_ip | 1:700e0285cfd8 | 109 | int set_time( time_t *tp ); |
nxp_ip | 0:1377bcf1455e | 110 | |
nxp_ip | 0:1377bcf1455e | 111 | /** Set the time |
nxp_ip | 0:1377bcf1455e | 112 | * @param s String data: The time information should be given in format of "YYYY MM DD HH MM SS" |
nxp_ip | 0:1377bcf1455e | 113 | */ |
nxp_ip | 1:700e0285cfd8 | 114 | int set_time( char *s ); |
nxp_ip | 0:1377bcf1455e | 115 | |
nxp_ip | 0:1377bcf1455e | 116 | /** Get time of day |
nxp_ip | 0:1377bcf1455e | 117 | * |
nxp_ip | 0:1377bcf1455e | 118 | * This function works similar to "time()" in standard-C-library |
nxp_ip | 0:1377bcf1455e | 119 | * |
nxp_ip | 0:1377bcf1455e | 120 | * @param tp Pointer to time_t |
nxp_ip | 0:1377bcf1455e | 121 | */ |
nxp_ip | 0:1377bcf1455e | 122 | time_t time( time_t *tp ); |
nxp_ip | 0:1377bcf1455e | 123 | |
nxp_ip | 0:1377bcf1455e | 124 | /** Register access interface with integer to BCD conversion |
nxp_ip | 0:1377bcf1455e | 125 | * |
nxp_ip | 0:1377bcf1455e | 126 | * @param addr Register address |
nxp_ip | 0:1377bcf1455e | 127 | * @param Integer data. Converted to BCD before writing inot the register. |
nxp_ip | 0:1377bcf1455e | 128 | */ |
nxp_ip | 1:700e0285cfd8 | 129 | int set_alarm( char addr, char s ); |
nxp_ip | 0:1377bcf1455e | 130 | |
nxp_ip | 0:1377bcf1455e | 131 | /** Clear interrupt flag |
nxp_ip | 0:1377bcf1455e | 132 | * |
nxp_ip | 0:1377bcf1455e | 133 | * Clears interrupt flags by writing 0x00 into Control_2 register |
nxp_ip | 0:1377bcf1455e | 134 | * |
nxp_ip | 0:1377bcf1455e | 135 | */ |
nxp_ip | 1:700e0285cfd8 | 136 | int clear_intr( void ); |
nxp_ip | 0:1377bcf1455e | 137 | |
nxp_ip | 0:1377bcf1455e | 138 | private: |
nxp_ip | 0:1377bcf1455e | 139 | |
nxp_ip | 1:700e0285cfd8 | 140 | typedef enum { |
nxp_ip | 1:700e0285cfd8 | 141 | Cntl1 = 0x03, |
nxp_ip | 1:700e0285cfd8 | 142 | Cntl2 = 0x00, |
nxp_ip | 1:700e0285cfd8 | 143 | Cntl3 = 0x00, |
nxp_ip | 1:700e0285cfd8 | 144 | ClkOut = 0x46 |
nxp_ip | 1:700e0285cfd8 | 145 | } |
nxp_ip | 1:700e0285cfd8 | 146 | DefaultRegParam; |
nxp_ip | 1:700e0285cfd8 | 147 | |
nxp_ip | 1:700e0285cfd8 | 148 | int init( char vControl_1, char vControl_2, char vControl_3, char vCLKOUT_ctl ); |
nxp_ip | 1:700e0285cfd8 | 149 | int set_register( char addr, char data ); |
nxp_ip | 1:700e0285cfd8 | 150 | int read_register( char addr ); |
nxp_ip | 1:700e0285cfd8 | 151 | char i2bcd( char n ); |
nxp_ip | 1:700e0285cfd8 | 152 | char bcd2i( char bcd ); |
nxp_ip | 1:700e0285cfd8 | 153 | |
nxp_ip | 1:700e0285cfd8 | 154 | I2C *i2c_p; |
nxp_ip | 1:700e0285cfd8 | 155 | I2C &i2c; |
nxp_ip | 0:1377bcf1455e | 156 | char device_address; |
nxp_ip | 0:1377bcf1455e | 157 | } |
nxp_ip | 0:1377bcf1455e | 158 | ; |
nxp_ip | 0:1377bcf1455e | 159 | #endif // end of "#ifndef MBED_PCF2127" |