ds1904 readout format


I got a ds1905 RTC ibutton from dallas(thanks to maxim-ic samples)

I got some samplecode working from this forum(will post link later)
I did a couple of hours of troubleshooting before reading the section about pull up resistor from the datasheet :stuck_out_tongue:

Now its working as it should, and i get a readout when i call the routines.

the problem is just that i get the time in a slightly unexpected format.
Its in hex, from 00 to FF, and i dont really know how to convert this to regular timeformat.
I would expect that data[1] from the code would contain the seconds byte, rolling over before reaching 0x3C(dec:60) but i continues.

Can anyone point me to a solution that outputs the time in regular hh:mm:ss format?

The link is http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248727122/9#9

Read the REAL-TIME CLOCK section on page 4 of the datasheet.

The real-time clock is a 32-bit binary counter. It is incremented once per second.

Your best bet would be to take whats received from that RTC and throw the 4 bytes into a uint32_t
Here is what I’m using for my NTP clock.

uint32_t timestamp;
timestamp = packet_buffer[47];
timestamp = timestamp << 8;
timestamp |= packet_buffer[48];
timestamp = timestamp << 8;
timestamp |= packet_buffer[49];
timestamp = timestamp << 8;
timestamp |= packet_buffer[50];

seconds = timestamp%60;
minutes = (timestamp%3600)/60;
hour = (timestamp%86400)/3600;

With the exception of the battery backup being inside the package, thats the ideal RTC for my NTP clock. What made you decide on an iButton RTC?

Ill look into that code later, it looks like it makes sense :)

1:formfactor - it looks like a battery, and is easy to place. 2:availabillity - i need only one for my little project, samples from maxim 3:new to me - i wanted to see how it worked, i could just as well have used a ds1307 or likewise for my project 4:internal battery backup - i dont need to add any extra components to make it 24/7 usable.

but mostly nr 3 :)

i suspect the final project will use a ds1307 on i2c, but the ibutton is a cool concept imho

thats the ideal RTC for my NTP clock…

@KyleK, I don’t want to hijack the thread but I am curious why you wanted an RTC for an Arduino connected to NTP?

There is an example NTP clock in the [u]playground[/u] that uses the arduino crystal to maintain time if there are periods when the network is not available.

@mem, I plan to use an RTC that supports battery backup, and then if getting time from NTP fails just after the clock turns on, i'll be able to read in a time from the RTC and then go from there.

I tried implimenting the code you suggested:

#include <OneWire.h>

// DS1904 Real Time Clock iButton I/O
OneWire ds(10);  // on pin 10

void setup(void) {

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");
      delay(500);  // for readability

  present = ds.reset();
  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];

But the output is something like


Im pretty sure i dont get how these RTCs are working, shouldnt the code output the timestamp, incrementing it every second?

erhm, forget what i wrote.

Im pretty sure i forgot what LSB and MSB meant for a bit and switched it around....

Now it looks like the right output :)