MAXIM DS3231 accurate Real Time Clock Library

Dependents:   20180621_FT813

Fork of DS3231 by remi cormier

Committer:
cromda
Date:
Sat Dec 22 19:23:19 2012 +0000
Revision:
0:11dfd7ac4188
Child:
1:84ccc64067d8
MAXIM DS3231 RTC

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cromda 0:11dfd7ac4188 1 #include "DS3231.h"
cromda 0:11dfd7ac4188 2
cromda 0:11dfd7ac4188 3 DS3231::DS3231(PinName sda, PinName scl) : i2c(sda, scl)
cromda 0:11dfd7ac4188 4 {
cromda 0:11dfd7ac4188 5 }
cromda 0:11dfd7ac4188 6
cromda 0:11dfd7ac4188 7
cromda 0:11dfd7ac4188 8 // BCD to decimal conversion
cromda 0:11dfd7ac4188 9 int DS3231::bcd2dec(int k)
cromda 0:11dfd7ac4188 10 {return(((k&0xF0)>>4)*10+(k&0x0F));}
cromda 0:11dfd7ac4188 11
cromda 0:11dfd7ac4188 12
cromda 0:11dfd7ac4188 13 // decimal to BCD conversion
cromda 0:11dfd7ac4188 14 int DS3231::dec2bcd(int k)
cromda 0:11dfd7ac4188 15 {return((k/10)*16+(k%10));}
cromda 0:11dfd7ac4188 16
cromda 0:11dfd7ac4188 17 void DS3231::setI2Cfrequency(int frequency)
cromda 0:11dfd7ac4188 18 {i2c.frequency(frequency);
cromda 0:11dfd7ac4188 19 }
cromda 0:11dfd7ac4188 20
cromda 0:11dfd7ac4188 21 // read temperature from DS3231
cromda 0:11dfd7ac4188 22 float DS3231::readTemp()
cromda 0:11dfd7ac4188 23 {char buf[2];
cromda 0:11dfd7ac4188 24 buf[0]=DS3231_MSB_Temp;
cromda 0:11dfd7ac4188 25 int w=i2c.write(DS3231_Address,buf,1);
cromda 0:11dfd7ac4188 26 int r=i2c.read(DS3231_Address,buf,2);
cromda 0:11dfd7ac4188 27 error=((w!=0)||(r!=0));
cromda 0:11dfd7ac4188 28 return(buf[0]+buf[1]/256.0);}
cromda 0:11dfd7ac4188 29
cromda 0:11dfd7ac4188 30 // decode time registers
cromda 0:11dfd7ac4188 31 void DS3231::decodeTime(int regHours, int regMinutes, int regSeconds,int *Hours, int *Minutes, int *Seconds)
cromda 0:11dfd7ac4188 32 {*Hours=bcd2dec(regHours);
cromda 0:11dfd7ac4188 33 *Minutes=bcd2dec(regMinutes);
cromda 0:11dfd7ac4188 34 *Seconds=bcd2dec(regSeconds);
cromda 0:11dfd7ac4188 35 }
cromda 0:11dfd7ac4188 36
cromda 0:11dfd7ac4188 37
cromda 0:11dfd7ac4188 38 // read time registers dand decode them to decimal
cromda 0:11dfd7ac4188 39 void DS3231::readTime(int *hours, int *minutes, int *seconds)
cromda 0:11dfd7ac4188 40 {char buf[3];
cromda 0:11dfd7ac4188 41 buf[0]=DS3231_Seconds;
cromda 0:11dfd7ac4188 42 int w=i2c.write(DS3231_Address,buf,1);
cromda 0:11dfd7ac4188 43 int r=i2c.read(DS3231_Address,buf,3);
cromda 0:11dfd7ac4188 44 decodeTime(buf[2],buf[1],buf[0],hours,minutes,seconds);
cromda 0:11dfd7ac4188 45 error=((w!=0)||(r!=0));
cromda 0:11dfd7ac4188 46 }
cromda 0:11dfd7ac4188 47
cromda 0:11dfd7ac4188 48 // set time register
cromda 0:11dfd7ac4188 49 void DS3231::setTime(int hours, int minutes, int seconds)
cromda 0:11dfd7ac4188 50 {writeRegister(DS3231_Seconds,dec2bcd(seconds));
cromda 0:11dfd7ac4188 51 writeRegister(DS3231_Minutes,dec2bcd(minutes));
cromda 0:11dfd7ac4188 52 writeRegister(DS3231_Hours,dec2bcd(hours));
cromda 0:11dfd7ac4188 53 }
cromda 0:11dfd7ac4188 54
cromda 0:11dfd7ac4188 55 // decode date registers
cromda 0:11dfd7ac4188 56 void DS3231::decodeDate(int regDate,int regMonth, int regYear, int *date, int *month, int *year)
cromda 0:11dfd7ac4188 57 {*date=bcd2dec(regDate);
cromda 0:11dfd7ac4188 58 int century=regMonth&128;
cromda 0:11dfd7ac4188 59 *month=bcd2dec(regMonth&127);
cromda 0:11dfd7ac4188 60 *year=bcd2dec(regYear);
cromda 0:11dfd7ac4188 61 if (century==0) // only XXth and XXIst centuries allowed
cromda 0:11dfd7ac4188 62 {*year=*year+1900;}
cromda 0:11dfd7ac4188 63 else
cromda 0:11dfd7ac4188 64 {*year=*year+2000;}
cromda 0:11dfd7ac4188 65 }
cromda 0:11dfd7ac4188 66
cromda 0:11dfd7ac4188 67 // read date registers
cromda 0:11dfd7ac4188 68 void DS3231::readDate(int *date, int *month, int *year)
cromda 0:11dfd7ac4188 69 {char buf[3];
cromda 0:11dfd7ac4188 70 buf[0]=DS3231_Date;
cromda 0:11dfd7ac4188 71 int w=i2c.write(DS3231_Address,buf,1);
cromda 0:11dfd7ac4188 72 int r=i2c.read(DS3231_Address,buf,3);
cromda 0:11dfd7ac4188 73 decodeDate(buf[0],buf[1],buf[2],date,month,year);
cromda 0:11dfd7ac4188 74 error=((w!=0)||(r!=0));
cromda 0:11dfd7ac4188 75 }
cromda 0:11dfd7ac4188 76
cromda 0:11dfd7ac4188 77 // set the date registers
cromda 0:11dfd7ac4188 78 void DS3231::setDate(int dayOfWeek, int date, int month, int year)
cromda 0:11dfd7ac4188 79 {writeRegister(DS3231_Date,dec2bcd(date));
cromda 0:11dfd7ac4188 80 writeRegister(DS3231_Day,dayOfWeek);
cromda 0:11dfd7ac4188 81 int century=0; // only XXth and XXIst centuries allowed
cromda 0:11dfd7ac4188 82 if (year<1900){year=1900;}
cromda 0:11dfd7ac4188 83 if (year>2099){year=2099;}
cromda 0:11dfd7ac4188 84 if (year<2000)
cromda 0:11dfd7ac4188 85 {year=year-1900;}
cromda 0:11dfd7ac4188 86 else
cromda 0:11dfd7ac4188 87 {year=year-2000;century=1;}
cromda 0:11dfd7ac4188 88 writeRegister(DS3231_Month_Century,dec2bcd(month)+128*century);
cromda 0:11dfd7ac4188 89 writeRegister(DS3231_Year,dec2bcd(year));
cromda 0:11dfd7ac4188 90 }
cromda 0:11dfd7ac4188 91
cromda 0:11dfd7ac4188 92 // read date and time registers
cromda 0:11dfd7ac4188 93 void DS3231::readDateTime(int *dayOfWeek, int *date, int *month, int *year, int *hours, int *minutes, int *seconds)
cromda 0:11dfd7ac4188 94 {char buf[7];
cromda 0:11dfd7ac4188 95 buf[0]=DS3231_Seconds;
cromda 0:11dfd7ac4188 96 int w=i2c.write(DS3231_Address,buf,1);
cromda 0:11dfd7ac4188 97 int r=i2c.read(DS3231_Address,buf,7);
cromda 0:11dfd7ac4188 98 decodeTime(buf[2],buf[1],buf[0],hours,minutes,seconds);
cromda 0:11dfd7ac4188 99 *dayOfWeek=buf[3];
cromda 0:11dfd7ac4188 100 decodeDate(buf[4],buf[5],buf[6],date,month,year);
cromda 0:11dfd7ac4188 101 error=((w!=0)||(r!=0));
cromda 0:11dfd7ac4188 102 }
cromda 0:11dfd7ac4188 103
cromda 0:11dfd7ac4188 104 // read a register
cromda 0:11dfd7ac4188 105 int DS3231::readRegister(char reg)
cromda 0:11dfd7ac4188 106 {char buf[1];
cromda 0:11dfd7ac4188 107 buf[0]=reg;
cromda 0:11dfd7ac4188 108 int w=i2c.write(DS3231_Address,buf,1);
cromda 0:11dfd7ac4188 109 int r=i2c.read(DS3231_Address,buf,1);
cromda 0:11dfd7ac4188 110 error=((w!=0)||(r!=0));
cromda 0:11dfd7ac4188 111 return(buf[0]);
cromda 0:11dfd7ac4188 112 }
cromda 0:11dfd7ac4188 113
cromda 0:11dfd7ac4188 114 // write a register
cromda 0:11dfd7ac4188 115 void DS3231::writeRegister(int reg,char byte)
cromda 0:11dfd7ac4188 116 {char buf[2];
cromda 0:11dfd7ac4188 117 buf[0]=reg;
cromda 0:11dfd7ac4188 118 buf[1]=byte;
cromda 0:11dfd7ac4188 119 int w=i2c.write(DS3231_Address,buf,2);
cromda 0:11dfd7ac4188 120 error=(w!=0);
cromda 0:11dfd7ac4188 121 }
cromda 0:11dfd7ac4188 122
cromda 0:11dfd7ac4188 123 void DS3231::eraseOSF()
cromda 0:11dfd7ac4188 124 {int reg=readRegister(DS3231_Control_Status);
cromda 0:11dfd7ac4188 125 reg=reg&0x7F;
cromda 0:11dfd7ac4188 126 writeRegister(DS3231_Control_Status,reg);
cromda 0:11dfd7ac4188 127 }
cromda 0:11dfd7ac4188 128
cromda 0:11dfd7ac4188 129 bool DS3231::OSF()
cromda 0:11dfd7ac4188 130 {int reg=readRegister(DS3231_Control_Status);
cromda 0:11dfd7ac4188 131 return(reg&DS3231_bit_OSF);
cromda 0:11dfd7ac4188 132 }