my prototype PCF8563's library

Committer:
irsanjul
Date:
Fri Jan 22 08:50:40 2016 +0000
Revision:
0:47c63ed3af91
my prototype PCF8563's library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
irsanjul 0:47c63ed3af91 1 #include "mbed.h"
irsanjul 0:47c63ed3af91 2 #include "PCF8563.h"
irsanjul 0:47c63ed3af91 3
irsanjul 0:47c63ed3af91 4 extern Serial dbg;
irsanjul 0:47c63ed3af91 5
irsanjul 0:47c63ed3af91 6 PCF8563::PCF8563(PinName sda, PinName scl) : pcf8563_i2c (sda, scl)
irsanjul 0:47c63ed3af91 7 {
irsanjul 0:47c63ed3af91 8 pcf8563_i2c.frequency(PCF8563_FREQ);
irsanjul 0:47c63ed3af91 9 }
irsanjul 0:47c63ed3af91 10
irsanjul 0:47c63ed3af91 11 time_t PCF8563::now()
irsanjul 0:47c63ed3af91 12 {
irsanjul 0:47c63ed3af91 13 struct tm now;
irsanjul 0:47c63ed3af91 14 char start = 0x02;
irsanjul 0:47c63ed3af91 15 char buffer[7];
irsanjul 0:47c63ed3af91 16
irsanjul 0:47c63ed3af91 17 if(pcf8563_i2c.write(W8563_ADDR, &start, 1) != 0) return 0;
irsanjul 0:47c63ed3af91 18 if(pcf8563_i2c.read(R8563_ADDR, buffer, 7) != 0) return 0;
irsanjul 0:47c63ed3af91 19
irsanjul 0:47c63ed3af91 20 now.tm_sec = bcdToDecimal(buffer[0] & 0x7F);
irsanjul 0:47c63ed3af91 21 now.tm_min = bcdToDecimal(buffer[1] & 0x7F);
irsanjul 0:47c63ed3af91 22 now.tm_hour = bcdToDecimal(buffer[2] & 0x3F);
irsanjul 0:47c63ed3af91 23 now.tm_mday = bcdToDecimal(buffer[3] & 0x3F);
irsanjul 0:47c63ed3af91 24 now.tm_mon = bcdToDecimal(buffer[5] & 0x1F) - 1;
irsanjul 0:47c63ed3af91 25 now.tm_year = bcdToDecimal(buffer[6]) + 2000 - 1900;
irsanjul 0:47c63ed3af91 26
irsanjul 0:47c63ed3af91 27 return mktime(&now);
irsanjul 0:47c63ed3af91 28 }
irsanjul 0:47c63ed3af91 29
irsanjul 0:47c63ed3af91 30 bool PCF8563::set_time(time_t time)
irsanjul 0:47c63ed3af91 31 {
irsanjul 0:47c63ed3af91 32 struct tm *now;
irsanjul 0:47c63ed3af91 33 char buffer[8];
irsanjul 0:47c63ed3af91 34
irsanjul 0:47c63ed3af91 35 now = localtime(&time);
irsanjul 0:47c63ed3af91 36
irsanjul 0:47c63ed3af91 37 buffer[0] = 0x02; // memory address
irsanjul 0:47c63ed3af91 38 buffer[1] = decimalToBcd(now->tm_sec) & 0x7F; // VL = 0
irsanjul 0:47c63ed3af91 39 buffer[2] = decimalToBcd(now->tm_min) & 0x7F;
irsanjul 0:47c63ed3af91 40 buffer[3] = decimalToBcd(now->tm_hour) & 0x3F;
irsanjul 0:47c63ed3af91 41 buffer[4] = now->tm_wday + 1;
irsanjul 0:47c63ed3af91 42 buffer[5] = decimalToBcd(now->tm_mday) & 0x3F;
irsanjul 0:47c63ed3af91 43 buffer[6] = decimalToBcd(now->tm_mon+1) & 0x1F;
irsanjul 0:47c63ed3af91 44 buffer[7] = decimalToBcd(now->tm_year + 1900 - 2000);
irsanjul 0:47c63ed3af91 45
irsanjul 0:47c63ed3af91 46 if(pcf8563_i2c.write(W8563_ADDR, buffer, 9) != 0) return 0;
irsanjul 0:47c63ed3af91 47
irsanjul 0:47c63ed3af91 48 return true;
irsanjul 0:47c63ed3af91 49 }
irsanjul 0:47c63ed3af91 50
irsanjul 0:47c63ed3af91 51 bool PCF8563::set_alarm(time_t time)
irsanjul 0:47c63ed3af91 52 {
irsanjul 0:47c63ed3af91 53 struct tm *now;
irsanjul 0:47c63ed3af91 54 char buffer[5];
irsanjul 0:47c63ed3af91 55
irsanjul 0:47c63ed3af91 56 now = localtime(&time);
irsanjul 0:47c63ed3af91 57
irsanjul 0:47c63ed3af91 58 buffer[0] = 0x09; // memory address
irsanjul 0:47c63ed3af91 59 buffer[1] = decimalToBcd(now->tm_min) & 0x80;
irsanjul 0:47c63ed3af91 60 buffer[2] = decimalToBcd(now->tm_hour) & 0x80;
irsanjul 0:47c63ed3af91 61 buffer[3] = decimalToBcd(now->tm_mday) & 0x80;
irsanjul 0:47c63ed3af91 62 buffer[4] = (now->tm_wday + 1) & 0x80;
irsanjul 0:47c63ed3af91 63 if(pcf8563_i2c.write(W8563_ADDR, buffer, 4) != 0) return 0;
irsanjul 0:47c63ed3af91 64
irsanjul 0:47c63ed3af91 65 char Int[2];
irsanjul 0:47c63ed3af91 66 Int[0] = 0x01; // control address
irsanjul 0:47c63ed3af91 67 Int[1] = 0x02; // set AIE to 1 for INT pin
irsanjul 0:47c63ed3af91 68 if(pcf8563_i2c.write(W8563_ADDR, Int, 2) != 0) return 0;
irsanjul 0:47c63ed3af91 69
irsanjul 0:47c63ed3af91 70 return true;
irsanjul 0:47c63ed3af91 71 }
irsanjul 0:47c63ed3af91 72
irsanjul 0:47c63ed3af91 73 bool PCF8563::alarmOff()
irsanjul 0:47c63ed3af91 74 {
irsanjul 0:47c63ed3af91 75 char start = 0x01;
irsanjul 0:47c63ed3af91 76 char buffer;
irsanjul 0:47c63ed3af91 77
irsanjul 0:47c63ed3af91 78 if(pcf8563_i2c.write(W8563_ADDR, &start, 1) != 0) return 0;
irsanjul 0:47c63ed3af91 79 //if(pcf8563_i2c.read(R8563_ADDR, buffer, 1) != 0) return 0;
irsanjul 0:47c63ed3af91 80
irsanjul 0:47c63ed3af91 81 pcf8563_i2c.start();
irsanjul 0:47c63ed3af91 82 pcf8563_i2c.write(R8563_ADDR);
irsanjul 0:47c63ed3af91 83 buffer = pcf8563_i2c.read(0);
irsanjul 0:47c63ed3af91 84 pcf8563_i2c.stop();
irsanjul 0:47c63ed3af91 85
irsanjul 0:47c63ed3af91 86 buffer = buffer - 0x08; // clear AF to turn off alarm
irsanjul 0:47c63ed3af91 87
irsanjul 0:47c63ed3af91 88 char send[2];
irsanjul 0:47c63ed3af91 89 send[0] = 0x01; //control 1 address
irsanjul 0:47c63ed3af91 90 send[1] = buffer;
irsanjul 0:47c63ed3af91 91 if(pcf8563_i2c.write(W8563_ADDR, send, 2) != 0) return 0;
irsanjul 0:47c63ed3af91 92
irsanjul 0:47c63ed3af91 93 return true;
irsanjul 0:47c63ed3af91 94 }
irsanjul 0:47c63ed3af91 95
irsanjul 0:47c63ed3af91 96 bool PCF8563::check_alarm()
irsanjul 0:47c63ed3af91 97 {
irsanjul 0:47c63ed3af91 98 char start = 0x01;
irsanjul 0:47c63ed3af91 99 char buffer;
irsanjul 0:47c63ed3af91 100
irsanjul 0:47c63ed3af91 101 if(pcf8563_i2c.write(W8563_ADDR, &start, 1) != 0) return 0;
irsanjul 0:47c63ed3af91 102 //if(pcf8563_i2c.read(R8563_ADDR, buffer, 1) != 0) return 0;
irsanjul 0:47c63ed3af91 103
irsanjul 0:47c63ed3af91 104 pcf8563_i2c.start();
irsanjul 0:47c63ed3af91 105 pcf8563_i2c.write(R8563_ADDR);
irsanjul 0:47c63ed3af91 106 buffer = pcf8563_i2c.read(0);
irsanjul 0:47c63ed3af91 107 pcf8563_i2c.stop();
irsanjul 0:47c63ed3af91 108
irsanjul 0:47c63ed3af91 109 buffer = buffer & 0x08;
irsanjul 0:47c63ed3af91 110 if(buffer == 0x08)
irsanjul 0:47c63ed3af91 111 {
irsanjul 0:47c63ed3af91 112 dbg.printf("== ALARM ON ==\r\n");
irsanjul 0:47c63ed3af91 113 wait_ms(200);
irsanjul 0:47c63ed3af91 114
irsanjul 0:47c63ed3af91 115 alarmOff();
irsanjul 0:47c63ed3af91 116 }
irsanjul 0:47c63ed3af91 117 return true;
irsanjul 0:47c63ed3af91 118 }