my prototype PCF8563's library

Files at this revision

API Documentation at this revision

Comitter:
irsanjul
Date:
Fri Jan 22 08:50:40 2016 +0000
Commit message:
my prototype PCF8563's library

Changed in this revision

PCF8563.cpp Show annotated file Show diff for this revision Revisions of this file
PCF8563.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8563.cpp	Fri Jan 22 08:50:40 2016 +0000
@@ -0,0 +1,118 @@
+#include "mbed.h"
+#include "PCF8563.h"
+
+extern Serial dbg;
+
+PCF8563::PCF8563(PinName sda, PinName scl) : pcf8563_i2c (sda, scl)
+{
+    pcf8563_i2c.frequency(PCF8563_FREQ);
+}
+
+time_t PCF8563::now()
+{
+    struct tm now;
+    char start = 0x02;
+    char buffer[7];
+    
+    if(pcf8563_i2c.write(W8563_ADDR, &start, 1) != 0) return 0;
+    if(pcf8563_i2c.read(R8563_ADDR, buffer, 7) != 0) return 0;
+    
+    now.tm_sec = bcdToDecimal(buffer[0] & 0x7F);
+    now.tm_min = bcdToDecimal(buffer[1] & 0x7F);
+    now.tm_hour = bcdToDecimal(buffer[2] & 0x3F);
+    now.tm_mday = bcdToDecimal(buffer[3] & 0x3F);
+    now.tm_mon = bcdToDecimal(buffer[5] & 0x1F) - 1;
+    now.tm_year = bcdToDecimal(buffer[6]) + 2000 - 1900;
+    
+    return mktime(&now);
+}
+
+bool PCF8563::set_time(time_t time)
+{
+    struct tm *now;
+    char buffer[8];
+    
+    now = localtime(&time);
+    
+    buffer[0] = 0x02; // memory address
+    buffer[1] = decimalToBcd(now->tm_sec) & 0x7F; // VL = 0
+    buffer[2] = decimalToBcd(now->tm_min) & 0x7F;
+    buffer[3] = decimalToBcd(now->tm_hour) & 0x3F;
+    buffer[4] = now->tm_wday + 1;
+    buffer[5] = decimalToBcd(now->tm_mday) & 0x3F;
+    buffer[6] = decimalToBcd(now->tm_mon+1) & 0x1F;
+    buffer[7] = decimalToBcd(now->tm_year + 1900 - 2000);
+    
+    if(pcf8563_i2c.write(W8563_ADDR, buffer, 9) != 0) return 0;
+    
+    return true;
+}
+
+bool PCF8563::set_alarm(time_t time)
+{
+    struct tm *now;
+    char buffer[5];
+    
+    now = localtime(&time);
+    
+    buffer[0] = 0x09; // memory address
+    buffer[1] = decimalToBcd(now->tm_min) & 0x80;
+    buffer[2] = decimalToBcd(now->tm_hour) & 0x80;
+    buffer[3] = decimalToBcd(now->tm_mday) & 0x80;
+    buffer[4] = (now->tm_wday + 1) & 0x80;
+    if(pcf8563_i2c.write(W8563_ADDR, buffer, 4) != 0) return 0;
+    
+    char Int[2];
+    Int[0] = 0x01; // control address
+    Int[1] = 0x02; // set AIE to 1 for INT pin
+    if(pcf8563_i2c.write(W8563_ADDR, Int, 2) != 0) return 0;
+    
+    return true;
+}
+
+bool PCF8563::alarmOff()
+{
+    char start = 0x01;
+    char buffer;
+    
+    if(pcf8563_i2c.write(W8563_ADDR, &start, 1) != 0) return 0;
+    //if(pcf8563_i2c.read(R8563_ADDR, buffer, 1) != 0) return 0;
+    
+    pcf8563_i2c.start();
+    pcf8563_i2c.write(R8563_ADDR);
+    buffer = pcf8563_i2c.read(0);
+    pcf8563_i2c.stop();
+    
+    buffer = buffer - 0x08; // clear AF to turn off alarm
+    
+    char send[2];
+    send[0] = 0x01; //control 1 address
+    send[1] = buffer;
+    if(pcf8563_i2c.write(W8563_ADDR, send, 2) != 0) return 0;
+    
+    return true;
+}
+
+bool PCF8563::check_alarm()
+{
+    char start = 0x01;
+    char buffer;
+    
+    if(pcf8563_i2c.write(W8563_ADDR, &start, 1) != 0) return 0;
+    //if(pcf8563_i2c.read(R8563_ADDR, buffer, 1) != 0) return 0;
+    
+    pcf8563_i2c.start();
+    pcf8563_i2c.write(R8563_ADDR);
+    buffer = pcf8563_i2c.read(0);
+    pcf8563_i2c.stop();
+    
+    buffer = buffer & 0x08;
+    if(buffer == 0x08)
+    {
+        dbg.printf("== ALARM ON ==\r\n");
+        wait_ms(200);
+        
+        alarmOff();
+    }
+    return true;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PCF8563.h	Fri Jan 22 08:50:40 2016 +0000
@@ -0,0 +1,61 @@
+#ifndef PCF8563_H
+#define PCF8563_H
+
+#define W8563_ADDR     0xA2    // I2C address for write
+#define R8563_ADDR     0xA3    // I2C address for read
+#define PCF8563_FREQ     400000  // bus speed 400 kHz
+
+class PCF8563
+{
+public:
+    PCF8563(PinName sda, PinName scl);
+
+    /** Read current real time from PCF8563
+     *
+     * @returns
+     *   current time on success,
+     *   0 on error (I2C fail, clock not set)
+     */
+    time_t now();
+    
+    /** Write current real time to PCF8563
+     *
+     * @param time Real time to set up
+     * @returns
+     *   true on success,
+     *   false on error (I2C fail)
+     */
+    bool set_time(time_t time);
+    
+    /** Write alarm time to PCF8563
+     * @param time is set time for alarm
+     * @returns
+     *    true on success,
+     *    false on error
+     */
+    bool set_alarm(time_t time);
+    
+    /**
+     */
+    bool alarmOff();
+    
+    /**
+     */
+    bool check_alarm();
+    
+    /**
+     */
+    
+private:
+    I2C pcf8563_i2c;
+
+    static int bcdToDecimal(int bcd) {
+        return ((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F);
+    }
+
+    static int decimalToBcd(int dec) {
+        return (dec % 10) + ((dec / 10) << 4);
+    }
+};
+
+#endif