DS1904 RTC - How to Convert Binary Time into Real Time/Date?


Closed Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807

    Default DS1904 RTC - How to Convert Binary Time into Real Time/Date?

    Hi. If anyone used the DS1904 please reply with the algorithm to convert from 32 bit binary counter to a real Date/Time data.

    Maxim does not have an available App. Note freely to all. Need first to be registered/approoved and maybe then one will get the App.Note...

    I applied but do not know if I am good enough to get the data.

    Ioannis

  2. #2
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    Don't really have an answer, but to help with info for everyone else. The 32 bit data represents number of seconds since reset and turned on. So if you started it on Jan1, 2010 at midnight, the counter would be 86,400 on Jan2 at midnight.

    So divide the answer by 86400 for number of days
    divide the remainder by 3600 for number of hours
    divide the remainder from that by 60 for number of min
    remainder is seconds

    now getting the date is another story. I have no idea how to account for 28,30 and 31 days in the months to be able to convert the number of days into an actual date
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Thanks for the reply.

    But I do not think it is counting from power on. It has internal battery and a way to set the date I suppose.

    If the device is read by the dev.kit of the Dallas-Maxim, it shows the correct time, date, year etc. It can even be synchronised with the PC's current time! See picture.

    Name:  ds1904.png
Views: 1931
Size:  49.3 KB

    Ioannis

  4. #4
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default Sorry

    I should have been more clear. I ment to say the 32 bit number represents the number of seconds since you sent it the command to reset the count, AND turn the OSC on. This thing looks to be a 32 bit counter, with each bit representing 1 second. I am not sure if it can be pre-set, or if you just have to know what time it is when you turn the OSC on.

    EDIT: Went back and read the datasheet again, yes you can write current time data to it. The most important thing is to realize it is all referenced to some known time. It just gives the number of seconds since then. I don't understand how it can be "syncronized" with the PC clock, this implies both use a reference system and they are the same
    Last edited by cncmachineguy; - 27th November 2010 at 15:39.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Hi Bert.

    The point that confussed me more was that the chip was not set by me. It was set by Maxim or another person far far away from me in a different time zone, before winter time.

    As you can see by the attached picture above, it has 0-1 seconds difference from my PC! Amazing. There must be a trick.

    I found this C program but could not understand what really does.

    Code:
    void loop(void) {
      byte i;
      byte present = 0;
      byte data[8];
      byte addr[8];
      uint32_t timestamp;
      int seconds = 0;
    
      if ( !ds.search(addr)) {
    	//Serial.print("No more addresses found.\n");
    	ds.reset_search();
    	delay(500);  // for readability
    	return;
      }
    
      present = ds.reset();
      ds.select(addr);
      ds.write(0x66,1);   // read RTC
      for ( i = 0; i < 5; i++) {
        data[i] = ds.read();
      }
    	timestamp = data[1];
    	timestamp = timestamp << 8;
    	timestamp |= data[2];
    	timestamp = timestamp << 8;
    	timestamp |= data[3];
    	timestamp = timestamp << 8;
    	timestamp |= data[4];
    Serial.println(timestamp);
    delay(1000);
    }
    Ioannis

  6. #6
    Join Date
    Aug 2010
    Location
    Maryland, USA
    Posts
    869


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ioannis View Post
    Hi Bert.

    The point that confussed me more was that the chip was not set by me. It was set by Maxim or another person far far away from me in a different time zone, before winter time.

    As you can see by the attached picture above, it has 0-1 seconds difference from my PC! Amazing. There must be a trick.
    That is quite incredible! I dont get how the maxim reader could know when the timer was started

    As for the code snippit, I don't know C, but it appears to just read the data into an array, and display it.
    -Bert

    The glass is not half full or half empty, Its twice as big as needed for the job!

    http://foamcasualty.com/ - Warbird R/C scratch building with foam!

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Yes it uses an array and does some oring and shifting.

    I really have not quite understood the logic behind it.

    A couple of days say Maxim for the AN to be approoved.

    Will see...

    Ioannis

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Update from Maxim.

    Well, finally the counter is pure binary and user have to set a referene time stamp.

    So far OK. But how on earth can one calculate the date, months, leap years etc?

    If anyone interested in the details of the AN from Maxim, PM. Code examples are in assembly though.

    Ioannis

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts