"PCF2127A" : RTC chip with TCXO and quartz crystal demo Please refer >> http://mbed.org/users/okano/notebook/nxp_pcf2172a-demo-code/

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NXP_PCF2127A.h Source File

NXP_PCF2127A.h

00001 /*
00002  *  PCF2127A (Integrated RTC, TCXO and quartz crystal) demo 
00003  *  (library)
00004  *
00005  *  PCF2127A is a "real time clock (RTC)" module which is including a Xtal and TCXO 
00006  *  http://www.nxp.com/pip/PCF2127A_2.html
00007  *
00008  *  This is a just simple operation sample of the PCF2127A.
00009  *  In this sample, the PCF2127A is interfaced by I2C through pin9 and 10 of mbed. 
00010  *  And also the mbed-pin8 is connected to RTC's /INT pin. 
00011  *  The RTC chip is set to generate periodical interrupt in every seconds. 
00012  *  This interrupt triggers the update of the terminal and LCD screen. 
00013  *  
00014  *  RTC initializing part is ported from...
00015  *    http://mbed.org/users/roen/notebook/real-time/
00016  *
00017  *  Released under the MIT License: http://mbed.org/license/mit
00018  *
00019  *  revision 1.0    05-Jun-2010     (a) 1st release
00020  *  revision 1.1    05-Jun-2010     (a) class name changed
00021  *                              
00022  */
00023 
00024 
00025 #ifndef        MBED_NXP_PCF2127A
00026 #define        MBED_NXP_PCF2127A
00027 
00028 
00029 #include    "mbed.h"
00030 
00031 
00032 //  PCF2127A IIC address
00033 #define     PCF2127A_addr       0xA2
00034 
00035 //  PCF2127A registers
00036 #define     Control_1           0x00
00037 #define     Control_2           0x01
00038 #define     Control_3           0x02
00039 #define     Seconds             0x03
00040 #define     Minutes             0x04
00041 #define     Hours               0x05
00042 #define     Days                0x06
00043 #define     Weekdays            0x07
00044 #define     Months              0x08
00045 #define     Years               0x09
00046 #define     Second_alarm        0x0A
00047 #define     Minute_alarm        0x0B
00048 #define     Hour_alarm          0x0C
00049 #define     Day_alarm           0x0D
00050 #define     Weekday_alarm       0x0E
00051 #define     CLKOUT_ctl          0x0F
00052 #define     Watchdg_tim_ctl     0x10
00053 #define     Watchdg_tim_val     0x11
00054 #define     Timestp_ctl         0x12
00055 #define     Sec_timestp         0x13
00056 #define     Min_timestp         0x14
00057 #define     Hour_timestp        0x15
00058 #define     Day_timestp         0x16
00059 #define     Mon_timestp         0x17
00060 #define     Year_timestp        0x18
00061 #define     Aging_offset        0x19
00062 
00063 class NXP_PCF2127A {
00064 public:
00065 
00066     NXP_PCF2127A(
00067         PinName sda,
00068         PinName sdl,
00069         char dev_address = PCF2127A_addr,
00070         char vControl_1 = 0x03,
00071         char vControl_2 = 0x00,
00072         char vControl_3 = 0x60
00073     )
00074             : i2c( sda, sdl ), device_address( dev_address ) {
00075         set_register( Control_1, vControl_1 );
00076         set_register( Control_2, vControl_2 );
00077         set_register( Control_3, vControl_3 );
00078     }
00079 
00080     ~NXP_PCF2127A() {
00081     }
00082 
00083     int is_init_required( void ) {
00084         return ( read_register( Seconds ) & 0x80 ? 1 : 0 );
00085     }
00086 
00087     void set_time( void ) {
00088         struct tm   dt, *dtp;
00089         char        buf[ 8 ];
00090         char        c;
00091 
00092         dtp = &dt;
00093 
00094 //#define TIME_SETTING_TEST
00095 #ifndef TIME_SETTING_TEST
00096         printf("Enter current date and time:\r\n");
00097         printf("YYYY MM DD HH MM SS[enter]\r\n");
00098         scanf("%d %d %d %d %d %d", &(dtp->tm_year), &(dtp->tm_mon), &(dtp->tm_mday), &(dtp->tm_hour), &(dtp->tm_min), &(dtp->tm_sec) );
00099         printf("%d/%d/%d - %d:%d:%d\r\n", (dtp->tm_year), (dtp->tm_mon), (dtp->tm_mday), (dtp->tm_hour), (dtp->tm_min), (dtp->tm_sec) );
00100 
00101 #else
00102         dtp->tm_year    = 2010;
00103         dtp->tm_mon     = 12;
00104         dtp->tm_mday    = 31;
00105         dtp->tm_hour    = 23;
00106         dtp->tm_min     = 59;
00107         dtp->tm_sec     = 50;
00108 
00109 #endif
00110 
00111         // adjust for tm structure required values
00112         dtp->tm_year = dtp->tm_year - 1900;
00113         dtp->tm_mon  = dtp->tm_mon - 1;
00114 
00115         buf[ 0 ]    = Seconds;
00116         buf[ 1 ]    = i2bcd( dtp->tm_sec  );
00117         buf[ 2 ]    = i2bcd( dtp->tm_min  );
00118         buf[ 3 ]    = i2bcd( dtp->tm_hour );
00119         buf[ 4 ]    = i2bcd( dtp->tm_mday );
00120         buf[ 5 ]    = i2bcd( dtp->tm_wday );
00121         buf[ 6 ]    = i2bcd( dtp->tm_mon  + 1   );
00122         buf[ 7 ]    = i2bcd( dtp->tm_year - 100 );
00123 
00124         c    = read_register( Seconds );
00125         while ( c == read_register( Seconds ) )
00126             ;
00127 
00128         i2c.write( device_address, buf, 8 );
00129     }
00130 
00131 
00132     time_t time( time_t *tp ) {
00133         struct tm   dt, *dtp;
00134         time_t      t;
00135         char        buf[ 8 ]    = { Seconds };
00136 
00137         dtp = &dt;
00138 
00139         i2c.write( device_address, buf, 1 );
00140         i2c.read( device_address, buf, 7 );
00141 
00142         dtp->tm_sec     = bcd2i( buf[ 0 ] );
00143         dtp->tm_min     = bcd2i( buf[ 1 ] );
00144         dtp->tm_hour    = bcd2i( buf[ 2 ] );
00145         dtp->tm_mday    = bcd2i( buf[ 3 ] );
00146         dtp->tm_wday    = bcd2i( buf[ 4 ] );
00147         dtp->tm_mon     = bcd2i( buf[ 5 ] ) - 1;
00148         dtp->tm_year    = bcd2i( buf[ 6 ] ) + 100;
00149 
00150         t   = mktime( dtp );
00151 
00152         if ( tp )
00153             *tp  = t;
00154 
00155         return( t );
00156     }
00157 
00158     void set_alarm( char addr, char s ) {
00159         char    v;
00160 
00161         v   = i2bcd( s );
00162         set_register( addr, v );
00163     }
00164 
00165     void clear_intr( void ) {
00166         set_register( Control_2, 0x00 );
00167     }
00168 
00169 private:
00170     I2C     i2c;
00171     char    device_address;
00172 
00173     void set_register( char addr, char data ) {
00174         char    b[ 2 ];
00175 
00176         b[ 0 ]    = addr;
00177         b[ 1 ]    = data;
00178 
00179         i2c.write( device_address, b, 2 );
00180     }
00181 
00182     char read_register( char addr ) {
00183         char    data;
00184 
00185         data    = addr;
00186         i2c.write( device_address, &data, 1 );
00187         i2c.read( device_address, &data, 1 );
00188 
00189         return ( data );
00190     }
00191 
00192     char i2bcd( char n ) {
00193         return ( ((n / 10) << 4) | (n % 10) );
00194     }
00195 
00196     char bcd2i( char bcd ) {
00197         return ( ((bcd >> 4) * 10) + (bcd & 0x0F) );
00198     }
00199 }
00200 ;
00201 #endif  // end of "#ifndef MBED_NXP_PCF2127A"