RX-8025NB Real Time Clock Module by EPSON

Dependents:   TYBLE16_simple_data_logger Check_external_RTC

Revision:
4:d8ce59684dfa
Parent:
3:d59c12d14ca9
Child:
5:e8e8b1b6c103
--- a/RX8025NB.cpp	Wed May 04 05:42:25 2016 +0000
+++ b/RX8025NB.cpp	Thu May 05 02:13:03 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: May        4th, 2016
+ *      Revised: May        5th, 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
@@ -105,27 +105,25 @@
 }
 
 /////////////// Set Alarm-D / INTA ////////////////////////
-void RX8025::set_next_alarmD_INTA (uint16_t time){
+uint8_t RX8025::set_alarmD_reg (uint16_t time){
     tm t;
     char dt;
     uint8_t m, h;
+    uint16_t set, real;
 
     dt = read_reg_byte(RX8025_REG_CONTL1);
     dt &= ~0x40;        // DALE = 0
     dt = write_reg_byte(RX8025_REG_CONTL1, dt);
-    if (time > 1440){   // Less than 24 hours
-        time = 1440;
-    } else if ((time == 0)){
-        time = 1;       // 1 minute is minimum vale
-    }
     read_rtc_std(&t);   // read current time
-    m = t.tm_min + (time % 60);
-    h = t.tm_hour; 
+    real = t.tm_hour * 60 + t.tm_min;
+    set = real + time;
+    m = t.tm_min + (uint8_t)(time % 60);
+    h = t.tm_hour;
     if (m >= 60){
         m -= 60;
         h += 1;
     }
-    h = h + (time / 60);
+    h += (uint8_t)(time / 60);
     if (h >= 24){
         h -= 24;
     }
@@ -136,6 +134,37 @@
     dt = read_reg_byte(RX8025_REG_CONTL1);
     dt |= 0x40;         // DALE = 1
     dt = write_reg_byte(RX8025_REG_CONTL1, dt);
+    real = bcd2bin(read_reg_byte(RX8025_REG_ALARMD_MIN));
+    real += (bcd2bin(read_reg_byte(RX8025_REG_ALARMD_HOUR)) * 60);
+    if (set == real) {
+        read_rtc_std(&t);   // read current time
+        real = t.tm_hour * 60 + t.tm_min;
+        if (set > real){
+            return 1;
+        } else {
+            return 0;
+        }
+    } else {
+        return 0;
+    }
+}
+
+void RX8025::set_next_alarmD_INTA (uint16_t time){
+    uint8_t ret;
+    uint16_t t;
+
+    if (time < 2){
+        // Alarm does not check seconds digit.
+        // If 59 to 0 is occured during setting here, 1 minute will have a trouble.
+        t = 2;
+    } else if (time > 1440){   // Less than 24 hours
+        t = 1440;
+    } else {
+        t = time;
+    }
+    do{
+        ret = set_alarmD_reg(t);
+    } while(ret == 0);
 }
 
 /////////////// Clear Alarm-D / INTA interrupt ////////////
@@ -146,7 +175,7 @@
         dt = reg & 0xfe;
         write_reg_byte(RX8025_REG_CONTL1, dt);
         reg = read_reg_byte(RX8025_REG_CONTL2);
-    } while (reg & 0x01);       
+    } while (reg & 0x01);
 }
 
 /////////////// Read/Write specific register //////////////
@@ -184,8 +213,7 @@
     tm->rtc_year = tm->rtc_year_raw + 100 + 1900;
 }
 
-void RX8025::write_rtc_direct (rtc_time *tm)
-{
+void RX8025::write_rtc_direct (rtc_time *tm){
     rtc_buf[RX8025_REG_YEAR + 1] = bin2bcd(tm->rtc_year_raw);
     rtc_buf[RX8025_REG_MON  + 1] = bin2bcd(tm->rtc_month);
     rtc_buf[RX8025_REG_WDAY + 1] = (tm->rtc_weekday & 0x07);