Files at this revision

API Documentation at this revision

Comitter:
misodengaku
Date:
Wed Jun 20 04:09:39 2012 +0000
Commit message:
test

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC8564.cpp	Wed Jun 20 04:09:39 2012 +0000
@@ -0,0 +1,61 @@
+#include "mbed.h"
+#include "RTC8564.h"
+
+void RTC8564::write(char address, char value)
+{
+    i2c.start();
+    i2c.write(RTC8564NB_ADR);
+    i2c.write(address);
+    i2c.write(value);
+    i2c.stop();
+}
+
+char RTC8564::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 RTC8564::time_just()
+{
+    char _min, _hour;
+    _min = read(MINUTES);
+    if (_min >= 0x30) { 
+            _hour = read(HOURS);
+            if (_hour == 0x23)
+                _hour = 0x00;
+            else if ((_hour & 0x0F) == 0x09)
+                _hour = (_hour & 0xF0) + 0x10;
+            else
+                _hour = _hour + 0x01;
+            write(HOURS, _hour);
+    }
+    write(MINUTES, 0x00);
+    write(SECONDS, 0x00);
+}
+
+
+void RTC8564::setdaytime(char y[3], char m[3], char d[3], char h[3], char min[3], char s[3], char week_val[2])
+{
+    write(CONTROL1, 0x20); //stop
+    write(CONTROL2, 0x00);
+    write(YEARS, ((y[0]-0x30)<<4)+(y[1]-0x30)); 
+    write(MONTHS, ((m[0]-0x30)<<4)+(m[1]-0x30));
+    write(DAYS, ((d[0]-0x30)<<4)+(d[1]-0x30));
+    write(HOURS, ((h[0]-0x30)<<4)+(h[1]-0x30));
+    write(MINUTES, ((min[0]-0x30)<<4)+(min[1]-0x30));
+    write(SECONDS, ((s[0]-0x30)<<4)+(s[1]-0x30));
+    write(WEEKDAYS, week_val[0] - 0x30); //Sunday = "0", Monday = "1" ...
+    write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
+    write(TIMER_CONTROL, 0x00);
+    write(CONTROL1, 0x00); //start
+    return;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RTC8564.h	Wed Jun 20 04:09:39 2012 +0000
@@ -0,0 +1,32 @@
+#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
+#include "mbed.h"
+
+class RTC8564{
+public:
+    RTC8564(PinName sda = p9, PinName scl = p10) : i2c(sda, scl) {
+    }
+    void write(char address, char value);
+    char read(char address);
+    void time_just();
+    void setdaytime(char y[3], char m[3], char d[3], char h[3], char min[3], char s[3], char week_val[2]);
+private:
+    I2C i2c;
+};
\ No newline at end of file