Pages: [1]   Go Down
Author Topic: First time using RTC  (Read 1194 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bought some RTC from ebay, http://www.ebay.com/itm/270729898023?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2648#ht_3257wt_956 but really dont know how to use them.

Have installed Time library found on playground and played alittle with it, but cant get it to work.
Someone know a stepbystep guide or want to help me getting started with it?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13707
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


On this page - http://www.gammon.com.au/forum/?id=10896 - is an Arduino I2C port scanner which should be able to detect the DS1307. That is the first step.

Are you using IDE1.0 or 0.22?

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok. thanks;)
Will read it later today..

using 1.0
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 4324
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
On this page ... is an Arduino I2C port scanner which should be able to detect the DS1307. That is the first step.

Why would you need to detect the DS1307?  The address may not be easy to find but I thought all DS1307 chips have the same address.  The 7-bit address for the DS1307 is 1101000 which is conveniently buried on p12 of my Maxim data sheet.  Note that the leading zero is not shown so it would be expressed as 0x68.

Don
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 4324
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Have installed Time library found on playground and played alittle with it, but cant get it to work.

This is not the library that you want.  The key items to search for are DS1307 and/or RTC (real time clock).  

There's a tutorial and library over on the Adafruit website.  The hardware is set up differently and the library may not be updated for v1.0 but you should be able to get some ideas there.

By the way, that is a really interesting device.  I'm really tempted to order one.


Don

Oops:  I edited this post with the Adafruit information before I saw that you had solved your problem.  Maybe it will help someone else in the future.
« Last Edit: February 18, 2012, 02:51:13 pm by floresta » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Got it working now;)
Read this page: http://wiring.org.co/learning/libraries/realtimeclock.html
It was an example here that I learned alot from.
The device seem to work just fine
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 4324
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess my reply #3 was vindicated by the example that you found.

Code:
int clockAddress = 0x68;  // This is the I2C address

Don
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 538
Posts: 27089
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is anyone seeing the pullup resistors that are needed on the SCL/SDL and Square wave output? I don't see them in the picture.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does it need a pullup resistor?

Anyone know how I can add weeknumber to this code? :

Code:
#include <Wire.h>

int clockAddress = 0x68;  // This is the I2C address
int command = 0;  // This is the command char, in ascii form, sent from the serial port     
long previousMillis = 0;  // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers,
// Probably need to put in checks for valid numbers.
void setDateDs1307()               
{
  // Use of (byte) type casting and ascii math to achieve result. 
  second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48));
  minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  dayOfWeek = (byte) (Serial.read() - 48);
  dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.write(decToBcd(second));  // 0 to bit 7 starts the clock
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));    // If you want 12 hour am/pm you need to set
  // bit 6 (also need to change readDateDs1307)
  Wire.write(decToBcd(dayOfWeek));
  Wire.write(decToBcd(dayOfMonth));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));
  Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
  // Reset the register pointer
  Wire.beginTransmission(clockAddress);
  Wire.write(byte(0x00));
  Wire.endTransmission();

  Wire.requestFrom(clockAddress, 7);

  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(Wire.read() & 0x7f);
  minute     = bcdToDec(Wire.read());
 
  // Need to change this if 12 hour am/pm
  hour       = bcdToDec(Wire.read() & 0x3f); 
  dayOfWeek  = bcdToDec(Wire.read());
  dayOfMonth = bcdToDec(Wire.read());
  month      = bcdToDec(Wire.read());
  year       = bcdToDec(Wire.read());

  Serial.print(hour, DEC);
  Serial.print(":");
  Serial.print(minute, DEC);
  Serial.print(":");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(year, DEC);

}


void setup() {
  Wire.begin();
  Serial.begin(57600);
}

void loop() {
  if (Serial.available()) {  // Look for char in serial que and process if found
    command = Serial.read();
    if (command == 84) {      //If command = "T" Set Date
      setDateDs1307();
      getDateDs1307();
      Serial.println(" ");
    }
    else if (command == 81) {  //If command = "Q" RTC1307 Memory Functions
      delay(100);     
      if (Serial.available()) {
        command = Serial.read();
       
        // If command = "1" RTC1307 Initialize Memory - All Data will be set to 255 (0xff). 
        // Therefore 255 or 0 will be an invalid value. 
        if (command == 49) {
         
          // 255 will be the init value and 0 will be cosidered an error that
          // occurs when the RTC is in Battery mode.
          Wire.beginTransmission(clockAddress);
         
          // Set the register pointer to be just past the date/time registers.
          Wire.write(byte(0x08)); 
          for (int i = 1; i <= 27; i++) {
            Wire.write(byte(0xff));
            delay(100);
          }   
          Wire.endTransmission();
          getDateDs1307();
          Serial.println(": RTC1307 Initialized Memory");
        }
        else if (command == 50) {      //If command = "2" RTC1307 Memory Dump
          getDateDs1307();
          Serial.println(": RTC 1307 Dump Begin");
          Wire.beginTransmission(clockAddress);
          Wire.write(byte(0x00));
          Wire.endTransmission();
          Wire.requestFrom(clockAddress, 64);
          for (int i = 1; i <= 64; i++) {
            test = Wire.read();
            Serial.print(i);
            Serial.print(":");
            Serial.println(test, DEC);
          }
          Serial.println(" RTC1307 Dump end");
        }
      } 
    }
    Serial.print("Command: ");
    Serial.println(command);  // Echo command CHAR in ascii that was sent
  }

  command = 0;  // reset command                 
  delay(100);
}
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 36
Posts: 4324
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is anyone seeing the pullup resistors that are needed on the SCL/SDL and Square wave output? I don't see them in the picture.
It's probably easier to add them when you need them rather than try to remove them when you don't.  This board does not seem to have room for any jumper pins that would allow you to select whether or not to use on-board resistors.

On the other hand they are shown on the schematic as R1 and R2, so maybe they are on the back or under the crystal or even under or between the ICs.


Quote
Does it need a pullup resistor?
Yes, but only one set is needed even when more than one device is connected to the I2C bus.  If you have two devices, both of which have on-board pull-ups, then you have to remove or disconnect all but one set.

Don
« Last Edit: February 19, 2012, 10:24:58 am by floresta » Logged

Pages: [1]   Go Up
Jump to: