Pages: [1]   Go Down
Author Topic: PCF8563 I2C Real-time clock/calendar/alarm/timer Library  (Read 586 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I basically just wrapped up this post / tutorial in nice and userfriendly class.

Warning: untested as of now (My package didn't yet arrive smiley-razz)

The API:
Code:
/*
 PCF8563.h - Library for working with NXP's RTC 8563.
 Created by Sol Bekic (S0lll0s), August, 2014.
 Released into the public domain.
 */
#define PCF8563_OSC_OFF       0
#define PCF8563_OSC_1HZ       B10000011
#define PCF8563_OSC_32KHZ     B10000010
#define PCF8563_OSC_1.024KHZ  B10000001
#define PCF8563_OSC_32.768KHZ B10000000

#define PCF8563_ALARM_MINUTE    B10000000
#define PCF8563_ALARM_HOUR      B01000000
#define PCF8563_ALARM_DAYOFWEEK B00100000
#define PCF8563_ALARM_DAY       B00010000

class PCF8563
{
public:
  PCF8563();
  PCF8563(int address);
  void setDate(pcf8563date date);                // set date
  void setTime(pcf8563time time);                // set time
  void setTimeAndDate(pcf8563datetime all);      // set time and date
  void setOscillator(int mode);                  // set the oscillator frequency or disable it
  void setAlarm(pcf8563alarm alarm);             // set alarm; OR PCF856_ALARM_* together to set which values (minute, hour...) are active
  void disableAlarm();                           // disable a triggered alarm
  void enableHardwareAlarm(boolean state);       // make alarm available on the PCF's pin 3 (open-drain)
  pcf8563time     getTime();                     // read the current time
  pcf8563date     getDate();                     // read the current date
  pcf8563datetime getTimeAndDate();              // read the current time and date
  boolean isTimeAccurate();                      // check if time (might) be inaccurate (timer was turned off). If it is, rewrite the time to unset
  boolean wasAlarmTriggered();                   // check if the alarm has been triggered. Use disableAlarm() to unset
private:
  int bcdToDec(int value);
  int decToBcd(int value);
  int _address;
};

It would be awesome if someone was able to test this.

* PCF8563.zip (2.2 KB - downloaded 10 times.)
Logged

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

no such chip (or datasheet) but a few comments

as an i2c address fits in a byte (uint8_t)  you should probably

PCF8563(uint8_t address);
...
uint8_t _address;

void setOscillator(int mode);   // shouldn't  that be a byte  too? reduce footprint (every byte counts)

I did not dive into the code yet
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: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are correct, and I originally had all the internal values typed as "byte". That however threw an error on every "requestFrom", something like "ISO C++ says requestFrom(int, int) and requestFrom(uint8, uint8) should be ambiguous but worst ...of the first Is worse than worst... Of the second"
I searched for the error and found a thread where someone told to just put int, and t
Logged

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

you could add casting in those places, please try

requestFrom( (int) _address, (int) nr_of_bytes);

gives the compiler the info to solve the ambiguousness
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: