Pages: 1 ... 3 4 [5] 6 7 8   Go Down
Author Topic: Library for DS1307 Real Time Clock  (Read 37129 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey, just wanted to share some info about how to use mattt's DS1307 library with some corrections:

1. In the main program instead of this:

Code:
include <Wire.h>
#include <DS1307.h>

use this (fixes sk_uk problem, i had it as well):

Code:
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

2. Name your .cpp and .h files as DS1307.cpp and DS1307.h and place them in the arduino-0012\hardware\libraries\DS1307\ directory
Case sensitive!

3. Day of month can not be set to the 31st
In the file DS1307.cpp change this:

Code:
case DS1307_DATE:
    if(v<31 && v>-1)
    {
      rtc_bcd[DS1307_DATE]=((v / 10)<<4) + (v % 10);
    }
    break;

To this:

Code:
case DS1307_DATE:
    if(v<32 && v>-1)
    {
      rtc_bcd[DS1307_DATE]=((v / 10)<<4) + (v % 10);
    }
    break;

4. The maximum number for year in the DS1307.cpp is specified as 12 (v<13). Since it is not far away and it is not limited by chip, use 20, 30 or whatever you like <100 (in the manual it writes: Valid Up to 2100).

Code:
case DS1307_YR:
    if(v<13 && v>-1)
    {
      rtc_bcd[DS1307_YR]=((v / 10)<<4) + (v % 10);
    }
    break;

Logged

Melbourne
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks to all the authors and editors on this thread, I've got a little card built and feeding the time into my ardinuo, all good.
Only problem is the time zone and date on the chip is wrong, and I can't quite figure out how to set the time using the code in this thread.

I'm still pretty much a voodoo user ( mixing bits of code and hardware from diagrams, hoping it will work, without fully understanding what I'm doing ) , so if the answer is obvious, I'll be pleased to learn.

Cheers

McSporran




Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy, good work!

Can someone post the libraries with all the corrections please?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

has anyone run into a problem using both the DS1307 (sparkfun rtc module) with the DS18B20 temp sensor?  Im prototyping a data logger that writes temp, light, wind speed and voltage values to an sd card running on an atmega 168.  Seems to be some interference between the wire.h and the OneWire.h libraries since I can get either to work without the other.  Is it an I2C communication problem and/or ram?  
I am using the ds18b30 code from the playground with modifications and the ds1307 from: glacialwanderer.com/hobbyrobotics.

at the moment the temp is reading and only part of the time/date working, but after much trial and error.

any suggestions would be super helpful.
thanks in advance!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am running a similar setup, where I log data from 3x DS18B20 temperature sensors to SD card and also use RTC based on DS1307 to timestamp each record. No problems so far. I use Sanguino platform, but in principle this is very similar to atmega 168.
My RTC is this one: http://www.futurlec.com.au/Mini_DS1307.jsp, however, any board based on DS1307 should work the same.
The only problem I have got working with this setup is that DS18B20 should be externally powered but not using parasitic power mode.
Here is a code where DS1307 and DS18B20 is working together with all the libraries I use:
http://kom.aau.dk/~kod/temp_time.rar

And here is only the program:
Code:
// libraries for RTC clock DS1307
#include <Wire.h>
#include <DS1307.h>

#include <OneWire.h>           // For temperature sensors 1-Wire interface library

OneWire ds(20);                // 1-wire temperature sensors connected on pin 20

int rtc[7];                                    // RTC clock output
//int ledPin = 0;                // LED connected to digital pin 13

//define unique sensor serial code
//call search_devices() to determine codes
byte temp_in[8]    = {0x28, 0x26, 0x82, 0xC7, 0x01, 0x00, 0x00, 0x46};
byte temp_out[8]   = {0x28, 0x95, 0x9B, 0xC7, 0x01, 0x00, 0x00, 0x97};
byte temp_spare[8] = {0x28, 0xED, 0xAB, 0xC7, 0x01, 0x00, 0x00, 0x2A};


void setup()                    // run once, when the sketch starts
{
  //pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  Serial.begin(9600);             // Open the first serial port (debug/upload/testing/etc.)
  Serial.println("Hello world!"); // Print something to the serial port
  
  /*
  // Set the time for RTC  
  RTC.stop();
  RTC.set(DS1307_YR,9);     // Year (0-99)
  RTC.set(DS1307_MTH,2);    // Month (1-12)
  RTC.set(DS1307_DATE,13);  // Day of month
  RTC.set(DS1307_HR,23);    // Hours (0-23)
  RTC.set(DS1307_MIN,16);   // Minutes (0-59)
  RTC.set(DS1307_SEC,0);    // Seconds (0-59)
  RTC.set(DS1307_DOW,5);    // Day of week (1-7)
  RTC.start();
  */
  
}

// Function searches for 1-Wire device and prints its address if found
// Use one device at a time
void search_devices(){    
   byte addr[8];
   int i=0;
 
  if ( !ds.search(addr)) {
      Serial.println("No more addresses.\n");
      ds.reset_search();
      return;
  }
  Serial.print("R=");
  for( i = 0; i < 8; i++) {
     Serial.print(addr[i], HEX);
     Serial.print(" ");
  }
  Serial.println();
  
  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!\n");
      return;
  }
 
  if ( addr[0] != 0x28) { // DS18B20 sensor identification number
      Serial.println("Device is not a DS18B20 family device.\n");
      return;
  }
}

// Function reads the temperature from the temperature sensor when addr is a sensor ID
int get_temp(byte* addr){
  byte present = 0;
  byte i;
  byte data[12];
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end
 
  //delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
 
  present = ds.reset();
  ds.select(addr);  
  ds.write(0xBE);         // Read Scratchpad
  //Serial.print("P=");
  //Serial.print(present,HEX);
  //Serial.print(" ");
  for ( i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
   //Serial.print(data[i], HEX);
   //Serial.print(" ");
  }
   //Serial.println();
  int temp;
 
  // compute temperature
  temp = data[1]*256 + data[0];      
  // check if the number is negative
  if (data[1] > 0x07)  // sign bit set, temp is negative
     temp = temp-65536; // correct the number acordingly

  // Convert temperature C -> F (multiplied by 10 to avoid float)
  //temp = (int)(((temp * 1.8) + 32)*10);  
  return temp;
}



void loop()                     // run over and over again
{
  //digitalWrite(ledPin, HIGH);   // sets the LED on
  //delay(1000);                  // waits for a second
  //digitalWrite(ledPin, LOW);    // sets the LED off
  //delay(1000);                  // waits for a second
  
  
  /*
  // Procedure to test if there is any communication going on in 1-Wire line
  Serial.println("Starting test...");
  int result = ds.reset(); //perform 1-wire bus rest and see if any devices wake up.
  if( result == 1 )
     Serial.println("A device was found on 1-wire bus");
  else
     Serial.println("No device answered.  Bus broken or shorted");
  
  Serial.println("...end test\n\n");
  */
  
  // Enable this line to get sensor device id. Comment out when finished. Test one sensor at a time
  //search_devices();
  
   float temp1, temp2, temp3;               // true temperature
   int temp11, temp21, temp31;              // temperature before comma
   int temp12, temp22, temp32;              // temperature 1 digit after comma
   temp1 = get_temp(temp_in)/16.0;
   temp2 = get_temp(temp_out)/16.0;
   temp3 = get_temp(temp_spare)/16.0;
   temp11 = (int) temp1;
   temp21 = (int) temp2;
   temp31 = (int) temp3;
   temp12 = temp1*10 - temp11*10;
   temp22 = temp2*10 - temp21*10;
   temp32 = temp3*10 - temp31*10;
  
   // send temperatures to the serial port in a format xx.x
   Serial.print("Temp_in    = ");
   Serial.print(temp11, DEC);
   Serial.print(".");
   Serial.print(temp12, DEC);
   //Serial.print("    ");
   //Serial.println(temp1*100, DEC);
   Serial.print("    Temp_out   = ");
   Serial.print(temp21, DEC);
   Serial.print(".");
   Serial.print(temp22, DEC);
   //Serial.print("    ");
   //Serial.println(temp2*100, DEC);
   Serial.print("    Temp_spare = ");
   Serial.print(temp31, DEC);
   Serial.print(".");
   Serial.println(temp32, DEC);
   //Serial.print("    ");
   //Serial.println(temp3*100, DEC);
   Serial.println();
  
  
  // Get the time from RTC
   RTC.get(rtc,true);
   // print date to the serial port
   Serial.print("Time: ");
   Serial.print(rtc[6], DEC);
   Serial.print("-");
   Serial.print(rtc[5], DEC);
   Serial.print("-");
   Serial.print(rtc[4], DEC);
   Serial.print(" ");
   Serial.print(rtc[2], DEC);
   Serial.print(":");
   Serial.print(rtc[1], DEC);
   Serial.print(":");
   Serial.print(rtc[0], DEC);
   switch (rtc[3]) {
    case 1:
      Serial.println("  Monday");
      break;
    case 2:
      Serial.println("  Tuesday");
      break;
    case 3:
      Serial.println("  Wednesday");
      break;
    case 4:
      Serial.println("  Thursday");
      break;
    case 5:
      Serial.println("  Friday");
      break;
    case 6:
      Serial.println("  Saturday");
      break;
    case 7:
      Serial.println("  Sunday");
      break;
  }
  delay(1000);                  // waits for 1 second
  
}
Logged

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

Hi all, after a long hiatus from Arduino tinkering, I find myself needing an RTC again.

I took my original code, and added the corrections suggested by dkostas and uploaded them to Google Code, a project called libds1307.

http://code.google.com/p/libds1307/

If anyone has any other fixes, please post them here.

Thanks

Matt
Logged

France
Offline Offline
Newbie
*
Karma: 0
Posts: 10
french Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello,
i try to test your DS1307 lib but i have some Pb with the compil (with 0015)

Code:
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

void setup()
{
}

void loop()
{
}

result :
Code:
In file included from DS1307.cpp:5:
/DS1307.H:6:1: error: unterminated #ifndef
DS1307.cpp: In member function 'void DS1307::set(int, int)':
DS1307.cpp:98: warning: suggest parentheses around arithmetic in operand of |
hardware\libraries\DS1307/DS1307.h:6:1: error: unterminated #ifndef

but i'm sure that i put the right code in the right library at the right place

Philippe
Logged

France
Offline Offline
Newbie
*
Karma: 0
Posts: 10
french Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

well, i remove the line
Code:
#ifndef DS1307_h
in the DS1307.h file and it create the DS1307.o file ...

why ?
« Last Edit: April 04, 2009, 03:23:29 am by phd0 » Logged

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

phd0,

The arithmetic warning is fixed in the latest version, available here : http://code.google.com/p/libds1307/source/browse/#svn/trunk

For the #ifndef error, just check you have #endif at the end of the .h file.

I've tested this on 0015.
Logged

Kansas City
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am running Matt's code but the time on the RTC is not changing.  I am using the module from SparkFun with the resistors.

Please be gentle.  I have been programming for 20 years but this is my first foray into this sort of stuff and I am definitely not an electronics guy.

I can see a response from the clock but it's all 0s.

Can somebody help me?

thanks,
Roger
Logged

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


I think if the response is all zeros, check the scl/sda connections.
Logged

France
Offline Offline
Newbie
*
Karma: 0
Posts: 10
french Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello matt,

can you add to your lib some function to use the RAM inside the DS1307, with a direct access
like :

Code:
 int DS1307ram[55]; // for RAM in adresse location 08h to 3Fh so : 56x8
RTC.getRAM(DS1307ram);
 for read RAM and
Code:
 RTC.setRAM(DS1307ram);
 for write Byte in RAM.

can you add to the keywords.txt file in your lib ?

yes,   :smiley  i want a lot of things
« Last Edit: April 18, 2009, 05:23:37 am by phd0 » Logged

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

I added some functions to read/write the sram.
Bytes not Integers though.

   void get_sram_data(byte *);
    void set_sram_data(byte *);
    byte get_sram_byte(int);
    void set_sram_byte(byte, int);


Available here : http://code.google.com/p/libds1307/source/browse/trunk

...needs proper testing.

What's the keywords.txt ?
Logged

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


ok, keywords.txt, done.
http://code.google.com/p/libds1307/source/browse/trunk/keywords.txt
Logged

France
Offline Offline
Newbie
*
Karma: 0
Posts: 10
french Arduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i like you !

 smiley-grin

i test it, :
Code:
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

int rtc[7];
int last_s;
byte data[56];

void setup()
{
Serial.begin(9600);
for(int i=0; i<56; i++)
  {
  RTC.set_sram_byte(i+64,i);
  }
}

void loop()
{
  RTC.get(rtc,true);
  if (rtc[0] != last_s )
  {
   last_s = rtc[0];
   Serial.print((rtc[0]));
   Serial.print(":");
   Serial.print(RTC.get_sram_byte(rtc[0]));
   Serial.println();
   }
}
my results :
  • if i want RAM from adress > 56, it return some hazardous variable... but want 00
  • is byte data[56]; realy util ?


i'm trying to create a lib for Astronomical clock :
turn ON and OFF a light with the Sun rise and Sun set with GPS (long + lat coords)

Philippe
« Last Edit: April 21, 2009, 03:24:25 pm by phd0 » Logged

Pages: 1 ... 3 4 [5] 6 7 8   Go Up
Jump to: