Go Down

Topic: Stalker V2 Help!! Set the time? RFID Logger (Read 3 times) previous topic - next topic

ardudillo

#10
Jul 28, 2011, 12:10 am Last Edit: Jul 28, 2011, 12:15 am by ardudillo Reason: 1
DS1307RTC.cpp:

Quote

/*
 * DS1307RTC.h - library for DS1307 RTC
  
  Copyright (c) Michael Margolis 2009
  This library is intended to be uses with Arduino Time.h library functions

  The library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  
  30 Dec 2009 - Initial release
 */

#include <Wire.h>
#include "DS1307RTC.h"

#define DS1307_CTRL_ID 0x68 

DS1307RTC::DS1307RTC()
{
  Wire.begin();
}
  
// PUBLIC FUNCTIONS
time_t DS1307RTC::get()   // Aquire data from buffer and convert to time_t
{
  tmElements_t tm;
  read(tm);
  return(makeTime(tm));
}

void  DS1307RTC::set(time_t t)
{
  tmElements_t tm;
  breakTime(t, tm);
  tm.Second |= 0x80;  // stop the clock
  write(tm);
  tm.Second &= 0x7f;  // start the clock
  write(tm);
}

// Aquire data from the RTC chip in BCD format
void DS1307RTC::read( tmElements_t &tm)
{
  Wire.beginTransmission(DS1307_CTRL_ID);
  Wire.send(0x00);
  Wire.endTransmission();

  // request the 7 data fields   (secs, min, hr, dow, date, mth, yr)
  Wire.requestFrom(DS1307_CTRL_ID, tmNbrFields);
  
  tm.Second = bcd2dec(Wire.receive() & 0x7f);   
  tm.Minute = bcd2dec(Wire.receive() );
  tm.Hour =   bcd2dec(Wire.receive() & 0x3f);  // mask assumes 24hr clock
  tm.Wday = bcd2dec(Wire.receive() );
  tm.Day = bcd2dec(Wire.receive() );
  tm.Month = bcd2dec(Wire.receive() );
  tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
}

void DS1307RTC::write(tmElements_t &tm)
{
  Wire.beginTransmission(DS1307_CTRL_ID);
  Wire.send(0x00); // reset register pointer
  
  Wire.send(dec2bcd(tm.Second)) ;   
  Wire.send(dec2bcd(tm.Minute));
  Wire.send(dec2bcd(tm.Hour));      // sets 24 hour format
  Wire.send(dec2bcd(tm.Wday));   
  Wire.send(dec2bcd(tm.Day));
  Wire.send(dec2bcd(tm.Month));
  Wire.send(dec2bcd(tmYearToY2k(tm.Year)));   

  Wire.endTransmission(); 
}
// PRIVATE FUNCTIONS

// Convert Decimal to Binary Coded Decimal (BCD)
uint8_t DS1307RTC::dec2bcd(uint8_t num)
{
  return ((num/10 * 16) + (num % 10));
}

// Convert Binary Coded Decimal (BCD) to Decimal
uint8_t DS1307RTC::bcd2dec(uint8_t num)
{
  return ((num/16 * 10) + (num % 16));
}

DS1307RTC RTC = DS1307RTC(); // create an instance for the user



pathub

Greetings,
I just have the same problems, but i have some "stupid" work-around :D

This is what I do,

set the time in the array RX8025_time[7]
i set it beyond 2 minute from the PC realtime clock.

and place this in void setup()
  RX8025.setRtcTime(RX8025_time);

After the sketch upload done, check the date/time
And test is again with reset.
YES the clock is ticking, but setting back to RX8025_time[7] array again.

When the date/time is correct, then comment the line   
//RX8025.setRtcTime(RX8025_time);
in the void setup()

and upload the sketch again

When it done, check again with reset. The clock will keep ticking and give the correct date/time after reset :)

I hope it helped.
Sorry for the Inglish :D

mem

The Stalker 2 RTC is not compatible with the DS1307 so the RTC1307 code in the Time download wont work.

Here is an updated version that autodetects the RTC type.
Save the files as RTC.cpp and RTC.h. Its used in the sketch like the old library but the name is now RTC

Code: [Select]
/*
* RTC.cpp - library for DS1307 and RX8025 RTC chips
 
  Copyright (c) Michael Margolis 2009, 2010
  This library is intended to be used with Arduino Time.h library functions

  The library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
   30 Dec 2009 - Initial release for DS1307
   20 Nov 2010 - renamed to RTC and Added support for DX8025
*/

#include <WProgram.h>
#include <Wire.h>
#include "RTC.h"

//#include <HardwareSerial.h>  //only needed for diagnostics

const uint8_t DS1307_ADDRESS = 0x68; 
const uint8_t RX8025_ADDRESS = 0x32;

const uint8_t RX8025_CTL1   = 0xE0;
const uint8_t RX8025_CTL2   = 0xf0;

const uint8_t RX8025_24HR_MODE    = 0x20;      // set bit 5 for 24 hr mode

const uint8_t RX8025_NO_INTR      = 0x0;       // interupts off, INTA hi-Z
const uint8_t RX8025_INTR_PER_SEC = 0x4;       // INTA occurs once per second
const uint8_t RX8025_INTR_PER_MIN = 0x5;       // INTA occurs once per minute
const uint8_t RX8025_INTR_PER_HR  = 0x6;       // INTA occurs once per hour
const uint8_t RX8025_INTR_PER_MON = 0x7;       // INTA occurs once per month

// PUBLIC FUNCTIONS   

time_t RTCget()  // static function that can be used as a callback
{
  RTC.get();
}

rtcClass::rtcClass()
{

}

// returns true if selection is valid or autodetect succeeds in finding a device
bool rtcClass::begin(RTCDevice_t selection)
{
   device =  rtcUnknown; // default to indicate that no device is selected
   Wire.begin();
   if( selection == rtcUnknown)
   {
      // discover RTC device
      if(isAddressValid( RX8025_ADDRESS))
        selection = rtcRX8025;   
      else if(isAddressValid( DS1307_ADDRESS))
        selection = rtcDS1307;             
   }
   
   if(selection == rtcDS1307)
   {
       address = DS1307_ADDRESS;
       device = rtcDS1307;
   }
   else if(selection == rtcRX8025)
   {
     address = RX8025_ADDRESS;       
     device = rtcRX8025;
     writeRegister(RX8025_CTL1,RX8025_24HR_MODE); // RX8025 defaults to 12 hr mode so change to 24hrs   
     writeRegister(RX8025_CTL2,0);                // reset power on reset flag in control register 2     
   }
   //Serial.print("device = "); Serial.println(device, DEC);
   //Serial.print("address = 0x"); Serial.println(address, HEX);
   return (device != rtcUnknown); // returns true if a valid device selected
   
}   

time_t rtcClass::get()   // Aquire data from RTC and convert to time_t
{
  tmElements_t tm;
  read(tm);
  return(makeTime(tm));
}

void  rtcClass::set(time_t t)  // set RTC to given time
{
  tmElements_t tm;
  breakTime(t, tm);
  tm.Second |= 0x80;  // stop the clock
  write(tm);
  tm.Second &= 0x7f;  // start the clock
  write(tm);
}

// Aquire data from the RTC chip in BCD format
void rtcClass::read( tmElements_t &tm)
{
  Wire.beginTransmission(address);
  Wire.send(0x00);
  Wire.endTransmission();

  // request the data fields   (secs, min, hr, dow, date, mth, yr)
  if( device == rtcRX8025)
  {
     Wire.requestFrom((int)address, 8);
     Wire.receive();  // RX8025 gets an extra byte that is ignored
  }
  else     
    Wire.requestFrom((int)address, tmNbrFields);
 
  tm.Second = bcd2dec(Wire.receive() & 0x7f);   
  tm.Minute = bcd2dec(Wire.receive() & 0x7f);  // mask added for RX8025
  tm.Hour   = bcd2dec(Wire.receive() & 0x3f);  // mask assumes 24hr clock
  tm.Wday   = bcd2dec(Wire.receive() & 0x07);  // mask added for RX8025
  tm.Day    = bcd2dec(Wire.receive() & 0x3f);  // mask added for RX8025
  tm.Month  = bcd2dec(Wire.receive() & 0x1f);  // mask added for RX8025
  tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
}

void rtcClass::write(tmElements_t &tm)
{
  Wire.beginTransmission((int)address);
  Wire.send(0x00); // reset register pointer
 
  Wire.send(dec2bcd(tm.Second)) ;   
  Wire.send(dec2bcd(tm.Minute));
  Wire.send(dec2bcd(tm.Hour));      // sets 24 hour format
  Wire.send(dec2bcd(tm.Wday));   
  Wire.send(dec2bcd(tm.Day));
  Wire.send(dec2bcd(tm.Month));
  Wire.send(dec2bcd(tmYearToY2k(tm.Year)));   

  Wire.endTransmission(); 
}

// this method sets the RX8025 periodic interrupt to the given period
// the INTA pin is set low at the selected interval if the interrupt is enabled
void rtcClass::setPeriodicInterrupt( RTCIntrInterval_t interval)
{
  if( device != rtcRX8025)
     return; // only allow this method if using RX8025
  switch(interval) {
     case rtcIntrDisable    : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_NO_INTR); break;
     case rtcIntrEachSecond : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_SEC ); break;
     case rtcIntrEachMinute : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_MIN ); break;
     case rtcIntrEachHour   : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_HR ); break;
     case rtcIntrEachMonth  : writeRegister(RX8025_CTL1, RX8025_24HR_MODE | RX8025_INTR_PER_MON ); break;
   }
}

// this method sets the INTA pin high impedence ready for next interrupt
// call this after every interrupt
void rtcClass::resetPeriodicInterrupt( )
{
  if( device == rtcRX8025)
     writeRegister(RX8025_CTL2, 0); // clear the control2 register
}

// PRIVATE FUNCTIONS

/// returns true if there is an i2c device at the given address
bool rtcClass::isAddressValid(uint8_t address)
{
   Wire.beginTransmission(address);
   Wire.send(0x00);
   Wire.endTransmission();
   Wire.requestFrom((int)address,1);
   delay(10);
   if (Wire.available() > 0 )
   {
      Wire.receive();
      //Serial.print("check for addr "); Serial.print(address,HEX), Serial.println(" returned true");
      return true;
   }
   //Serial.print("check for addr "); Serial.print(address,HEX), Serial.println(" returned false");   
   return false;       
}       

// sets the given register to the given value
void rtcClass::writeRegister( uint8_t reg, uint8_t value)
{
  Wire.beginTransmission((int)address);
  Wire.send(reg);
  Wire.send(value);
  Wire.endTransmission(); 
}

// Convert Decimal to Binary Coded Decimal (BCD)
uint8_t rtcClass::dec2bcd(uint8_t num)
{
  return ((num/10 * 16) + (num % 10));
}

// Convert Binary Coded Decimal (BCD) to Decimal
uint8_t rtcClass::bcd2dec(uint8_t num)
{
  return ((num/16 * 10) + (num % 16));
}

rtcClass RTC = rtcClass(); // create an instance for the user


Code: [Select]
/*
* RTC.h - library for DS1307 RTC
* This library is intended to be uses with Arduino Time.h library functions
*/

#ifndef rtcClass_h
#define rtcClass_h
#include <Time.h>    // download from http://www.arduino.cc/playground/Code/Time

// the list of supported RTC devices
enum RTCDevice_t {rtcUnknown, rtcDS1307, rtcRX8025};

// the list of interrupt intervals supported by the RX8025
enum RTCIntrInterval_t {rtcIntrDisable, rtcIntrEachSecond, rtcIntrEachMinute,rtcIntrEachHour,rtcIntrEachMonth} ; 

extern time_t RTCget(); //  function that can be used as a sync provider callback 

// library interface description

class rtcClass
{
  // user-accessible "public" interface
  public:
    rtcClass();
bool begin(RTCDevice_t device = rtcUnknown);
     //getExternalTime get;   // pointer to the get time method
time_t get();
void set(time_t t);
void read(tmElements_t &tm);
void write(tmElements_t &tm);
void setPeriodicInterrupt( RTCIntrInterval_t interval); // RX8025 only, set the periodic interrupt
void resetPeriodicInterrupt();                         // call after each interrupt to reset the pin.       
     RTCDevice_t device;  // the active device
  private:
     uint8_t address;     
bool isAddressValid(uint8_t address);
uint8_t dec2bcd(uint8_t num);
     uint8_t bcd2dec(uint8_t num);
void writeRegister( uint8_t register, uint8_t value);
};

extern rtcClass RTC;

#endif




ardudillo

Thank you very much mem, I was about to try adding some function to the RX8025 library that returns time_t so I could feed it to the Time library's setSyncProvider() but thankfully your library will save me from getting mauled by the compiler.

I just tried your code and it initialises the RX8025 fine, as well as reading from it. However, the Time library doesn't seem to like its get() for some bizarre reason. Here is my code:

Quote

#include <RTC.h>
#include <Time.h>
#include <Wire.h>
 
void setup() {
  
  Serial.begin(9600);
  if (RTC.begin()) Serial.println("Valid RTC device initialised");
  else Serial.println("Problem initialising RTC");
  
  Serial.println("Testing the two flavours of the RTC.get() function (should return time_t):");
  Serial.print(RTC.get());
  Serial.print(" : ");
  Serial.println(RTCget());
  
  //setSyncProvider(RTCget());
  //setSyncProvider(RTC.get());
  
}
 
void loop() {
}


If I uncomment either line:

setSyncProvider(RTCget());
or
setSyncProvider(RTC.get());


I get the following error:

error: invalid conversion from 'time_t' to 'time_t (*)()'

which seems strange as, if you look at the DS1307RTC library (which works fine with the Time library) the get() is declared in the DS1307RTC.h (line 17) as follows:

static time_t get();


So, the expected return type is time_t. No idea, how the static is breaking things. Could you please post some sample code of registering your lib's get() function as a sync provider?

mem

sure, try this:
Code: [Select]
#include <RTC.h>
#include <Time.h>
#include <Wire.h>

void setup() {
 
  Serial.begin(9600);
  if (RTC.begin()) {
     Serial.println("Valid RTC device initialised");
     setSyncProvider(RTCget);  // register the get function
  }
  else {
    Serial.println("Problem initialising RTC"); 
  }


 
}

void loop() {
}

Go Up