Dennis Smith / PCF8583_rtc

Dependents:   NixieClock800Max

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCF8583_rtc.h Source File

PCF8583_rtc.h

00001 #include <mbed.h>
00002 
00003 #define PCF8583_addr_1      0xA0     // PCF8583 Write address,
00004 #define PCF8583_addr_2      0xA2     // PCF8583 Write address,
00005 #define ON                  1
00006 #define OFF                 0
00007 
00008 //Configuration Alarm Control Register
00009 #define alarm_enable_bit    0x04
00010 #define alarm_flag          0x02;
00011 #define timer_hundsek       0x01
00012 #define timer_seconds       0x02
00013 #define timer_minutes       0x03
00014 #define timer_hours         0x04
00015 #define timer_days          0x05
00016 #define timer_int_enable    0x08
00017 #define daily_alarm         0x10
00018 #define weekday_alarm       0x20
00019 #define dated_alarm         0x30
00020 #define timer_alarm_enable  0x40
00021 #define alarm_int_enable    0x80
00022 #define timer_alarm_enable  0x40
00023 #define alarm_int_enable    0x80
00024 
00025 // Use the first 2 NVRAM addresses for the century and year bytes.
00026 #define CENTURY_REG         0x10
00027 #define YEAR_REG            0x11
00028 #define USER_REG            0x12
00029 
00030 #define TIME                1
00031 #define ALARM               9
00032 
00033 /// Structures
00034 struct Time_t {
00035   bool fmt_hours;
00036   bool am_pm_flag;
00037   char hours;
00038   char minutes;
00039   char seconds;
00040   char hundreds;
00041 };
00042 
00043 struct Date_t {
00044   char day;
00045   char month;
00046   char year;
00047   char century;
00048   char weekday;
00049 };
00050 
00051 struct DateTime_t {
00052   struct Date_t date;
00053   struct Time_t time;
00054 };
00055 
00056 class PCF8583rtc {
00057   I2C *_i2c;
00058   
00059 public:
00060 
00061 /**
00062  * Set these public variables according to your locale, default (in brackets) is Australian English.
00063  *  char *ShortDateFormat;  ("d,m,yy")
00064  *  char *LongDateFormat;   ("dddd dd mmmm yyyy")
00065  *  char *ShortTimeFormat;  ("d:m:yy")
00066  *  char *LongTimeFormat;   ("dd:nn:yyyy")
00067  *
00068  *  char DateSeparator;     ("\")
00069  *  char TimeSeparator;     (":")
00070  *
00071  *  char *ShortDayNames[7];    ({"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"})
00072  *  char *LongDayNames[7];     ({"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"})
00073  *  char *ShortMonthNames[12]; ({"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"})
00074  *  char *LongMonthNames[12];  ({"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"})
00075  *
00076 * Example:
00077 * @code
00078 * #include <mbed.h>
00079 * #include "PCF8583_rtc.h"
00080 *
00081 * I2C i2c(P0_10, P0_11);       // sda, scl
00082 * PCF8583rtc rtc(&i2c, PCF8583_addr_2);
00083 * 
00084 *    struct DateTime_t dtl;
00085 *
00086 *    //Set the time
00087 *    dtl = rtc.read(TIME);
00088 *    dtl.time.hours = rtc.bin2bcd(11);
00089 *    dtl.time.minutes = rtc.bin2bcd(43);
00090 *    rtc.write(TIME, dtl);
00091 *
00092 *    //Read and display the time on the nixie display
00093 *    dtl = rtc.read(TIME);
00094 *    i2c.write(ADDR_8574_1, &dtl.time.hours, 1);
00095 *    i2c.write(ADDR_8574_2, &dtl.time.minutes, 1);
00096 *
00097 * @endcode
00098 
00099   PCF8583rtc(I2C *i2c, char I2cAddress);
00100 
00101   DateTime_t read(const char address);
00102   void   write(const char address, DateTime_t dti);
00103   void   FormatDateTime(char *dest, char *format);
00104   bool   WriteNVram(char address, char * value, char num);
00105   bool   ReadNVram(char address, char * dest, char num);
00106   void   SetDateTime(DateTime_t dti);
00107   struct DateTime_t GetDateTimeBCD(void);
00108 
00109 */
00110 
00111   char *ShortDateFormat;
00112   char *LongDateFormat;
00113   char *ShortTimeFormat;
00114   char *LongTimeFormat;
00115 
00116   char DateSeparator;
00117   char TimeSeparator;
00118 
00119   char *ShortDayNames[7];
00120   char *LongDayNames[7];
00121   char *ShortMonthNames[12];
00122   char *LongMonthNames[12];
00123     
00124   PCF8583rtc(I2C *i2c, char I2cAddress);
00125 
00126 /** read the current Time or Alarm
00127 *
00128 * @param address "TIME" = read the time, "ALARM" = read the alarm
00129 * @returns
00130 *   a DateTime_t structure
00131 */
00132   DateTime_t read(const char address);
00133 
00134 /** write the current Time or Alarm
00135 *
00136 * @param address "TIME" = read the time, "ALARM" = read the alarm 
00137 * @param dti a DateTime_t structure containing the time or alarm data 
00138 */
00139   void write(const char address, DateTime_t dti);
00140 
00141   void FormatDateTime(char *dest, char *format);
00142 
00143   bool WriteNVram(char address, char * value, char num);
00144 
00145   bool ReadNVram(char address, char * dest, char num);
00146 
00147   void SetDateTime(DateTime_t dti);
00148 
00149   char bin2bcd(char value);
00150   
00151   void configureAlarmReg(char alarm);
00152   void configureControlReg(char control);
00153 
00154 private:
00155   struct DateTime_t dt;
00156   char   _I2cAddress;
00157 
00158   char Bcd2Char(char *d, char val, char WantLeadZero);
00159   void enableCounting(void);
00160   void pauseCounting(void);
00161   char readByte(char address);
00162   void writeByte(char address, char d);
00163 };
00164 
00165 /** PCF8583_rtc class.
00166  *
00167  * Example:
00168  * @code
00169  * #include <mbed.h>
00170  * #include <PCF8583_rtc.h>
00171  *
00172  * I2C i2c(P0_10, P0_11);       // sda, scl
00173  * PCF8583rtc rtc(&i2c);
00174  *
00175  * int main() { 
00176  *  
00177  * rtc.read(TIME);
00178  * i2c.write(ADDR_8574_1, &rtc.HoursBCD, 1);   //write hours to display
00179  * i2c.write(ADDR_8574_2, &rtc.MinsBCD, 1);    //write minutes to display
00180  * }
00181  * @endcode
00182  */
00183  
00184 /** Create a PCF8583rtc object using a pointer to the given I2C object.
00185  *
00186  * @param i2c pointer to an I2C object to which the PCF8583 device is connected.
00187  */
00188 //PCF8583rtc(I2C *i2c);
00189 
00190 /** the Read_DateTime function.
00191  *
00192  * @param address  Determines whether to read the date/time or alarm registers
00193  *                 Values are retrieved from the internal datetime structure
00194  */
00195 //void read(const char address);
00196 
00197 /** the Write_DateTime function.
00198  *
00199  * @param address  Determines whether to set the date/time or alarm registers
00200  *                 Values are first set into the internal datetime structure prior to calling
00201  */
00202 //void write(const char address);
00203  
00204 /** the FormatDateTime function.
00205  *
00206  * @param dest   a pointer to a char array where formatted values are to be placed
00207  * @param format a pointer to a string describing how to format the values 
00208  */
00209 //void FormatDateTime(char *dest, char *format);
00210 
00211 /** the WriteNVram function.
00212  *
00213  * @param address  address where value is to be placed
00214  * @param value    pointer to char array containing the value(s) to be stored
00215  * @param num      the number bytes to store
00216  */
00217 //void WriteNVram(char address, char *value, char num);
00218 
00219 //#endif