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@0:1377bcf1455e, 2014-12-04 (annotated)
- Committer:
- nxp_ip
- Date:
- Thu Dec 04 04:58:24 2014 +0000
- Revision:
- 0:1377bcf1455e
- Child:
- 1:700e0285cfd8
initial version
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 | 0:1377bcf1455e | 5 | * @version 1.5 |
nxp_ip | 0:1377bcf1455e | 6 | * @date 04-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 | 0:1377bcf1455e | 57 | |
nxp_ip | 0:1377bcf1455e | 58 | /** Create a PCF2127 instance connected to specified I2C pins with specified address |
nxp_ip | 0:1377bcf1455e | 59 | * |
nxp_ip | 0:1377bcf1455e | 60 | * @param I2C_sda I2C-bus SDA pin |
nxp_ip | 0:1377bcf1455e | 61 | * @param I2C_scl I2C-bus SCL pin |
nxp_ip | 0:1377bcf1455e | 62 | * @param vControl_1 (option) data for Control_1 register |
nxp_ip | 0:1377bcf1455e | 63 | * @param vControl_2 (option) data for Control_2 register |
nxp_ip | 0:1377bcf1455e | 64 | * @param vControl_3 (option) data for Control_3 register |
nxp_ip | 0:1377bcf1455e | 65 | */ |
nxp_ip | 0:1377bcf1455e | 66 | PCF2127( PinName sda, PinName sdl, char vControl_1 = 0x03, char vControl_2 = 0x00, char vControl_3 = 0x60 ); |
nxp_ip | 0:1377bcf1455e | 67 | |
nxp_ip | 0:1377bcf1455e | 68 | /** Destractor |
nxp_ip | 0:1377bcf1455e | 69 | */ |
nxp_ip | 0:1377bcf1455e | 70 | ~PCF2127(); |
nxp_ip | 0:1377bcf1455e | 71 | |
nxp_ip | 0:1377bcf1455e | 72 | /** Clock integrity check |
nxp_ip | 0:1377bcf1455e | 73 | * |
nxp_ip | 0:1377bcf1455e | 74 | * @return non-zero value if the clock was stopped (means need to set the time) |
nxp_ip | 0:1377bcf1455e | 75 | */ |
nxp_ip | 0:1377bcf1455e | 76 | int is_init_required( void ); |
nxp_ip | 0:1377bcf1455e | 77 | |
nxp_ip | 0:1377bcf1455e | 78 | /** Set the time |
nxp_ip | 0:1377bcf1455e | 79 | * |
nxp_ip | 0:1377bcf1455e | 80 | * @param dtp Pointer to struct tm |
nxp_ip | 0:1377bcf1455e | 81 | */ |
nxp_ip | 0:1377bcf1455e | 82 | void set_time( struct tm *dtp ); |
nxp_ip | 0:1377bcf1455e | 83 | |
nxp_ip | 0:1377bcf1455e | 84 | /** Set the time |
nxp_ip | 0:1377bcf1455e | 85 | * @param s String data: The time information should be given in format of "YYYY MM DD HH MM SS" |
nxp_ip | 0:1377bcf1455e | 86 | */ |
nxp_ip | 0:1377bcf1455e | 87 | void set_time( char *s ); |
nxp_ip | 0:1377bcf1455e | 88 | |
nxp_ip | 0:1377bcf1455e | 89 | /** Get time of day |
nxp_ip | 0:1377bcf1455e | 90 | * |
nxp_ip | 0:1377bcf1455e | 91 | * This function works similar to "time()" in standard-C-library |
nxp_ip | 0:1377bcf1455e | 92 | * |
nxp_ip | 0:1377bcf1455e | 93 | * @param tp Pointer to time_t |
nxp_ip | 0:1377bcf1455e | 94 | */ |
nxp_ip | 0:1377bcf1455e | 95 | time_t time( time_t *tp ); |
nxp_ip | 0:1377bcf1455e | 96 | |
nxp_ip | 0:1377bcf1455e | 97 | /** Register access interface with integer to BCD conversion |
nxp_ip | 0:1377bcf1455e | 98 | * |
nxp_ip | 0:1377bcf1455e | 99 | * @param addr Register address |
nxp_ip | 0:1377bcf1455e | 100 | * @param Integer data. Converted to BCD before writing inot the register. |
nxp_ip | 0:1377bcf1455e | 101 | */ |
nxp_ip | 0:1377bcf1455e | 102 | void set_alarm( char addr, char s ); |
nxp_ip | 0:1377bcf1455e | 103 | |
nxp_ip | 0:1377bcf1455e | 104 | /** Clear interrupt flag |
nxp_ip | 0:1377bcf1455e | 105 | * |
nxp_ip | 0:1377bcf1455e | 106 | * Clears interrupt flags by writing 0x00 into Control_2 register |
nxp_ip | 0:1377bcf1455e | 107 | * |
nxp_ip | 0:1377bcf1455e | 108 | */ |
nxp_ip | 0:1377bcf1455e | 109 | void clear_intr( void ); |
nxp_ip | 0:1377bcf1455e | 110 | |
nxp_ip | 0:1377bcf1455e | 111 | private: |
nxp_ip | 0:1377bcf1455e | 112 | void set_register( char addr, char data ); |
nxp_ip | 0:1377bcf1455e | 113 | char read_register( char addr ); |
nxp_ip | 0:1377bcf1455e | 114 | char i2bcd( char n ); |
nxp_ip | 0:1377bcf1455e | 115 | char bcd2i( char bcd ); |
nxp_ip | 0:1377bcf1455e | 116 | |
nxp_ip | 0:1377bcf1455e | 117 | I2C i2c; |
nxp_ip | 0:1377bcf1455e | 118 | char device_address; |
nxp_ip | 0:1377bcf1455e | 119 | } |
nxp_ip | 0:1377bcf1455e | 120 | ; |
nxp_ip | 0:1377bcf1455e | 121 | #endif // end of "#ifndef MBED_PCF2127" |