http://mbed.org/users/jf1vrr/programs/RTC8564NB_Clock/lqbcpuを改造してシリアルに垂れ流すようにしたもの。日時は手で設定

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
misodengaku
Date:
Tue Jun 19 15:52:45 2012 +0000
Commit message:

Changed in this revision

RTC8564.cpp Show annotated file Show diff for this revision Revisions of this file
RTC8564.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r bfb6e89e28ae RTC8564.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC8564.cpp	Tue Jun 19 15:52:45 2012 +0000
@@ -0,0 +1,63 @@
+#include "mbed.h"
+#include "RTC8564.h"
+
+I2C i2c(p9, p10);
+
+void rtc_write(char address, char value)
+{
+    i2c.start();
+    i2c.write(RTC8564NB_ADR);
+    i2c.write(address);
+    i2c.write(value);
+    i2c.stop();
+}
+
+char rtc_read(char address)
+{
+    char value;
+    i2c.start();
+    i2c.write(RTC8564NB_ADR);
+    i2c.write(address);
+    i2c.start();
+    i2c.write(RTC8564NB_ADR | _READ);
+    value = i2c.read(0);
+    i2c.stop();
+    
+    return value;
+}
+
+void time_just()
+{
+    char _min, _hour;
+    _min = rtc_read(MINUTES);
+    if (_min >= 0x30) { 
+            _hour = rtc_read(HOURS);
+            if (_hour == 0x23)
+                _hour = 0x00;
+            else if ((_hour & 0x0F) == 0x09)
+                _hour = (_hour & 0xF0) + 0x10;
+            else
+                _hour = _hour + 0x01;
+            rtc_write(HOURS, _hour);
+    }
+    rtc_write(MINUTES, 0x00);
+    rtc_write(SECONDS, 0x00);
+}
+
+
+void rtc_setdaytime(char y[3], char m[3], char d[3], char h[3], char min[3], char s[3], char week_val[2])
+{
+    rtc_write(CONTROL1, 0x20); //stop
+    rtc_write(CONTROL2, 0x00);
+    rtc_write(YEARS, ((y[0]-0x30)<<4)+(y[1]-0x30)); 
+    rtc_write(MONTHS, ((m[0]-0x30)<<4)+(m[1]-0x30));
+    rtc_write(DAYS, ((d[0]-0x30)<<4)+(d[1]-0x30));
+    rtc_write(HOURS, ((h[0]-0x30)<<4)+(h[1]-0x30));
+    rtc_write(MINUTES, ((min[0]-0x30)<<4)+(min[1]-0x30));
+    rtc_write(SECONDS, ((s[0]-0x30)<<4)+(s[1]-0x30));
+    rtc_write(WEEKDAYS, week_val[0] - 0x30); //Sunday = "0", Monday = "1" ...
+    rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
+    rtc_write(TIMER_CONTROL, 0x00);
+    rtc_write(CONTROL1, 0x00); //start
+    return;
+}
\ No newline at end of file
diff -r 000000000000 -r bfb6e89e28ae RTC8564.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC8564.h	Tue Jun 19 15:52:45 2012 +0000
@@ -0,0 +1,25 @@
+#define RTC8564NB_ADR 0xA2
+
+#define CONTROL1 0x00
+#define CONTROL2 0x01
+#define SECONDS 0x02
+#define MINUTES 0x03
+#define HOURS 0x04
+#define DAYS 0x05
+#define WEEKDAYS 0x06
+#define MONTHS 0x07
+#define YEARS 0x08
+#define MINUTE_ALARM 0x09
+#define HOUR_ALARM 0x0A
+#define DAY_ALARM 0x0B
+#define WEEKDAY_ALARM 0x0C
+#define CLOCKOUT_FREQ 0x0D
+#define TIMER_CONTROL 0x0E
+#define TIMER 0x0F
+#define _READ 0x01
+
+
+void rtc_write(char address, char value);
+char rtc_read(char address);
+void time_just();
+void rtc_setdaytime(char y[3], char m[3], char d[3], char h[3], char min[3], char s[3], char week_val[2]);
\ No newline at end of file
diff -r 000000000000 -r bfb6e89e28ae main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 19 15:52:45 2012 +0000
@@ -0,0 +1,38 @@
+//Based on http://mbed.org/users/jf1vrr/programs/RTC8564NB_Clock/lqbcpu
+
+#include "mbed.h"
+#include "RTC8564.h"
+
+Serial pc(USBTX, USBRX);
+
+char year, month, day, week;
+char hour, minute, sec;
+
+char week_chr[7][4] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
+
+
+int main() {
+    //2012.06.20 00:30:00 Wed
+    rtc_setdaytime("12", "06", "20", "00", "30", "00", "3");
+    
+    while(1) { 
+        year = rtc_read(YEARS);
+        month = rtc_read(MONTHS);
+        day = rtc_read(DAYS);
+        week = rtc_read(WEEKDAYS);
+        hour = rtc_read(HOURS);
+        minute = rtc_read(MINUTES);
+        sec = rtc_read(SECONDS);
+        pc.printf("20%c%c/%c%c/%c%c %s\n",
+            ((year >> 4) & 0x03) + 0x30, (year & 0x0F) + 0x30, 
+                ((month >> 4) & 0x01) + 0x30, (month & 0x0F) + 0x30, 
+                    ((day >> 4) & 0x03)+ 0x30, (day & 0x0F) + 0x30, 
+                        week_chr[week & 0x07]);
+        pc.printf("%c%c:%c%c:%c%c\n",
+            ((hour >> 4) & 0x03) + 0x30, (hour & 0x0F) + 0x30, 
+                (minute >> 4) + 0x30, (minute & 0x0F) + 0x30, 
+                    (sec >> 4) + 0x30, (sec & 0x0F) + 0x30 );
+        
+        wait(0.1);
+    }
+}
diff -r 000000000000 -r bfb6e89e28ae mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jun 19 15:52:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912