Pages: [1]   Go Down
Author Topic: Realtime clock running not correct  (Read 704 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi every body ! i found a source code to use RTC DS1307 and display time on LCD1602. When i upload code to arduino. The time on LCD is correct. But when i unplug USB cable and then plug it to computer again. the time is not correct. Please help me correct it ! Thanks
here is my code :

*
Quote
by Leocarbon @leocrbn@gmail.com
* @http://twitter.com/mangodaniel
* @http://leocarbon.oa.to
* @http://electronicmango.blogspot.com
* @http://youtube.com/newelectricmango
* @http://youtube.com/leocrbn
*
*Verison History (last updated 12.24.12)
* 1.0.0.0.r2 (compile 0145)
*
* 1.0.0.0.r2 (12.24.12)
* - Uses Adafruit's RTClib @https://github.com/adafruit/RTClib
* - Updates DS1307's time each and every upload
* - Backlight
* - LCD compatible
* - Eye-soothing 30 fps (33.33 millisecond frame delay)
* - Friendly code commenting lol
* - Thanks to Adafruit & Ladyada
*
*
*
* RTC is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
*/
#include <Wire.h>
#include <LiquidCrystal.h>
#include <RTClib.h>
 
RTC_Millis RTC; //New DS1307 RTC

LiquidCrystal LCD(3,4,5,6,7,8); //New Liquid Crystal Display
int backlightOut = 9; //LCD backlight pin
int backlightIn = 2; //
unsigned long backlightTiming; //backlight ON timing (millis())
byte backlightState;

void setup(){
  RTC.begin(DateTime(__DATE__,__TIME__)); //Start RTC
  Wire.begin(); //Start Serial
  LCD.begin(2,16); //Start LCD
  LCD.clear(); //Clear LCD just in case
  pinMode(backlightOut,OUTPUT); //LCD backlight
  //pinMode(backlightIn,INPUT_PULLUP); //LCD backlight trigger switch signal input pin
  //digitalWrite(backlightOut,HIGH); //Turn on backlight on start
}

void loop(){
  backlightTiming = millis(); //Backlight timing
  backlightState = digitalRead(backlightIn); // Read the LCD backlight trigger switch signal input pin
  if(backlightTiming >= 3000){
    digitalWrite(backlightOut,LOW); //Turn LCD backlight off if 3 seconds (3000 milliseconds) has passed since the start of the code
  }
  if(backlightState == 1){
    digitalWrite(backlightOut,HIGH); //Turn LCD backlight on on tilt (for tilt switches (poorman's gyroscope))
  }
  DateTime now = RTC.now(); //update the DS1307's time (you should do this every year; at least)
  //Time - Screen position filtering
  if (now.month() < 10 && now.day() < 10){
    LCD.setCursor(4,0);
  }
  else {
    LCD.setCursor(3,0);
  }
  LCD.print(now.day());
  LCD.print("/");
  LCD.print(now.month());
  LCD.print("/");
  LCD.print(now.year());
  LCD.print(" ");
  //Time - Screen position filtering
  if (now.hour() < 10 && now. minute() < 10){
    LCD.setCursor(5,1);
  }
  else if (now.hour() < 10 && now.second() < 10){
    LCD.setCursor(5,1);
  }
  else if (now.second() < 10 && now. minute() < 10){
    LCD.setCursor(5,1);
  }
  else if (now.hour() < 10 && now. minute() < 10 && now.second() < 10){
    LCD.setCursor(5,1);
  }
  else {
    LCD.setCursor(4,1);
  }
  LCD.print(now.hour());
  LCD.print(':');
  LCD.print(now.minute());
  LCD.print(':');
  LCD.print(now.second());
  delay(33.33); //30 fps
  LCD.clear(); //update screen
}
« Last Edit: April 05, 2013, 09:20:28 pm by vudiepdh1 » Logged

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

Every time you power up the Arduino, you reset the clock. Why?
Logged

Denmark
Offline Offline
God Member
*****
Karma: 15
Posts: 654
Please don't send questions as PM!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How is the RTC chip able to keep time when you haven't included a battery ?

Refer to the datasheet for the RTC to see how to connect up a battery.

// Per.
Logged

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

Yes , i have battery 3V for DS1307, but i think when the power off, battery should be backup time . is that right ?
Logged

Denmark
Offline Offline
God Member
*****
Karma: 15
Posts: 654
Please don't send questions as PM!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Check the connections for the battery.

Also, check that the sketch does not set the time at power on.

// Per.
Logged

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

Quote
Also, check that the sketch does not set the time at power on.
See reply #1. The sketch DOES set the time every time.
Logged

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

I think arduino didn't write time parameter to ds1307 !  smiley-confuse
Logged

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

I think arduino didn't write time parameter to ds1307 !  smiley-confuse

Who would know?  Your battery could be dead but I think the programme is junk. A comment lines says it updates the DS1307 each and every upload and, if that is the case, it seems you might as well look at your watch and use the millis counter.

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


#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(){

  byte second =      0; //0-59
  byte minute =      59; //0-59
  byte hour =        11; //0-23
  byte weekDay =     2; //1-7 1=Monday
  byte monthDay =    9; //1-31
  byte month =       4; //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);
}

And here is a clock that works, use your own LCD pin setup

Code:
//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include <LiquidCrystal.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68

LiquidCrystal lcd(8,9,16,5,6,7);

void setup(){
  Wire.begin();
  Serial.begin(9600);
      lcd.begin(16, 2);
      lcd.clear();
  // Print a message to the LCD.
  lcd.print("It is");
}

void loop(){
  printDate();
  delay(1000);

}

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);

  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 weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());


 lcd.setCursor(2, 1);
    switch (weekDay)                      // Friendly printout the weekday
  {
    case 1:
      lcd.print("MON  ");
      Serial.print("MON  ");
      break;
    case 2:
      lcd.print("TUE  ");
      Serial.print("TUE  ");
      break;
    case 3:
      lcd.print("WED  ");
      Serial.print("WED  ");
      break;
    case 4:
      lcd.print("THU  ");
      Serial.print("THU  ");
      break;
    case 5:
      lcd.print("FRI  ");
      Serial.print("FRI  ");
      break;
    case 6:
      lcd.print("SAT  ");
      Serial.print("SAT  ");
      break;
    case 7:
      lcd.print("SUN  ");
       Serial.print("SUN  ");
      break;
  }

  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);
 
   lcd.setCursor(8,1);
 
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);

  lcd.setCursor(8,0);
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
  lcd.print(second);
  lcd.println("   ");
  }
Logged

Pages: [1]   Go Up
Jump to: