MAXIM DS3231 accurate Real Time Clock Library

Dependents:   I2C-LCD-DHT22 wifigpslcd HomeStatus project ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS3231.cpp Source File

DS3231.cpp

00001 #include "DS3231.h"
00002 
00003 DS3231::DS3231(PinName sda, PinName scl) : i2c(sda, scl)
00004     {
00005     } 
00006     
00007     
00008 // BCD to decimal conversion    
00009 int DS3231::bcd2dec(int k)
00010     {return(((k&0xF0)>>4)*10+(k&0x0F));}
00011     
00012     
00013 // decimal to BCD conversion    
00014 int DS3231::dec2bcd(int k)
00015     {return((k/10)*16+(k%10));}      
00016     
00017 void DS3231::setI2Cfrequency(int frequency)
00018     {i2c.frequency(frequency);
00019     }
00020     
00021 // read temperature from DS3231    
00022 float DS3231::readTemp()
00023     {char buf[2];
00024      buf[0]=DS3231_MSB_Temp;
00025      int w=i2c.write(DS3231_Address,buf,1);
00026      int r=i2c.read(DS3231_Address,buf,2);
00027      error=((w!=0)||(r!=0));   
00028      return(buf[0]+buf[1]/256.0);}  
00029 
00030 // force temperatue conversion     
00031 void DS3231::convertTemperature()
00032     {int Control=readRegister(DS3231_Control)|DS3231_bit_CONV;
00033      writeRegister(DS3231_Control,Control);
00034     }
00035 
00036 // decode time registers     
00037 void DS3231::decodeTime(int regHours, int regMinutes, int regSeconds,int *Hours, int *Minutes, int *Seconds)
00038     {*Hours=bcd2dec(regHours);
00039      *Minutes=bcd2dec(regMinutes);
00040      *Seconds=bcd2dec(regSeconds);
00041     }      
00042      
00043      
00044 // read time registers dand decode them to decimal     
00045 void DS3231::readTime(int *hours, int *minutes, int *seconds)
00046     {char buf[3];
00047      buf[0]=DS3231_Seconds;
00048      int w=i2c.write(DS3231_Address,buf,1);
00049      int r=i2c.read(DS3231_Address,buf,3);
00050      decodeTime(buf[2],buf[1],buf[0],hours,minutes,seconds);    
00051      error=((w!=0)||(r!=0));
00052     }  
00053     
00054 // set time register    
00055 void DS3231::setTime(int hours, int minutes, int seconds)
00056     {writeRegister(DS3231_Seconds,dec2bcd(seconds));
00057      writeRegister(DS3231_Minutes,dec2bcd(minutes));
00058      writeRegister(DS3231_Hours,dec2bcd(hours));
00059     }    
00060     
00061 // decode date registers
00062 void DS3231::decodeDate(int regDate,int regMonth, int regYear, int *date, int *month, int *year)
00063     {*date=bcd2dec(regDate);
00064      int century=regMonth&128;
00065      *month=bcd2dec(regMonth&127);
00066      *year=bcd2dec(regYear);
00067      if (century==0)         // only XXth and XXIst centuries allowed
00068         {*year=*year+1900;}
00069      else
00070         {*year=*year+2000;}
00071     }         
00072     
00073 // read date registers    
00074 void DS3231::readDate(int *date, int *month, int *year)
00075     {char buf[3];
00076      buf[0]=DS3231_Date;
00077      int w=i2c.write(DS3231_Address,buf,1);
00078      int r=i2c.read(DS3231_Address,buf,3);
00079      decodeDate(buf[0],buf[1],buf[2],date,month,year);            
00080      error=((w!=0)||(r!=0));         
00081     }     
00082     
00083 // set the date registers
00084 void DS3231::setDate(int dayOfWeek, int date, int month, int year)
00085     {writeRegister(DS3231_Date,dec2bcd(date));
00086      writeRegister(DS3231_Day,dayOfWeek);
00087      int century=0; // only XXth and XXIst centuries allowed
00088      if (year<1900){year=1900;}
00089      if (year>2099){year=2099;}
00090      if (year<2000)
00091         {year=year-1900;}
00092      else
00093         {year=year-2000;century=1;}   
00094      writeRegister(DS3231_Month_Century,dec2bcd(month)+128*century);   
00095      writeRegister(DS3231_Year,dec2bcd(year));
00096     }     
00097     
00098 // read date and time registers    
00099 void DS3231::readDateTime(int *dayOfWeek, int *date, int *month, int *year, int *hours, int *minutes, int *seconds)
00100     {char buf[7];
00101      buf[0]=DS3231_Seconds;
00102      int w=i2c.write(DS3231_Address,buf,1);
00103      int r=i2c.read(DS3231_Address,buf,7);
00104      decodeTime(buf[2],buf[1],buf[0],hours,minutes,seconds);
00105      *dayOfWeek=buf[3];
00106      decodeDate(buf[4],buf[5],buf[6],date,month,year);
00107      error=((w!=0)||(r!=0));     
00108     }    
00109     
00110 // read a register    
00111 int DS3231::readRegister(char reg)
00112     {char buf[1];
00113      buf[0]=reg;
00114      int w=i2c.write(DS3231_Address,buf,1);
00115      int r=i2c.read(DS3231_Address,buf,1);
00116      error=((w!=0)||(r!=0));  
00117      return(buf[0]);
00118     }   
00119 
00120 // write a register    
00121 void DS3231::writeRegister(int reg,char byte)
00122     {char buf[2];
00123      buf[0]=reg;
00124      buf[1]=byte;
00125      int w=i2c.write(DS3231_Address,buf,2); 
00126      error=(w!=0);   
00127     }      
00128     
00129 void DS3231::eraseOSF()
00130     {int reg=readRegister(DS3231_Control_Status);
00131      reg=reg&0x7F;
00132      writeRegister(DS3231_Control_Status,reg);
00133     }     
00134     
00135 bool DS3231::OSF()
00136     {int reg=readRegister(DS3231_Control_Status);
00137     return(reg&DS3231_bit_OSF);
00138     }