Seeed / DS1337

Fork of DS1337 by wei zou

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1337.h Source File

DS1337.h

00001 /*
00002   DS1337.h - library for DS1337 rtc
00003 */
00004  
00005 // ensure this library description is only included once
00006 #ifndef DS1337_h
00007 #define DS1337_h
00008  
00009 // include types & constants of Wiring core API
00010 #include "mbed.h"
00011  
00012 typedef int8_t    byte;
00013 #define I2C_FREQ            100000
00014 // indices within the rtc_bcd[] buffer
00015 #define DS1337_SEC  0
00016 #define DS1337_MIN  1
00017 #define DS1337_HR   2
00018 #define DS1337_DOW  3
00019 #define DS1337_DATE 4
00020 #define DS1337_MTH  5
00021 #define DS1337_YR   6
00022  
00023 #define DS1337_BASE_YR      2000
00024  
00025 #define DS1337_CTRL_ID      0xD0
00026  
00027  
00028  
00029 // Define register bit masks
00030 #define DS1337_CLOCKHALT    (1<<7)
00031  
00032 #define DS1337_LO_BCD       0xf
00033 #define DS1337_HI_BCD       0xf0
00034  
00035 #define DS1337_HI_SEC       0x70
00036 #define DS1337_HI_MIN       0x70
00037 #define DS1337_HI_HR        0x30
00038 #define DS1337_LO_DOW       0x07
00039 #define DS1337_HI_DATE      0x30
00040 #define DS1337_HI_MTH       0x30
00041 #define DS1337_HI_YR        0xf0
00042  
00043 #define DS1337_ARLM1        0x07
00044 #define DS1337_ARLM1_LO_SEC 0xf
00045 #define DS1337_ARLM1_HI_SEC 0x70
00046 #define DS1337_ARLM1_LO_MIN 0x70
00047 #define DS1337_ARLM1_HI_MIN 0xf
00048  
00049 #define DS1337_SP           0x0E
00050 #define DS1337_SP_EOSC      (1<<7)
00051 #define DS1337_SP_RS2       (1<<4)
00052 #define DS1337_SP_RS1       (1<<3)
00053 #define DS1337_SP_INTCN     (1<<2)
00054 #define DS1337_SP_A2IE      (1<<1)
00055 #define DS1337_SP_A1IE      (1<<0)
00056  
00057 #define DS1337_STATUS       0x0F
00058 #define DS1337_STATUS_OSF   (1<<7)
00059 #define DS1337_STATUS_A2F   (1<<1)
00060 #define DS1337_STATUS_A1F   (1<<0)
00061  
00062 /* Definitions for alarm repeat */
00063 /* The private variable alarm_repeat holds the user's alarm repeat preference. However, the DS1337 encodes these in the topmost bit(s) of the 4 alarm registers. */
00064 /* Splattering these bits across the alarm regs is handled in the writeAlarm() function. */
00065 /* If DY/DT is set, the day field is interpreted as a DayOfWeek (1 ~ 7), else it is interpreted as a DayOfMonth.*/
00066  
00067 /* user alarm_repeat bit mask:
00068        7   6   5    4       3      2       1     0
00069       [x   x   x   A1M4   DY/DT   A1M3   A1M2   A1M1]
00070 */
00071  
00072 #define EVERY_SECOND       B00010111
00073 #define EVERY_MINUTE       B00010110
00074 #define EVERY_HOUR         B00010100
00075 #define EVERY_DAY          B00010000
00076 #define EVERY_WEEK         B00001000
00077 #define EVERY_MONTH        B00000000
00078  
00079  
00080 /* typedef struct {
00081   unsigned int year;
00082   unsigned char month;
00083   unsigned char day;
00084   unsigned char dayOfWeek;
00085   unsigned char hour;
00086   unsigned char minute;
00087   unsigned char second;
00088 } TIME; */
00089  
00090  
00091 // library interface description
00092 class DS1337 {
00093     // user-accessible "public" interface
00094 public:
00095     DS1337::DS1337(PinName sda, PinName scl) : i2c(sda,scl) {
00096         i2c.frequency(I2C_FREQ);
00097     };
00098     void readTime(void);
00099     unsigned char getSeconds();
00100     unsigned char getMinutes();
00101     unsigned char getHours();
00102     unsigned char getDays();
00103     unsigned char getDayOfWeek();
00104     unsigned char getMonths();
00105     unsigned int getYears();
00106  
00107     void setSeconds(unsigned char);
00108     void setMinutes(unsigned char);
00109     void setHours(unsigned char);
00110     void setDays(unsigned char);
00111     void setDayOfWeek(unsigned char);
00112     void setMonths(unsigned char);
00113     void setYears(unsigned int);
00114     
00115     void setTime();
00116  
00117  
00118     void    start(void);
00119     void    stop(void);
00120     unsigned char getRegister(unsigned char registerNumber);
00121     void    setRegister(unsigned char registerNumber, unsigned char registerValue);
00122     //void  unsetRegister(unsigned char registerNumber, unsigned char registerMask);
00123  
00124     // library-accessible "private" interface
00125 private: 
00126     I2C     i2c;  
00127     byte    time_set;
00128     byte alarm_repeat;
00129     byte    rtc_bcd[7]; // used prior to read/set DS1337 registers;
00130     void    read(void);
00131     void    save(void);
00132     byte bcd2bin(byte);
00133     byte bin2bcd(byte);
00134 };
00135  
00136 //extern DS1337 RTC_DS1337(P0_5,P0_4);
00137  
00138 #endif