Go Down

Topic: ds1307 in arduino (Read 606 times) previous topic - next topic

Fightforjuly

May 07, 2013, 06:03 pm Last Edit: May 07, 2013, 06:28 pm by Fightforjuly Reason: 1
I tried to show the time and date in arduino with RTC 1307, but after I turn off the power and   turn it on  again the next day, hour and date don't update,, i'm confused why such as  ... there is sample code  I used to try:

Code: [Select]
/ This is for compatibility with both arduino 1.0 and previous versions
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif


#include <Wire.h>
#include <DS1307.h> // written by  mattt on the Arduino forum and modified by D. Sjunnesson

void setup()
{
 Serial.begin(9600);

 RTC.stop();
 RTC.set(DS1307_SEC,1);        //set the seconds
 RTC.set(DS1307_MIN,23);     //set the minutes
 RTC.set(DS1307_HR,12);       //set the hours
 RTC.set(DS1307_DOW,4);       //set the day of the week
 RTC.set(DS1307_DATE,5);       //set the date
 RTC.set(DS1307_MTH,3);        //set the month
 RTC.set(DS1307_YR,9);         //set the year
 RTC.start();

}

void loop()
{

 Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
 Serial.print(":");
 Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
 Serial.print(":");
 Serial.print(RTC.get(DS1307_SEC,false));//read seconds
 Serial.print("      ");                 // some space for a more happy life
 Serial.print(RTC.get(DS1307_DATE,false));//read date
 Serial.print("/");
 Serial.print(RTC.get(DS1307_MTH,false));//read month
 Serial.print("/");
 Serial.print(RTC.get(DS1307_YR,false)); //read year
 Serial.println();

 delay(1000);

}


please help me...

johnwasser

Are you saying that when you set the RTC to  March 5th, 2009 12:23:01 in setup() that the time gets reset to  March 5th, 2009 12:23:01 each time you turn on the power?  Isn't that what you are telling it to do?

Perhaps you should only set the RTC chip once and after that just read the time.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Nick_Pyner

Two codes

one to set

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

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

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

void setup(){
  Wire.begin();
  Serial.begin(9600);
      lcd.begin(16, 2);
      lcd.clear();
 
  setDateTime(); //MUST CONFIGURE IN FUNCTION
printDate();
Serial.println("loopstart");
}

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

void setDateTime(){

  byte second =      30; //0-59
  byte minute =      24; //0-59
  byte hour =        0; //0-23
  byte weekDay =     5; //1-7
  byte monthDay =    19; //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);


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



The other to run

Code: [Select]
//Arduino 1.0+ Only
//Arduino 1.0+ Only

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

// set the LCD address to 0x27 for a 20 chars 4 line display
LiquidCrystal_I2C lcd(0x27,20,4); 

void setup(){
  Wire.begin();
  Serial.begin(9600);
 
    lcd.init();                      // initialize the lcd

  // Print our characters on the LCD
  lcd.backlight();  //Backlight ON if under program control
  lcd.setCursor(0,0); //Start at character 3 on line 0
  // Print a message to the LCD.
  lcd.print("Today 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(12, 0);
    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(0,1);
 
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);
  lcd.print(" ");
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
    if(second <10)
  {
    lcd.print("0");
  }
  lcd.print(second);

 
}


All you need............

JimboZA

That's good thinking Nick...
Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Fightforjuly


Are you saying that when you set the RTC to  March 5th, 2009 12:23:01 in setup() that the time gets reset to  March 5th, 2009 12:23:01 each time you turn on the power?  Isn't that what you are telling it to do?

Perhaps you should only set the RTC chip once and after that just read the time.


Quote
yesterday  i'm set 12:5:38 6/5/2012 , but when I turn it on again last time being 2 hours late from the actual time and date update wasn't be 05/07/2012,, I'm confused why?

JimboZA

John's pointing out to you, that every time you run the sketch, these lines reset the time and date back to where it was originally...

Code: [Select]
RTC.stop();
  RTC.set(DS1307_SEC,1);        //set the seconds
  RTC.set(DS1307_MIN,23);     //set the minutes
  RTC.set(DS1307_HR,12);       //set the hours
  RTC.set(DS1307_DOW,4);       //set the day of the week
  RTC.set(DS1307_DATE,5);       //set the date
  RTC.set(DS1307_MTH,3);        //set the month
  RTC.set(DS1307_YR,9);         //set the year
  RTC.start();
 

Nick's suggestion is to have one sketch to set the time and date (and theoretically never have to run it again) and another sketch to use the clock.

Arduino ethernet server here.... http://jimboza.gotdns.com:8085/

No PMs for help please

Go Up