Pages: [1]   Go Down
Author Topic: Setting 1307 RTC to computer time during compile  (Read 650 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have been looking for a way to set the RTC time to current computer time when compiled.  I found the following code HERE. ( http://www.ladyada.net/make/logshield/rtc.html )

Code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();

  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

}

void loop () {
    DateTime now = RTC.now();
    
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
    Serial.print(" since 1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
    
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
    
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
    
    Serial.println();
    delay(3000);
}

All I receive in the serial is text similar to " Ã,FB'ÀOÿ# áƈG\B&À,þ&5Q½ˆ....." about every 5 seconds

I keep seeing the same example and cannot get it to work.  I have an UNO running V1.0.3

Additionally I was trying to change the I2C pins for the RTC as the pad for A4 came detached.  Is this possible, I tried the following with no luck.

Code:
pinMode(A4, INPUT);
pinMode(A5, OUTPUT);


Thank you in advance
« Last Edit: February 04, 2013, 09:47:43 pm by danielNC » Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't see why you would want to do that, it just costs time and space. I guess if you need to adjust the time more than twice a year and three times every ten years, you need something better than the DS1307.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I simply want my project to be set for the current date and time when the program is compiled and the RTC will maintain that time and date when project is powered down.

I know it will be off slightly due to compile time, but just trying to get close.

Dan
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The garbage in the Serial Monitor suggests to me that you have not selected 57600 as its baud rate.
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2202
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I simply want my project to be set for the current date and time when the program is compiled and the RTC will maintain that time and date when project is powered down.

I know it will be off slightly due to compile time, but just trying to get close.

Dan

The code below uses a preset time too but the time is set when the reset button is pressed - no delay.  The method you quote sounds absurd and maybe it actually works the same as this. Strip out the LCD stuff you don't need.


Code:
//Arduino 1.0+ Only
//Arduino 1.0+ Only
// pre-set the time in the void then use reset button to set it!

#include <PCD8544.h>
static PCD8544 lcd;
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

void setup(){
  Wire.begin();
  Serial.begin(9600);
 lcd.begin(84, 48);
 
  setDateTime(); //MUST CONFIGURE IN FUNCTION
}

void loop(){
      lcd.clear();
  printDate();
 
  delay(1000);
}

void setDateTime(){

  byte second =      0; //0-59
  byte minute =       36; //0-59
  byte hour =        0; //0-23
  byte weekDay =     5; //1-7
  byte monthDay =    4; //1-31
  byte month =       1; //1-12
  byte year  =       13; //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();

}

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);
}
 /*
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);
 
  lcd.setCursor(0,1);
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
    if(second <10)
  {
    lcd.print("0");
  }
  lcd.print(second);
}*/
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

These posts are confusing. You want too much.

First of all, is your serial monitor working right ?
Use a baudrate of 9600 (most common baudrate) and send some string at start. For example : Serial.println("Staring DS1307 code");

After that, write the __DATE__ and __TIME__ to the serial monitor. Do they look okay?

The most important thing: do you actually read and write to the DS1307 ?
You can use the library to see if it is running. If not, try to start it. Do the seconds change if you read it ?

After that you can finally try the full sketch.
Logged

Pages: [1]   Go Up
Jump to: