RX-8025NB Real Time Clock Module by EPSON

Dependents:   TYBLE16_simple_data_logger Check_external_RTC

Revision:
3:d59c12d14ca9
Parent:
2:ce49c4ba4c02
Child:
4:d8ce59684dfa
--- a/RX8025NB.cpp	Wed Mar 09 12:41:13 2016 +0000
+++ b/RX8025NB.cpp	Wed May 04 05:42:25 2016 +0000
@@ -7,7 +7,7 @@
  *  http://www.page.sannet.ne.jp/kenjia/index.html
  *  http://mbed.org/users/kenjiArai/
  *      Created: June       3rd, 2015
- *      Revised: March      8th, 2016
+ *      Revised: May        4th, 2016
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
@@ -19,21 +19,18 @@
 #include "mbed.h"
 #include "RX8025NB.h"
 
-RX8025::RX8025 (PinName p_sda, PinName p_scl) : _i2c(p_sda, p_scl)
-{
+RX8025::RX8025 (PinName p_sda, PinName p_scl) : _i2c(p_sda, p_scl){
     RX8025_addr = RX8025ADDR;
     init();
 }
 
-RX8025::RX8025 (I2C& p_i2c) : _i2c(p_i2c)
-{
+RX8025::RX8025 (I2C& p_i2c) : _i2c(p_i2c){
     RX8025_addr = RX8025ADDR;
     init();
 }
 
 /////////////// Initialize ////////////////////////////////
-void RX8025::init()
-{
+void RX8025::init(){
     tm t;
     char dt;
 
@@ -63,13 +60,11 @@
 }
 
 /////////////// Read RTC data /////////////////////////////
-void RX8025::get_time_rtc (tm *t)
-{
+void RX8025::get_time_rtc (tm *t){
     read_rtc_std(t);
 }
 
-void RX8025::read_rtc_std (tm *t)
-{
+void RX8025::read_rtc_std (tm *t){
     rtc_time time;
 
     read_rtc_direct(&time);
@@ -88,13 +83,11 @@
 }
 
 /////////////// Write data to RTC /////////////////////////
-void RX8025::set_time_rtc (tm *t)
-{
+void RX8025::set_time_rtc (tm *t){
     write_rtc_std(t);
 }
 
-void RX8025::write_rtc_std (tm *t)
-{
+void RX8025::write_rtc_std (tm *t){
     rtc_time time;
 
     time.rtc_seconds  = t->tm_sec;
@@ -112,52 +105,59 @@
 }
 
 /////////////// Set Alarm-D / INTA ////////////////////////
-void RX8025::set_next_alarmD_INTA (uint16_t time)
-{
+void RX8025::set_next_alarmD_INTA (uint16_t time){
     tm t;
     char dt;
+    uint8_t m, h;
 
     dt = read_reg_byte(RX8025_REG_CONTL1);
-    dt &= ~0x40;    // DALE = 0
+    dt &= ~0x40;        // DALE = 0
     dt = write_reg_byte(RX8025_REG_CONTL1, dt);
-    if (time > 1440){
+    if (time > 1440){   // Less than 24 hours
         time = 1440;
-    } else if ((time == 0) || (time == 1)){
-        time = 2;
+    } else if ((time == 0)){
+        time = 1;       // 1 minute is minimum vale
     }
     read_rtc_std(&t);   // read current time
-    rtc_buf[2] = bin2bcd((t.tm_hour + (time / 60)) % 24U);
-    rtc_buf[1] = bin2bcd((t.tm_min + (time % 60)) % 60U);
+    m = t.tm_min + (time % 60);
+    h = t.tm_hour; 
+    if (m >= 60){
+        m -= 60;
+        h += 1;
+    }
+    h = h + (time / 60);
+    if (h >= 24){
+        h -= 24;
+    }
+    rtc_buf[2] = bin2bcd(h);
+    rtc_buf[1] = bin2bcd(m);
     rtc_buf[0] = RX8025_REG_ALARMD_MIN << 4;
     _i2c.write(RX8025_addr, rtc_buf, 3, false);
     dt = read_reg_byte(RX8025_REG_CONTL1);
-    dt |= 0x40;    // DALE = 1
+    dt |= 0x40;         // DALE = 1
     dt = write_reg_byte(RX8025_REG_CONTL1, dt);
 }
 
 /////////////// Clear Alarm-D / INTA interrupt ////////////
-void RX8025::clear_alarmD_INTA ()
-{
-    char dt;
-
-    dt = read_reg_byte(RX8025_REG_CONTL2);
-    if (dt & 0x01){     // if DAFG = 1 then clear
-        dt &= ~0x01;    // DAFG = 0
-    }
-    dt = write_reg_byte(RX8025_REG_CONTL1, dt);
+void RX8025::clear_alarmD_INTA (){
+    char dt, reg;
+    reg = read_reg_byte(RX8025_REG_CONTL2);
+    do { // make sure to set Hi-imp state
+        dt = reg & 0xfe;
+        write_reg_byte(RX8025_REG_CONTL1, dt);
+        reg = read_reg_byte(RX8025_REG_CONTL2);
+    } while (reg & 0x01);       
 }
 
 /////////////// Read/Write specific register //////////////
-uint8_t RX8025::read_reg_byte(uint8_t reg)
-{
+uint8_t RX8025::read_reg_byte(uint8_t reg){
     rtc_buf[0] = reg << 4;
     _i2c.write(RX8025_addr, rtc_buf, 1, true);
     _i2c.read(RX8025_addr, rtc_buf, 1, false);
     return rtc_buf[0];
 }
 
-uint8_t RX8025::write_reg_byte(uint8_t reg, uint8_t data)
-{
+uint8_t RX8025::write_reg_byte(uint8_t reg, uint8_t data){
     rtc_buf[0] = reg << 4;
     rtc_buf[1] = data;
     _i2c.write(RX8025_addr, rtc_buf, 2, false);
@@ -165,14 +165,12 @@
 }
 
 /////////////// I2C Freq. /////////////////////////////////
-void RX8025::frequency(int hz)
-{
+void RX8025::frequency(int hz){
     _i2c.frequency(hz);
 }
 
 /////////////// Read/Write RTC another format /////////////
-void RX8025::read_rtc_direct (rtc_time *tm)
-{
+void RX8025::read_rtc_direct (rtc_time *tm){
     rtc_buf[0] = RX8025_REG_SEC << 4;
     _i2c.write(RX8025_addr, rtc_buf, 1, true);
     _i2c.read(RX8025_addr, rtc_buf, 10, false);
@@ -199,8 +197,7 @@
     _i2c.write(RX8025_addr, rtc_buf, 8, false);
 }
 
-uint8_t RX8025::bin2bcd (uint8_t dt)
-{
+uint8_t RX8025::bin2bcd (uint8_t dt){
     uint8_t bcdhigh = 0;
 
     while (dt >= 10) {
@@ -210,8 +207,7 @@
     return  ((uint8_t)(bcdhigh << 4) | dt);
 }
 
-uint8_t RX8025::bcd2bin (uint8_t dt)
-{
+uint8_t RX8025::bcd2bin (uint8_t dt){
     uint8_t tmp = 0;
 
     tmp = ((uint8_t)(dt & (uint8_t)0xf0) >> (uint8_t)0x4) * 10;