How to Request time from RTC 1307

manhoosbilli1:
Thanks for the reply. i first tried to copy and paste the code looked fine to me. but it didn't work.

What happened when you tried it? Did it compile? How exactly did it "not work"?

As it stands, I see now that I made some mistakes, most of which related to variable scope. I have (hopefully) corrected them below:

#include "Wire.h"

// variables to hold the numbers for the time and date
uint8_t ss=0x00;
uint8_t mi=0x00;
uint8_t hh=0x00;
uint8_t wd=0x06;
uint8_t dd=0x01;
uint8_t mo=0x01;
uint8_t yy=0x00;

void setup() {
  Wire.begin();
  Serial.begin(9600); // or whatever baud rate you are using
  
  /*
  
  // If you need to set the Chronodot, uncomment this section.
  Wire.beginTransmission(0x68); // address DS3231
  Wire.write(0x00); // select register
  // NOTE: if you wish to correctly set the Chronodot,
  // you *must* change the following numbers to the correct time!
  // (plus 20 seconds or so to allow for compilation, etc.)
  Wire.write(0x30); // seconds (BCD)
  Wire.write(0x43); // minutes (BCD)
  Wire.write(0x17); // hours (BCD)
  Wire.write(0x07); // day of week (I use Mon=1 .. Sun=7)
  Wire.write(0x15); // day of month (BCD)
  Wire.write(0x09); // month (BCD)
  Wire.write(0x13); // year (BCD, two digits)
  Wire.endTransmission();
  
  */
}

void loop() {
  delay(500);

  // send request to receive data starting at register 0
  Wire.beginTransmission(0x68); // 0x68 is DS3231 device address
  Wire.write((uint8_t)0); // start at register 0
  Wire.endTransmission();
  Wire.requestFrom(0x68, 7); // request seven bytes (ss, mi, hr, wd, dd, mo, yy)

  if (Wire.available() >= 7)
  {
    ss = Wire.read(); // get seconds
    mi = Wire.read(); // get minutes
    hh = Wire.read(); // get hours
    wd = Wire.read();
    dd = Wire.read();
    mo = Wire.read();
    yy = Wire.read();

    Serial.print ("\'");
    if (yy<0x10) Serial.print("0"); Serial.print(yy,HEX); Serial.print("-");
    if (mo<0x10) Serial.print("0"); Serial.print(mo,HEX); Serial.print("-");
    if (dd<0x10) Serial.print("0"); Serial.print(dd,HEX); Serial.print("(");
    switch (wd) {
      case 1: Serial.print("Mon"); break;
      case 2: Serial.print("Tue"); break;
      case 3: Serial.print("Wed"); break;
      case 4: Serial.print("Thu"); break;
      case 5: Serial.print("Fri"); break;
      case 6: Serial.print("Sat"); break;
      case 7: Serial.print("Sun"); break;
      default: Serial.print("Bad");  
    }
    Serial.print(") ");
    if (hh<0x10) Serial.print("0"); Serial.print(hh,HEX); Serial.print(":");
    if (mi<0x10) Serial.print("0"); Serial.print(mi,HEX); Serial.print(":");
    if (ss<0x10) Serial.print("0"); Serial.print(ss,HEX); Serial.println("");  
  }  
  else {
    Serial.println("Unable to read time from RTC!");
  }
}