0

I am using this real-time-clock module to keep track of the time for a project: http://www.dfrobot.com/wiki/index.php/Real_Time_Clock_Module_(DS1307)V1.1(SKU:DFR0151)

I have hooked up the respective pins correctly to the Arduino.

Below is the code I'm using. This first one is the code used to set the time on the module. It writes the initial time you specify to the RTC module.

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
void setup(){
 Wire.begin();
 Serial.begin(9600);
 setDateTime(); //MUST CONFIGURE IN FUNCTION
}
void loop(){
 printDate();
 delay(1000);
}
void setDateTime(){ //Set the time to a minute or two ahead. After upload has completed, simply press the reset button to start the time while watching another clock.
 byte second = 0; //0-59
 byte minute = 56; //0-59
 byte hour = 13; //0-23
 byte monthDay = 26; //1-31
 byte month = 5; //1-12
 byte year = 15; //0-99
 Wire.beginTransmission(DS1307_ADDRESS);
 Wire.write(zero); //stop Oscillator
 Wire.write(decToBcd(second));
 Wire.write(decToBcd(minute));
 Wire.write(decToBcd(hour));
 Wire.write(decToBcd(monthDay));
 Wire.write(decToBcd(month));
 Wire.write(decToBcd(year));
 Wire.write(zero); //start 
 Wire.endTransmission();
}
byte decToBcd(byte val){
return ((val/10*16)+(val%10)); //Convert normal decimal numbers to binary coded decimal
}
byte bcdToDec(byte val) {
 return ((val/16*10)+(val%16)); //Convert binary coded decimal to normal decimal numbers
}
void printDate(){
 Wire.beginTransmission(DS1307_ADDRESS); // Reset the register pointer
 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 monthDay = bcdToDec(Wire.read());
 int month = bcdToDec(Wire.read());
 int year = bcdToDec(Wire.read());
 Serial.print(month); //print the date, EG: 3/1/11 23:59:59
 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);
}
//http://projectsfromtech.blogspot.com/2013/06/arduino-rtc-tinyrtc-v1-with-arduino.html

And this next block reads the time from the RTC module. It has an onboard battery, so it keeps time even when disconnected from the Arduino.

#include "Wire.h"
#define DS1307_ADDRESS 0x68
void setup(){
 Wire.begin();
 Serial.begin(9600);
}
void loop(){
 printDate();
 delay(1000);
}
byte bcdToDec(byte val){
 return ((val/16*10)+(val%16)); //Convert binary coded decimal to normal decimal numbers
}
void printDate(){
 Wire.beginTransmission(DS1307_ADDRESS); // Reset the register pointer
 byte zero = 0x00;
 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 monthDay = bcdToDec(Wire.read());
 int month = bcdToDec(Wire.read());
 int year = bcdToDec(Wire.read());
 Serial.print(month); //print the date, EG: 3/1/11 23:59:59
 Serial.print("/");
 Serial.print(monthDay);
 Serial.print("/");
 Serial.print("20");
 Serial.print(year);
 Serial.print(" ");
 Serial.print(hour);
 Serial.print(":");
 Serial.print(minute);
 Serial.print(":");
 Serial.println(second);
}
//http://projectsfromtech.blogspot.com/2013/06/arduino-rtc-tinyrtc-v1-with-arduino.html

The weird thing is, when I open the serial monitor after I upload the getTime sketch (the second one), every thing works except that the dayOfMonth only shows the last digit. For example, if the day was the 23rd, it would only print 3. Or if it was 31, it prints 1. I've gone through the code, but can't find anything that I think would cause this. Any tips?

To get started, I followed this tutorial: http://projectsfromtech.blogspot.com/2013/06/arduino-rtc-tinyrtc-v1-with-arduino.html

asked May 27, 2015 at 9:36

1 Answer 1

3

You forgot

int weekDay = bcdToDec(Wire.read()); //0-6 -> Sunday - Saturday

just before reading monthDay. Even though you may not be interested in this piece of data, the RTC chip has no way of knowing that, and it will always send the date and time in the same format. You can replace that line by

Wire.read(); // discard day of week

if you want to make clear that you are discarding this information.

Ah, BTW, decToBcd() and bcdToDec() are ill-named: they convert between BCD and binary, not decimal.

answered May 27, 2015 at 14:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.