Dear all,
I am having RTC ds1307.I am facing small issue with it.
I have below code. When RTC is connected to board it read proper date and time prints along with Serial print statement.When i removed the sda and scl pins the arduino board is hanging i.e statement below print date in void loop not executing . Again once i connect back it start working. For other devices like pic,msp it reading garbage value/ default value has 0.0.0 0.0.0 along with print statements.
As per my concern. if rtc is disconnected it should print atleast the serial print data .but don’t know why it is hanging.
RTC has battery back up.
#include <Wire.h>
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
int s ;
int m ;
int h;
int w;
int dd;
int mm;
int yy;
void setup(){
Wire.begin();
Serial.begin(9600);
setDateTime(); //MUST CONFIGURE IN FUNCTION
}
void loop(){
Serial.println("Welcome to arduino main program");
getdate();
printDate();
Serial.println("read data from RTC");
delay(1000);
Serial.println("leaving the main");
}
void setDateTime(){
byte second =45; //0-59
byte minu =11; //0-59
byte hour =13; //0-23
byte weekday=2;
byte monthDay =19; //1-31
byte month =3; //1-12
byte year =13; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minu));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekday));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void getdate()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS,7);
s = bcdToDec(Wire.read());
m = bcdToDec(Wire.read());
h = bcdToDec(Wire.read() & 0b111111); //24 hour time
w=bcdToDec(Wire.read());
dd= bcdToDec(Wire.read());
mm= bcdToDec(Wire.read());
yy = bcdToDec(Wire.read());
}
void printDate(){
Serial.print(dd);
Serial.print("/");
Serial.print(mm);
Serial.print("/");
Serial.print(yy);
Serial.print(" ");
Serial.print(h);
Serial.print(":");
Serial.print(m);
Serial.print(":");
Serial.println(s);
}