Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] ds1307 set time help

Status
Not open for further replies.

johnny78

Full Member level 5
Full Member level 5
Joined
Jun 28, 2017
Messages
270
Helped
1
Reputation
2
Reaction score
5
Trophy points
1,298
Visit site
Activity points
3,464
hi Guys

im trying to set time manualy using Ds1307 Module & Atmega328P working on Arduino IDE
the set time example is too easy it gets the time from the pc & updates the rtc time
i want to set the time manually But i couldnt find how to do that
im using the original ds1307 library which i couldnt fine a way to set time manually

here is the section of code set the time:

Code:
void setup() {
  bool parse=false;
  bool config=false;

  // get the date and time the compiler was run
  if (getDate(__DATE__) && getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }
would you please help with this ?

thanks in advance
 

Hi,

manually? What does that mean?
* two pushbuttons controlling SCL and SDA?
* 4 buttons using microcontroller for UP, DOWN, ENTER, MENU
* using a rotary encoder
* using a touch screen
* ???
* do you have a display? If yes, which one and how is it controlled?


What help do you need?
* doing internet search for you
* help with hardware
* help with writing code
* ???

I guess you need to explain.

Klaus
 

I think you should look at what the "tm" means as a parameter in "RTC.write(tm)".
It is a structure containing each element of the time and date. You have to fill the elements yourself with your own numbers then call the function. The getTime and getDate do just that, inserting the current date and time into the structure.

For example "tm_hour = 12;" would take a value between 0 and 23 and set the hour entry in the structure to 12 (mid day). If you then used "RTC.write(tm);" it would set the DS1307 hour to 12. Obviously, fill in the other parts of the tm structure first as they will also be written into the DS1307.

Brian.
 

I think you should look at what the "tm" means as a parameter in "RTC.write(tm)".
It is a structure containing each element of the time and date. You have to fill the elements yourself with your own numbers then call the function. The getTime and getDate do just that, inserting the current date and time into the structure.

For example "tm_hour = 12;" would take a value between 0 and 23 and set the hour entry in the structure to 12 (mid day). If you then used "RTC.write(tm);" it would set the DS1307 hour to 12. Obviously, fill in the other parts of the tm structure first as they will also be written into the DS1307.

Brian.
would you please help me to understand this code?
Code:
tmElements_t tm;

void setup() {
  bool parse = false;
  bool config = false;
  // get the date and time the compiler was run
  if (getTime(__TIME__)) {
    parse = true;
    // and configure the RTC with this info
    if (RTC.write(tm)) {
      config = true;
    }
  }
}
void loop() {
}
bool getTime(const char *str)
{
  int Hour, Min, Sec;
  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = (12); // originally (12) was Hour
  tm.Minute = (25);// originally (25) was Minute
  return true;
}
now this code sets the time to 12:25
but i cant understand how it works really
any help will be appreciated
--- Updated ---


Hi,

manually? What does that mean?
* two pushbuttons controlling SCL and SDA?
* 4 buttons using microcontroller for UP, DOWN, ENTER, MENU
* using a rotary encoder
* using a touch screen
* ???
* do you have a display? If yes, which one and how is it controlled?


What help do you need?
* doing internet search for you
* help with hardware
* help with writing code
* ???

I guess you need to explain.

Klaus
thanks Klaus
from my first post i think its clear from the code that im not understanding how the code works
whatever the way to set the time is
anyway i like your answers & its always useful

thanks
Johnny
 
Last edited:

The confusing point is that you give the impression of understanding how the regular set time example works. Presume it uses the same RTC.write() functions?
 

The confusing point is that you give the impression of understanding how the regular set time example works. Presume it uses the same RTC.write() functions?
unfortunately no

in arabic they say (let others say im rich better than say im poor even if im)

if i understand already how it works i wouldnt post for help here Sir
 

Code:
       Code:   
  
       tmElements_t tm;

void setup() {
bool parse = false; // not sure what this does - used elsewhere in the program
bool config = false;  // not sure what this does - used elsewhere in the program
  // get the date and time the compiler was run
if (getTime(__TIME__)) { // __TIME__ is a complier variable that holds the present computer time
    parse = true;
    // and configure the RTC with this info
if (RTC.write(tm)) { // this writes the compiler time into the RTC
      config = true;
    }
  }
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec; // these are local variables to hold the time numbers
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; // reads the string 'str' passed to the function and extracts values from it
tm.Hour = (12); // originally (12) was Hour  // would have loaded the Hour from the line above.
tm.Minute = (25);// originally (25) was Minute // would have loaded the Minute from two lines above.
return true;  // returns 'OK', if the wrong number of parameters had been passed in 'str', false would already have been returned.
}
 

Code:
       Code: 
 
       tmElements_t tm;

void setup() {
bool parse = false; // not sure what this does - used elsewhere in the program
bool config = false;  // not sure what this does - used elsewhere in the program
  // get the date and time the compiler was run
if (getTime(__TIME__)) { // __TIME__ is a complier variable that holds the present computer time
    parse = true;
    // and configure the RTC with this info
if (RTC.write(tm)) { // this writes the compiler time into the RTC
      config = true;
    }
  }
}
void loop() {
}
bool getTime(const char *str)
{
int Hour, Min, Sec; // these are local variables to hold the time numbers
if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false; // reads the string 'str' passed to the function and extracts values from it
tm.Hour = (12); // originally (12) was Hour  // would have loaded the Hour from the line above.
tm.Minute = (25);// originally (25) was Minute // would have loaded the Minute from two lines above.
return true;  // returns 'OK', if the wrong number of parameters had been passed in 'str', false would already have been returned.
}
hi
thanks for your help
still not undersanding how it works & i have tried to find more documentations about the library but no nothing found & no conact for the author to ask help
Code:
  if (getTime(__TIME__)) {
    parse = true;
this part of code is something essential even without using the computer time
as a simple code how can you split it into 2 functions:
1: start set time
2: save settings

when using this section of code in one function it gives error in the compiler
Code:
          bool getTime(const char *str);
          bool parse = false;
          bool config = false;

          if (getTime(__TIME__)) {
            parse = true;
            // and configure the RTC with this info
            if (RTC.write(tm)) {
              config = true;
            }
          }
& when i keep the declaration out of the function it gives me error that str & parse are not declared

thanks for help
 
Last edited:

Everything centers on the structure tm. It is an array of numbers designed to hold the date and time. Almost all C compilers will include it as standard or in a library called "time.h". You can make copies of the tm structure for your own use it you want to.

"getTime(<the time goes here>)" is a function that gets the time passed to it and breaks it down into the component parts of seconds, minutes and hours.It then stores them in the tm structure. It doesn't do anything to the DS1307 itself, it just organizes the time values into a standard structure. "__TIME__" is another standard feature of C compilers, it is just a snapshot of the current time at the compilers computer. It only copies the time of compilation, it isn't a running clock so the value can be used to show when a program was compiled for example.

There is an equivalent "__DATE__" that holds the date of compilation and can be broken down with the "getDate()" function.

You can provide your own time and date instead of the __TIME__ and __DATE__ values of course but those are ones ready made by the compiler. Whichever way you do it, the tm structure will contain the numbers for each element of the time and date but it is still just that, a structure in memory. The "RTC.write(tm)" function does the electrical part, it takes each of the elements in the tm structure and writes them to the registers inside the DS1307. This may seem a convoluted way to do it but remember that DS1307 is only one of many types of clock IC and each has their own register assignments so going through the "tm" structure first means each IC's library function has a common place to get its data from. The "tm" structure is also used in many other situations where time and dates are used.

The reverse function "RTC.read(tm)" reads the registers of the DS1307 and puts the values into the "tm" structure where you can then use them as needed.

The contents of the tm structure are documented here: https://cplusplus.com/reference/ctime/tm/

Brian.
 

I did this for an OLED clock based on DS18B20. I added 3 push buttons. One for stopping the clock, another 2 for increment and decrement. I will share the code if I can find.
 

Hi,

you should be nominated for the award for asking the most unelaborated questions.

Think about this:
* you see some posts
* we see some posts
* we see only "our" posts, so we don´t kow what you see.

For sure we all do see "some latest post". No one knows whether we see the same "latest post".

Thus I guess it should be a good idea to tell
* which "latest post you see" and
* which post you miss

******
What´s the highest number you can think of? Then add 1 to it.

Klaus
 

why i cant see the latest post ?
Are you asking about a recent post of yours? No post was deleted in this thread. If you are missing a post, it got possibly lost due to server issues. This happens very rarely but can happen.

As KlausST mentioned, please give more info, or simply repost.
 

Are you asking about a recent post of yours? No post was deleted in this thread. If you are missing a post, it got possibly lost due to server issues. This happens very rarely but can happen.

As KlausST mentioned, please give more info, or simply repost.
sorry but in this thread last time i checked it Mr betwixt has replied in my post but i couldnt read it
& today i was free to check it but i couldnt find the latest reply'
that what im asking for
--- Updated ---

Hi,

you should be nominated for the award for asking the most unelaborated questions.

Think about this:
* you see some posts
* we see some posts
* we see only "our" posts, so we don´t kow what you see.

For sure we all do see "some latest post". No one knows whether we see the same "latest post".

Thus I guess it should be a good idea to tell
* which "latest post you see" and
* which post you miss

******
What´s the highest number you can think of? Then add 1 to it.

Klaus
ok Mr im sorry but as i guess i posted in the same thread asking for lost post
maybe its my mistake i must ask for the last reply on the same post

anyway im interested to have that award please
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top