Code to interface with the MCP7940 Real time clock. Supports getting and setting the time in 24 hour format

Committer:
MichaelW
Date:
Fri Oct 02 11:36:17 2015 +0000
Revision:
2:941a59078507
Parent:
1:8f330348d96b
Added some documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MichaelW 1:8f330348d96b 1 #ifndef __MCP7940_H__
MichaelW 1:8f330348d96b 2 #define __MCP7940_H__
MichaelW 1:8f330348d96b 3
MichaelW 1:8f330348d96b 4 #include "mbed.h"
MichaelW 2:941a59078507 5 /** MCP7940 class.
MichaelW 2:941a59078507 6 * Interface to the MCP7940 allowing you to get and set the time
MichaelW 2:941a59078507 7 * Example:
MichaelW 2:941a59078507 8 * @code
MichaelW 2:941a59078507 9 * #include "mbed.h"
MichaelW 2:941a59078507 10 * #include "MCP7940.h"
MichaelW 2:941a59078507 11 *
MichaelW 2:941a59078507 12 * MCP7940 clock(PinName sda, PinName scl);
MichaelW 2:941a59078507 13 *
MichaelW 2:941a59078507 14 * int main() {
MichaelW 2:941a59078507 15 *
MichaelW 2:941a59078507 16 * clock.setTime(2015, 9, 26, 22, 50);
MichaelW 2:941a59078507 17 * clock.StartClock();
MichaelW 2:941a59078507 18 * printf("%s \r\n" clock.TimeStamp());
MichaelW 2:941a59078507 19 * //or
MichaelW 2:941a59078507 20 * clock.getTime();
MichaelW 2:941a59078507 21 * printf("Day: %i, Month: %i, Year %i \r\n" clock.Day(), clock.Month(), clock.Year());
MichaelW 2:941a59078507 22 * }
MichaelW 2:941a59078507 23 * @endcode
MichaelW 2:941a59078507 24 */
MichaelW 1:8f330348d96b 25
MichaelW 2:941a59078507 26 class MCP7940
MichaelW 2:941a59078507 27 {
MichaelW 1:8f330348d96b 28 public:
MichaelW 2:941a59078507 29 /** Constructor
MichaelW 2:941a59078507 30 * Start year will default to 2000.
MichaelW 2:941a59078507 31 * @param sda I2C SDA pin
MichaelW 2:941a59078507 32 * @param scl I2C SCL pin
MichaelW 2:941a59078507 33 */
MichaelW 2:941a59078507 34 MCP7940(PinName sda, PinName scl);
MichaelW 2:941a59078507 35 /** Constructor
MichaelW 2:941a59078507 36 * Start year will default to 2000.
MichaelW 2:941a59078507 37 * @param sda I2C SDA pin
MichaelW 2:941a59078507 38 * @param scl I2C SCL pin
MichaelW 2:941a59078507 39 * @param StarYear The start year for you calender. The MCP7940 defines years as XX, so that this library can give XXXX years you need to set your year 0, ie 2000
MichaelW 2:941a59078507 40 */
MichaelW 2:941a59078507 41 MCP7940(PinName sda, PinName scl, int StartYear);
MichaelW 2:941a59078507 42 MCP7940();
MichaelW 2:941a59078507 43 /** Day
MichaelW 2:941a59078507 44 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 45 * @returns The day of the month
MichaelW 2:941a59078507 46 */
MichaelW 1:8f330348d96b 47 int Day();
MichaelW 2:941a59078507 48 /** Month
MichaelW 2:941a59078507 49 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 50 * @returns The month.
MichaelW 2:941a59078507 51 */
MichaelW 1:8f330348d96b 52 int Month();
MichaelW 2:941a59078507 53 /** Year
MichaelW 2:941a59078507 54 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 55 * @returns The Year as XXXX = StartYear + XX as in MCP7940
MichaelW 2:941a59078507 56 */
MichaelW 1:8f330348d96b 57 int Year();
MichaelW 2:941a59078507 58 /** Hour
MichaelW 2:941a59078507 59 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 60 * @returns the hour
MichaelW 2:941a59078507 61 */
MichaelW 1:8f330348d96b 62 int Hour();
MichaelW 2:941a59078507 63 /** Minutes
MichaelW 2:941a59078507 64 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 65 * @returns Minutes
MichaelW 2:941a59078507 66 */
MichaelW 1:8f330348d96b 67 int Minutes();
MichaelW 2:941a59078507 68 /** Seconds
MichaelW 2:941a59078507 69 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 70 * @returns seconds
MichaelW 2:941a59078507 71 */
MichaelW 1:8f330348d96b 72 int Seconds();
MichaelW 2:941a59078507 73 /** MilliSeconds
MichaelW 2:941a59078507 74 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 75 * Included for completeness - but MCP7940 doesn't have a mS counter
MichaelW 2:941a59078507 76 * @returns Milliseconds = 0 for MCP7940
MichaelW 2:941a59078507 77 */
MichaelW 1:8f330348d96b 78 int MilliSeconds();
MichaelW 2:941a59078507 79 /** DayOfWeek
MichaelW 2:941a59078507 80 * Note getTime() must have been called to update the value of day.
MichaelW 2:941a59078507 81 * @returns The day of the week, a number 1-7. This is user defined
MichaelW 2:941a59078507 82 */
MichaelW 1:8f330348d96b 83 int DayOfWeek();
MichaelW 1:8f330348d96b 84
MichaelW 2:941a59078507 85 /** SetTime
MichaelW 2:941a59078507 86 * Transfers the values of the time members into the MCP7940. Use one of the other setTime functions
MichaelW 2:941a59078507 87 */
MichaelW 2:941a59078507 88 int setTime();
MichaelW 2:941a59078507 89 /** SetTime
MichaelW 2:941a59078507 90 * Transfers the values of the time members into the MCP7940. Use one of the other setTime functions
MichaelW 2:941a59078507 91 * @param Year the year in YYYY
MichaelW 2:941a59078507 92 * @param Month
MichaelW 2:941a59078507 93 * @param Day the Day of the month
MichaelW 2:941a59078507 94 * @param Hour
MichaelW 2:941a59078507 95 * @param Minutes
MichaelW 2:941a59078507 96 */
MichaelW 1:8f330348d96b 97 int setTime(int Year, int Month, int Day, int Hour, int Mins);
MichaelW 2:941a59078507 98 /** SetTime
MichaelW 2:941a59078507 99 * Transfers the values of the time members into the MCP7940. Use one of the other setTime functions
MichaelW 2:941a59078507 100 * @param Year the year in YYYY
MichaelW 2:941a59078507 101 * @param Month
MichaelW 2:941a59078507 102 * @param Day the Day of the month
MichaelW 2:941a59078507 103 * @param Hour
MichaelW 2:941a59078507 104 * @param Minutes
MichaelW 2:941a59078507 105 * @param Secs
MichaelW 2:941a59078507 106 */
MichaelW 1:8f330348d96b 107 int setTime(int Year, int Month, int Day, int Hour, int Mins, int Secs);
MichaelW 2:941a59078507 108 /** SetTime
MichaelW 2:941a59078507 109 * Transfers the values of the time members into the MCP7940. Use one of the other setTime functions
MichaelW 2:941a59078507 110 * @param Year the year in YYYY
MichaelW 2:941a59078507 111 * @param Month
MichaelW 2:941a59078507 112 * @param Day the Day of the month
MichaelW 2:941a59078507 113 * @param Hour
MichaelW 2:941a59078507 114 * @param Minutes
MichaelW 2:941a59078507 115 * @param Secs
MichaelW 2:941a59078507 116 * @param MiliSecs - note only for completeness and future extension as MCP7940 doesn't include mS
MichaelW 2:941a59078507 117 */
MichaelW 1:8f330348d96b 118 int setTime(int Year, int Month, int Day, int Hour, int Mins, int Secs, int MiliSecs);
MichaelW 2:941a59078507 119 /** SetTime
MichaelW 2:941a59078507 120 * Transfers the values of the time members into the MCP7940. Use one of the other setTime functions
MichaelW 2:941a59078507 121 * @param Year the year in YYYY
MichaelW 2:941a59078507 122 * @param Month
MichaelW 2:941a59078507 123 * @param Day the Day of the month
MichaelW 2:941a59078507 124 * @param Hour
MichaelW 2:941a59078507 125 * @param Minutes
MichaelW 2:941a59078507 126 * @param Secs
MichaelW 2:941a59078507 127 * @param MiliSecs - note only for completeness and future extension as MCP7940 doesn't include mS
MichaelW 2:941a59078507 128 * @param DayOfWeek - the day of the week as a number 1-7, User defined
MichaelW 2:941a59078507 129 */
MichaelW 1:8f330348d96b 130 int setTime(int Year, int Month, int Day, int Hour, int Mins, int Secs, int MiliSecs, int DayOfWeek);
MichaelW 2:941a59078507 131
MichaelW 1:8f330348d96b 132 int setDefaultTime();
MichaelW 2:941a59078507 133 /** getTime
MichaelW 2:941a59078507 134 * Loads the time from the MCP7940 so its upto date and can be accessed by the Day(), Month()... functions
MichaelW 2:941a59078507 135 */
MichaelW 1:8f330348d96b 136 int getTime();
MichaelW 2:941a59078507 137 /** StartClock
MichaelW 2:941a59078507 138 * Clears and sets bit 7 of register 0x00 to start the clock. Keeps the current values of 0x00 bits 0-6
MichaelW 2:941a59078507 139 */
MichaelW 1:8f330348d96b 140 void StartClock();
MichaelW 2:941a59078507 141
MichaelW 2:941a59078507 142 /** TimeStamp
MichaelW 2:941a59078507 143 * getTime() is called within this function so does not need to be called from code.
MichaelW 2:941a59078507 144 * @returns A pointer to a time stamp as YYYY-MM-DDTHH:MM:SS
MichaelW 2:941a59078507 145 */
MichaelW 1:8f330348d96b 146 char * TimeStamp();
MichaelW 1:8f330348d96b 147 void TimeStamp(char * buf);
MichaelW 1:8f330348d96b 148 void niceTimeStamp(char * buf);
MichaelW 1:8f330348d96b 149 void niceDate(char * buf);
MichaelW 1:8f330348d96b 150 void niceTime(char * buf);
MichaelW 2:941a59078507 151
MichaelW 2:941a59078507 152 private:
MichaelW 1:8f330348d96b 153 char _TimeStamp[20];
MichaelW 1:8f330348d96b 154 int _Day;
MichaelW 1:8f330348d96b 155 int _Month;
MichaelW 1:8f330348d96b 156 int _Year;
MichaelW 1:8f330348d96b 157 int _Hour;
MichaelW 1:8f330348d96b 158 int _Minutes;
MichaelW 2:941a59078507 159 int _Seconds;
MichaelW 1:8f330348d96b 160 int _MilliSeconds;
MichaelW 1:8f330348d96b 161 int _DayOfWeek;
MichaelW 2:941a59078507 162
MichaelW 1:8f330348d96b 163 int _YearStart;
MichaelW 2:941a59078507 164
MichaelW 2:941a59078507 165 protected:
MichaelW 1:8f330348d96b 166 I2C Clock;
MichaelW 2:941a59078507 167 char IntToBCD(char Data);
MichaelW 2:941a59078507 168 char BCDtoInt(char Data);
MichaelW 2:941a59078507 169 int _addr;
MichaelW 2:941a59078507 170
MichaelW 2:941a59078507 171
MichaelW 1:8f330348d96b 172 };
MichaelW 1:8f330348d96b 173
MichaelW 1:8f330348d96b 174
MichaelW 1:8f330348d96b 175 #endif