Wiblocks --- RTC

RTC/RTC.h

00001 #ifndef __RTC_H__
00002 #define __RTC_H__
00003 
00004 #if defined(ARDUINO) && (ARDUINO >= 100)
00005 #else
00006 #include <inttypes.h>
00007 #endif
00008 
00009 
00010 
00011 
00012 
00013 #define RTC_REG_SECS      0x00
00014 #define RTC_REG_MINS      0x01
00015 #define RTC_REG_HOURS     0x02
00016 #define RTC_REG_DAYOFWEEK 0x03
00017 #define RTC_REG_DAY       0x04
00018 #define RTC_REG_MONTH     0x05
00019 #define RTC_REG_YEAR      0x06
00020 
00024 
00025 #define RTC_REG_A1SECS    0x07
00026 #define RTC_REG_A1MINS    0x08
00027 #define RTC_REG_A1HOURS   0x09
00028 #define RTC_REG_A1DOW     0x0A
00029 #define RTC_REG_A1DAY     0x0A
00030 
00031 #define RTC_REG_A2MINS    0x0B
00032 #define RTC_REG_A2HOURS   0x0C
00033 #define RTC_REG_A2DOW     0x0D
00034 #define RTC_REG_A2DAY     0x0D
00035 
00039 
00040 #define RTC_REG_CONTROL   0x0E
00041 #define RTC_REG_STATUS    0x0F
00042 
00043 #define RTC_LAST_REG      RTC_REG_STATUS
00044 
00048 
00049 #define RTC_EOSC          0x80
00050 #define RTC_RS2           0x10
00051 #define RTC_RS1           0x08
00052 #define RTC_INTCN         0x04
00053 #define RTC_A2IE          0x02
00054 #define RTC_A1IE          0x01
00055 
00059 
00060 #define RTC_OSF           0x80
00061 #define RTC_A2F           0x02
00062 #define RTC_A1F           0x01
00063 
00068 
00069 #define RTC_ALARM1_MODE1  0x0F ///< once per second
00070 #define RTC_ALARM1_MODE2  0x0E ///< seconds match
00071 #define RTC_ALARM1_MODE3  0x0C ///< minutes and seconds match
00072 #define RTC_ALARM1_MODE4  0x08 ///< hours, minutes, seconds, match
00073 #define RTC_ALARM1_MODE5  0x00 ///< day, hours, minutes, seconds, match
00074 #define RTC_ALARM1_MODE6  0x10 ///< dow, hours, minutes, seconds, match
00075 
00079 
00080 #define PM 1
00081 #define AM 0
00082 
00083 class RTC {
00084  private:
00085   // EEPROM Registers
00086   union {
00087     struct {
00088       unsigned i2c_addr   : 8;
00089 
00090       unsigned secs       : 4;
00091       unsigned secs_10    : 3;
00092       unsigned UNUSED_0   : 1;
00093       
00094       unsigned mins       : 4;
00095       unsigned mins_10    : 3;
00096       unsigned UNUSED_1   : 1;
00097 
00098       unsigned hours      : 4;
00099       unsigned hours_10   : 2; // pm is msb
00100       unsigned hour12     : 1;
00101       unsigned UNUSED_2   : 1;
00102 
00103       unsigned dow        : 3;
00104       unsigned UNUSED_3   : 5;
00105     
00106       unsigned day        : 4;
00107       unsigned day_10     : 2;
00108       unsigned UNUSED_4   : 2;
00109       
00110       unsigned month      : 4;
00111       unsigned month_10   : 1;
00112       unsigned century    : 1;
00113       unsigned UNUSED_5   : 2;
00114     
00115       unsigned year       : 4;
00116       unsigned year_10    : 4;
00117 
00118       unsigned a1_secs       : 4;
00119       unsigned a1_secs_10    : 3;
00120       unsigned a1m1          : 1;
00121       
00122       unsigned a1_mins       : 4;
00123       unsigned a1_mins_10    : 3;
00124       unsigned a1m2          : 1;
00125 
00126       unsigned a1_hours      : 4;
00127       unsigned a1_hours_10   : 1;
00128       unsigned a1_pm         : 1;
00129       unsigned a1_hour12     : 1;
00130       unsigned a1m3          : 1;
00131 
00132       unsigned a1_day        : 4;
00133       unsigned a1_day10      : 2;
00134       unsigned a1_dy         : 1;
00135       unsigned a1m4          : 1;
00136     
00137       unsigned a2_mins       : 4;
00138       unsigned a2_mins_10    : 3;
00139       unsigned a2m2          : 1;
00140 
00141       unsigned a2_hours      : 4;
00142       unsigned a2_hours_10   : 1;
00143       unsigned a2_pm         : 1;
00144       unsigned a2_hour12     : 1;
00145       unsigned a2m3          : 1;
00146 
00147       unsigned a2_day        : 4;
00148       unsigned a2_day_10     : 2;
00149       unsigned a2_dy         : 1;
00150       unsigned a2m4          : 1;
00151 
00152       unsigned a1ie          : 1;
00153       unsigned a2ie          : 1;
00154       unsigned intcn         : 1;
00155       unsigned rs1           : 1;
00156       unsigned rs2           : 1;
00157       unsigned UNUSED_6      : 2;
00158       unsigned n_eosc        : 1;
00159       
00160       unsigned a1f           : 1;
00161       unsigned a2f           : 1;
00162       unsigned UNUSED_7      : 5;
00163       unsigned osf           : 1;
00164     
00165     } reg_bits;
00166     struct {
00167       unsigned char i2c_addr;
00168       unsigned char secs;
00169       unsigned char mins;
00170       unsigned char hours;
00171       unsigned char dayofweek;
00172       unsigned char day;
00173       unsigned char month;
00174       unsigned char year;
00175 
00176       unsigned char a1_secs;
00177       unsigned char a1_mins;
00178       unsigned char a1_hours;
00179       unsigned char a1_day;
00180 
00181       unsigned char a2_mins;
00182       unsigned char a2_hours;
00183       unsigned char a2_day;
00184 
00185       unsigned char control;
00186       unsigned char status;
00187     } reg_bytes;
00188     unsigned char reg_array[17];
00189   } _regs;
00190   unsigned char _buf[17];
00191  public:
00192   static const unsigned char _i2c_rd_address = (0x68 << 1) | 0x01; 
00193   static const unsigned char _i2c_wr_address = (0x68 << 1) | 0x00; 
00194   RTC();
00195   char write_reg_ptr(unsigned char reg);
00196   char write_reg(unsigned char reg, unsigned char v);
00197   char write_regs(unsigned char reg, unsigned char *v, unsigned char n);
00198   unsigned char read_reg(unsigned char reg);
00199   char read_regs();
00200   unsigned char get_secs();
00201   unsigned char get_mins();
00202   unsigned char get_hours();
00203   unsigned char get_day();  
00204   unsigned char get_month();
00205   unsigned int  get_year();
00206   unsigned char set_year(unsigned int year);
00207   unsigned char set_month(unsigned char month);
00208   unsigned char set_day(unsigned char day);
00209   unsigned char set_date(unsigned int year, unsigned char month, 
00210                          unsigned char day);
00211   unsigned char set_secs(unsigned char secs);
00212   unsigned char set_mins(unsigned char mins);
00213   unsigned char set_hours12(unsigned char hours, unsigned char pm_p);
00214   unsigned char set_hours(unsigned char hours);
00215   unsigned char set_time(unsigned char hours, unsigned char mins, 
00216                          unsigned char secs);
00217   unsigned char set_time12(unsigned char hours, unsigned char mins, 
00218                            unsigned char secs, unsigned char pm_p);
00219   unsigned char set_alarm1(unsigned char mode, unsigned char day_or_dow,
00220                            unsigned char hours, unsigned char mins, 
00221                            unsigned char secs);
00222   unsigned char enable_alarm1(void);
00223   unsigned char clear_alarm1(void);
00224   void localtime(char *str);
00225   unsigned char get_reg(unsigned char reg);
00226 };
00227 
00228 #endif