Hello,
I try to read the time and date from my RTC Module for arduino, but I have some issue with doing that.
I'm working with MEGA-2560, so my SDA and SCL pins are (20) and (21).
I'm working with this article: Search Results for “RTC” – bildr.org
I don't understant why, but when I try to read from the RTC, I always get the same output at the serial monitor, pic is attached, even when the module disconnected, so I guess the RTC doesn't connected properly or worse, broken.
I would love to get some help with this issue.. maybe I just miss something.
Thanks!
My code for reading from the module is:
#include <Wire.h>
#define DS1307_ADDRESS 0x68 // This is the I2C address
byte zero = 0x00; //workaround for issue #527
void setup(){
Wire.begin();
Serial.begin(9600);
//setDateTime(); //MUST CONFIGURE IN FUNCTION
}
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 printDate()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
//print the date EG 3/1/11 23:59:59
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
void setDateTime(){
byte second = 0; //0-59
byte minute = 57; //0-59
byte hour = 15; //0-23
byte weekDay = 6; //1-7
byte monthDay = 5; //1-31
byte month = 8; //1-12
byte year = 16; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
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();
}
void loop(){
printDate();
delay(1000);
}
sketch_RTC_2.ino (1.86 KB)