Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MCP7940.cpp
00001 #include "MCP7940.h" 00002 00003 MCP7940::MCP7940(PinName sda, PinName scl):Clock(sda, scl){ 00004 _addr = 0xDE; // 1101 111x 00005 Clock.frequency(100000); 00006 _YearStart = 2000; 00007 } 00008 MCP7940::MCP7940(PinName sda, PinName scl, int StartYear):Clock(sda, scl){ 00009 _addr = 0xDE; // 1101 111x 00010 Clock.frequency(100000); 00011 _YearStart = StartYear; 00012 } 00013 00014 char MCP7940::IntToBCD(char Data){ 00015 char Tens = Data / 10; 00016 char Units = Data % 10; 00017 char BCD = (Tens << 4 | Units); 00018 return(BCD); 00019 } 00020 char MCP7940::BCDtoInt(char Data){ 00021 char Value; 00022 Value = (((Data & 0xF0 ) >> 4) * 10) + (((Data & 0x0F ))); 00023 return(Value); 00024 } 00025 00026 int MCP7940::setTime(int Year, int Month, int Day, int Hour, int Mins){ 00027 return(setTime(Year, Month, Day, Hour, Mins, 0, 0)); 00028 } 00029 int MCP7940::setTime(int Year, int Month, int Day, int Hour, int Mins, int Secs){ 00030 return(setTime(Year, Month, Day, Hour, Mins, Secs, 0)); 00031 } 00032 int MCP7940::setTime(int Year, int Month, int Day, int Hour, int Mins, int Secs, int MiliSecs){ 00033 return(setTime(Year, Month, Day, Hour, Mins, Secs, 0,1)); 00034 } 00035 int MCP7940::setTime(int Year, int Month, int Day, int Hour, int Mins, int Secs, int MiliSecs, int DayOfWeek){ 00036 _Day = Day; 00037 _Month = Month; 00038 _Year = Year; 00039 _Hour = Hour; 00040 _Minutes = Mins; 00041 _Seconds = Secs; 00042 _MilliSeconds = MiliSecs; 00043 _DayOfWeek = DayOfWeek; 00044 return(setTime()); 00045 } 00046 void MCP7940::StartClock(){ 00047 char cmd[8]; 00048 cmd[0] = 0x00; // The start register 00049 cmd[1] = 0x00; 00050 00051 Clock.start(); 00052 Clock.write(_addr,cmd,1) ; 00053 Clock.read(_addr, cmd, 1); 00054 00055 //Cmd 0 now contains the value of the seconds regsiter 00056 cmd[1] = cmd[0] & 0x7F; //Move the seconds register value and clear the top bit 00057 cmd[0] = 0x00; //Set write location 00058 // Clock.write(_addr,cmd,2); 00059 cmd[1] = cmd[1] | 0x80 ; //Now set the top bit 00060 Clock.write(_addr,cmd,2); //Write to Reg 0x00 the value with top bit set 00061 Clock.stop(); 00062 00063 } 00064 int MCP7940::setDefaultTime(){ 00065 _Day = 1; 00066 _Month = 10; 00067 _Year = 2015; 00068 _Hour = 1; 00069 _Minutes = 0; 00070 _Seconds = 0; 00071 _MilliSeconds = 0; 00072 _DayOfWeek = 1; 00073 return(setTime()); 00074 } 00075 int MCP7940::setTime(){ 00076 //Write time to the Device 00077 char cmd[8]; 00078 cmd[0] = 0x00; // The start register 00079 cmd[1] = IntToBCD(_Seconds); 00080 cmd[2] = IntToBCD(_Minutes); 00081 cmd[3] = IntToBCD(_Hour); 00082 cmd[4] = IntToBCD(_DayOfWeek) | 0x80; //NB this will clear the PWR Fail and set VBat Enable 00083 cmd[5] = IntToBCD(_Day); 00084 cmd[6] = IntToBCD(_Month); 00085 cmd[7] = IntToBCD(_Year - _YearStart); 00086 00087 Clock.start(); 00088 Clock.write(_addr, cmd, 8); 00089 Clock.stop(); 00090 00091 return(1); 00092 } 00093 int MCP7940::getTime(){ 00094 00095 char cmd[7]; 00096 cmd[0] = 0x00; 00097 00098 Clock.start(); 00099 //Set the position to Reg 0x00 00100 Clock.write(_addr, cmd, 1); 00101 //Read 7 bytes from 0x00 00102 Clock.read(_addr, cmd, 7); 00103 Clock.stop(); 00104 00105 //printf("%i %i %i %i %i %i %i\r\n", cmd[0], cmd[1],cmd[2],cmd[3],cmd[4], cmd[5], cmd[6]); 00106 00107 //Convert into numbers - note have to mask out the configuration bits as well 00108 _Day = BCDtoInt(cmd[4] & 0x3F); 00109 _Month = BCDtoInt(cmd[5] & 0x1F); 00110 _Year = BCDtoInt(cmd[6] & 0xFF) + _YearStart; 00111 _Hour = BCDtoInt(cmd[2] & 0x3F); //Should handle 12 or 24 hour here. 00112 _Minutes = BCDtoInt(cmd[1] & 0x7F); 00113 _Seconds = BCDtoInt(cmd[0] & 0x7F); 00114 _DayOfWeek = BCDtoInt(cmd[3] & 0x07); 00115 _MilliSeconds = 0; 00116 //printf("Day: %i Month: %i Year: %i Hour: %i Minute: %i Second: %i\r\n", _Day, _Month,_Year,_Hour,_Minutes, _Seconds); 00117 return(0); 00118 } 00119 char * MCP7940::TimeStamp(){ 00120 TimeStamp(_TimeStamp); 00121 return(_TimeStamp); 00122 } 00123 void MCP7940::TimeStamp(char * buf){ 00124 getTime(); 00125 sprintf(buf,"%04i-%02i-%02iT%02i:%02i:%02i",_Year, _Month,_Day,_Hour,_Minutes,_Seconds); 00126 } 00127 void MCP7940::niceTimeStamp(char * buf){ 00128 getTime(); 00129 sprintf(buf,"%02i/%02i/%04i %02i:%02i:%02i",_Day, _Month,_Year,_Hour,_Minutes,_Seconds); 00130 } 00131 void MCP7940::niceDate(char * buf){ 00132 getTime(); 00133 sprintf(buf,"%02i-%02i-%04i",_Day, _Month,_Year); 00134 } 00135 void MCP7940::niceTime(char * buf){ 00136 getTime(); 00137 sprintf(buf,"%02i:%02i:%02i",_Hour,_Minutes,_Seconds); 00138 } 00139 00140 int MCP7940::Day(){ 00141 return(_Day); 00142 } 00143 int MCP7940::Month(){ 00144 return(_Month); 00145 } 00146 int MCP7940::Year(){ 00147 return(_Year); 00148 } 00149 int MCP7940::Hour(){ 00150 return(_Hour); 00151 } 00152 int MCP7940::Minutes(){ 00153 return(_Minutes); 00154 00155 } 00156 int MCP7940::Seconds(){ 00157 return(_Seconds); 00158 } 00159 int MCP7940::MilliSeconds(){ 00160 return(_MilliSeconds); 00161 } 00162 int MCP7940::DayOfWeek(){ 00163 return(_DayOfWeek); 00164 } 00165
Generated on Thu Jul 21 2022 08:18:33 by
1.7.2