#include "SoftwareI2C.h"
#include "rtc.h"
#include<Arduino.h>
//void RTC_write(char sec, char mnt, char hour, char day, char date, char month, char year);
SoftwareI2C Wire;
void write(char sec=0, char mnt=0, char hour=12, char day=0, char date=0, char month=0, char year=0);
int RTC_read(int addr);
char dec_to_bcd(unsigned char value);
void rtc::begin(int d,int c)
{
Serial.begin(9600);
Wire.begin(d,c);
//RTC_write(0,59,2,6,9,5,21);
}
int rtc::hours(){
int h;
if(RTC_read(0x02)==0){h=12;}
else if(RTC_read(0x02)>12){
h=RTC_read(0x02)-12;
}
else if(RTC_read(0x02)<=12&&RTC_read(0x02)>0)
{
h=RTC_read(0x02);
}
return h;
}
int rtc::minutes()
{
return RTC_read(0x01);
}
int rtc::seconds()
{
return RTC_read(0x00);
}
void rtc::write(char sec, char mnt, char hour, char day, char date, char month, char year)
{
Wire.beginTransmission(0x68);
Wire.write(0x00);
Wire.write(dec_to_bcd(sec));
Wire.write(dec_to_bcd(mnt));
Wire.write(dec_to_bcd(hour));
Wire.write(dec_to_bcd(day));
Wire.write(dec_to_bcd(date));
Wire.write(dec_to_bcd(month));
Wire.write(dec_to_bcd(year));
Wire.endTransmission();
}
char rtc::dec_to_bcd(unsigned char value)
{
unsigned char msb,lsb,hex;
msb=value/10;
lsb=value%10;
hex=((msb<<4)+lsb);
return hex;
}
int data;
int rtc::RTC_read(int addr)
{
Wire.beginTransmission(0x68);
Wire.write(addr);
Wire.endTransmission();
delay(5);
Wire.requestFrom(0x68,1);
if(Wire.available())
{
data=Wire.read();
data=((data>>4)*10)+(data&0x0f);
return data;
}
}