Hello, please help me to understand, why my prog printing the day of the week all the time - "zero" . the global time and the month day is correct
The code is :
#include "Wire.h"
#include "RTClib.h"
#define DS1307_ADDRESS 0x68
RTC_DS1307 RTC; // import the I2C library and set the bus address of the RTC in the sketch as 0x68.
byte zero = 0x00; //workaround for issue #527
DateTime currentDateTime;
byte timeBuffer[8];
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());
void setup(){
Wire.begin();
Serial.begin(9600);
setDateTime(); /not be highlighted while reading the time, Here it will setdate/
Wire.begin(); // Initiate the Wire library and join the I2C bus as a master or slave. This should normally be called only once.
RTC.begin();
RTC.adjust(DateTime(DATE, TIME)); // is take the Date and Time according the computer you're using
}
void loop(){
printDate();
delay(1000);
}
void setDateTime()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
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();
}
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());
Serial.print(monthDay);
Serial.print("/");
Serial.print(month);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.print(second);
Serial.print(":");
Serial.println(weekDay); //today
}