Pages: [1]   Go Down
Author Topic: Is there a standard place to upload libraries you create?  (Read 1002 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I wasn't happy with the libraries floating around for the DS1337 Real Time Clock, so I modified the example one for the DS1307 included with Time.h to add support for the alarms and square wave output. Is there a repository to upload it to share? Also anyone that wants to critique my code feel free, I'm very new to arduino.

DS1337.h:
Code:

/*
 * DS1337RTC.h - library for DS1337 RTC
 * This library is intended to be uses with Arduino Time.h library functions
 */

#ifndef DS1337RTC_h
#define DS1337RTC_h

#include <Time.h>

#define DS1337_CTRL_ID 0x68
#define CLOCK_ADDRESS 0x00
#define ALARM1_ADDRESS 0x07
#define ALARM2_ADDRESS 0x0B
#define STATUS_ADDRESS 0x0F
#define CONTROL_ADDRESS 0x0E
#define INTB 0
#define SQW 1

// library interface description
class DS1337RTC
{
  // user-accessible "public" interface
  public:
    DS1337RTC();
    static time_t get(int address);
    static void set(time_t t, int address);
    static time_t sync();
    static void read(tmElements_t &tm, int address);
    static void write(tmElements_t &tm, int address);
    static void enableAlarm(int address);
    static void disableAlarm(int address);
    static void resetAlarms();
    static void interruptSelect(int mode);
    static void freqSelect(int freq);

  private:
    static uint8_t dec2bcd(uint8_t num);
    static uint8_t bcd2dec(uint8_t num);
    static void startClock();
    static void stopClock();
};

extern DS1337RTC RTC;

#endif
 

DS1337.cpp:
Code:

/*
 * DS1337RTC.h - library for DS1337 RTC
 
  By Eric Trombly
 
  modified from DS1307RTC.h (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
 
  7 Jul 2011 - Initial release
 */

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

byte controlRegister = 0x18; // default settings on start

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

void  DS1337RTC::set(time_t t, int address) // Convert from time_t and save to rtc
{
  tmElements_t tm;
  breakTime(t, tm);
  stopClock();
  write(tm, address);
  startClock();
}

time_t DS1337RTC::sync()  // Function to use for Time.h setSyncProvider
{
  return(get(CLOCK_ADDRESS));
}

void DS1337RTC::read( tmElements_t &tm, int address) // Aquire data from the RTC chip in BCD format
{
  int numberBytes;
 
  switch(address){
    case CLOCK_ADDRESS: numberBytes = tmNbrFields; break;
    case ALARM1_ADDRESS: numberBytes = 4; break;
    case ALARM2_ADDRESS: numberBytes = 3; break;
  }
 
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(address);
  Wire.endTransmission();

  Wire.requestFrom(DS1337_CTRL_ID, numberBytes);
 
  if(address != ALARM2_ADDRESS){  // alarm 2 doesn't have a seconds field
    tm.Second = bcd2dec(Wire.receive() );
  }
  tm.Minute = bcd2dec(Wire.receive() );
  tm.Hour =   bcd2dec(Wire.receive() );
  if(address == ALARM1_ADDRESS || address == ALARM2_ADDRESS){  // the alarms don't have a wday,day,month,or year field
    tm.Day = bcd2dec(Wire.receive() );
    tm.Month = 0;
    tm.Year = 0;
  }else{
    tm.Wday = bcd2dec(Wire.receive() );
    tm.Day = bcd2dec(Wire.receive() );
    tm.Month = bcd2dec(Wire.receive() );
    tm.Year = y2kYearToTm((bcd2dec(Wire.receive())));
  }
}

void DS1337RTC::write(tmElements_t &tm, int address)
{
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(address);
 
  if(address != ALARM2_ADDRESS){  // alarm 2 doesn't have a seconds field
    Wire.send(dec2bcd(tm.Second));
  }
  Wire.send(dec2bcd(tm.Minute));
  Wire.send(dec2bcd(tm.Hour));
  if(address == ALARM1_ADDRESS || address == ALARM2_ADDRESS){  // the alarms don't have a wday,day,month,or year field
    Wire.send(dec2bcd(tm.Day));
  }else{
    Wire.send(dec2bcd(tm.Wday));   
    Wire.send(dec2bcd(tm.Day));
    Wire.send(dec2bcd(tm.Month));
    Wire.send(dec2bcd(tmYearToY2k(tm.Year)));   
  }
  Wire.endTransmission();
}

void DS1337RTC::enableAlarm(int address){  //turn on the A1IE or A2IE bit
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(CONTROL_ADDRESS); 
  if(address == ALARM1_ADDRESS){
    controlRegister |= 0x01;
  }else{
    controlRegister |= 0x03;
  }
 
  Wire.send(controlRegister);
  Wire.endTransmission();
}

void DS1337RTC::disableAlarm(int address){  // turn off the A1IE or A2IE bit
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(CONTROL_ADDRESS); 
  if(address == ALARM1_ADDRESS){
    controlRegister &= 0xFE;
  }else{
    controlRegister &= 0xFD;
  }
 
  Wire.send(controlRegister);
  Wire.endTransmission();
}

void DS1337RTC::resetAlarms(){  // reset the A1F and A2F bits
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(STATUS_ADDRESS);
  Wire.send(0x00);
  Wire.endTransmission();
}

void DS1337RTC::interruptSelect(int mode){  // set the INTCN bit, valid modes are INTB and SQW
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(CONTROL_ADDRESS);
  if(mode == INTB){
    controlRegister |= 0x04;
  }else{
    controlRegister &= 0xFB;
  }
 
  Wire.send(controlRegister);
  Wire.endTransmission();
}

void DS1337RTC::freqSelect(int freq){  // set RS1 and RS2 bits, freq 0=1Hz, 1=4.096kHz, 2=8.192kHz, 3=32.768kHz
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(CONTROL_ADDRESS);
  switch(freq){
    case 0: controlRegister &= 0xE7; break;
    case 1: controlRegister |= 0x08; break;
    case 2: controlRegister |= 0x10; break;
    case 3: controlRegister |= 0x18; break;
  }
 
  Wire.send(controlRegister);
  Wire.endTransmission();
}
// PRIVATE FUNCTIONS

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

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

void DS1337RTC::stopClock(){
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(STATUS_ADDRESS);
  Wire.send(0x80);
  Wire.endTransmission();
}

void DS1337RTC::startClock(){
  Wire.beginTransmission(DS1337_CTRL_ID);
  Wire.send(STATUS_ADDRESS);
  Wire.send(0x00);
  Wire.endTransmission();
}

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

example usage:
Code:
#include <DS1337RTC.h>
#include <Time.h>
#include <Wire.h>
#define alarmPin 12

char *monthName[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

void setup(){
  pinMode(alarmPin, INPUT);
  digitalWrite(alarmPin,HIGH);

  Serial.begin(9600);

  tmElements_t tmSet;
  tmSet.Year = 2011 - 1970;
  tmSet.Month = 7;
  tmSet.Day = 3;
  tmSet.Hour = 20;
  tmSet.Minute = 13;
  tmSet.Second = 0;
  RTC.set(makeTime(tmSet), CLOCK_ADDRESS); // set the clock

  tmSet.Second = 4;
  RTC.set(makeTime(tmSet), ALARM1_ADDRESS); // set the alarm for 4 seconds later

  RTC.enableAlarm(ALARM1_ADDRESS);

  RTC.freqSelect(1);  // set the squarewave freq on alarm pin b to 4.096kHz
}
 
void loop(){
  time_t alarm = RTC.get(ALARM1_ADDRESS);  // get the time the alarm is set for
  tmElements_t alarmSet;
  breakTime(alarm, alarmSet);

  //print the time using Time.h
  Serial.print(day());
  Serial.print(" ");
  Serial.print(monthName[month() - 1]);
  Serial.print(" ");
  Serial.print(year());
  Serial.print(" ");
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print("\t");

  // print the alarm time
  Serial.print("ALARM1: ");
  Serial.print((int)alarmSet.Day);
  Serial.print(" ");
  Serial.print((int)alarmSet.Hour);
  printDigits(alarmSet.Minute);
  printDigits(alarmSet.Second);
  Serial.print("\t");
 
  if(digitalRead(alarmPin) == HIGH){  //pin is set low when alarm is triggered
    Serial.println("ALARM off");
  }else{
    Serial.println("ALARM on");
    RTC.resetAlarms();
  }
 
  delay(1000);
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 160
Posts: 10418
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good question...  The parallax guys have an 'object exchange' for the propeller chip.  One issue is code quality - who manages quality issues?
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
Occasional activity, sporadic progress
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can add libraries to the "playground" http://arduino.cc/playground/Main/LibraryList.  People can access, comment on, and potentially improve upon them there.
Logged

Occasional activity,
sporadic progress

Offline Offline
Sr. Member
****
Karma: 0
Posts: 423
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice work.

However, i tried the example, and it returns an error while compiling:

Quote
RTC was not declared in this scope

By the way, is there a way to read the time such as unix time?

Thanks!
Logged

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

Looks like a really cool project, I've been hunting for a good library for about a week. Sadly, like madepablo, I can't get yours to compile either.

If anyone is looking for a good one and finds this, have a look at this one http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290497465 . I don't think it enables everything but it's the first one out of many that I have tried that compiles.

I hope iggykoopa can get back to us quickly I'm looking forward to try out your library, having code that works with the Time.h library is nice to make sketches more usable for people who might be using different RTCs.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if you look in the example code I set:
time_t alarm
then use breaktime() to get a tmelements_t. The time_t is unix time, it's an unsigned long. So just don't use breaktime() on it and you're already good to go. I'll look into why it's not compiling, I haven't touched the code in like a month because I'm waiting to design the clock face.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry I just had the names wrong in the example. It should be DS1337RTC.h and DS1337RTC.cpp, put them in a folder in your libraries folder called DS1337RTC. Also you will need the time library from here http://www.arduino.cc/playground/Code/Time.
Logged

Pages: [1]   Go Up
Jump to: