Dear all,
Here is simple code written . where i would like to change DS1307 time On serial request.
If Serially i send Date and time format i could able to see change RTC Time else should show Previously written time format.
There are 2 time write function One being Previously used in library to demostrate write RTC time & another i have created Where I can See RTC format
Let me How can change system based On function I have created
#include <Wire.h>
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
static int dd;
static int mm;
static int yy;
static int s;
static int h;
static int m;
static int wkDay;
//Local time date variables
static int local_day;
static int local_month;
static int local_year;
static int local_s;
static int local_h;
static int local_m;
static int CENTURY=2000;
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) );
}
int getLastTwoDigOfYear(int y)
{
return(y%100);
}
void rtcSetup(){
Wire.begin();
}
void getRTCDateTime()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
//Serial.println("RTC IS ACTIVE :\n");
s = bcdToDec(Wire.read());
m = bcdToDec(Wire.read());
h = bcdToDec(Wire.read() & 0b111111); //24 hour time
wkDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
dd = bcdToDec(Wire.read());
mm = bcdToDec(Wire.read());
yy = CENTURY + bcdToDec(Wire.read());
}
void rtcWrite(int sec1,int min1,int hour1, int wkday1,int dd1,int mm1,int yy1)
{
int year1;
rtcSetup();
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(sec1));
Wire.write(decToBcd(min1));
Wire.write(decToBcd(hour1)& 0b111111);
Wire.write(decToBcd(wkday1));
Wire.write(decToBcd(dd1));
Wire.write(decToBcd(mm1));
//Wire.write(decToBcd(yy1);
Wire.write(decToBcd(getLastTwoDigOfYear(yy1)));
//year1= Wire.write(decToBcd(getLastTwoDigOfYear(yy1)));
//Serial.print("date in rtc write:");
//Serial.println(year1);
Wire.write(zero); //start
Wire.endTransmission();
// convertUTCtoLocal();
getRTCDateTime();
}
void setDateTime() {
byte second = 45; //0-59
byte minute = 41; //0-59
byte hour = 8; //0-23
byte weekDay = 2; //1-7
byte monthDay = 16; //1-31
byte month = 7; //1-12
byte year = 14; //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(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
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(monthDay);
Serial.print("/");
Serial.print(month);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
void setup() {
// put your setup code here, to run once:
rtcSetup();
// setDateTime();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
getRTCDateTime();
printDate();
delay(1000);
}