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

Dependencies:   mbed

Committer:
misodengaku
Date:
Tue Jun 19 15:52:45 2012 +0000
Revision:
0:bfb6e89e28ae

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
misodengaku 0:bfb6e89e28ae 1 #include "mbed.h"
misodengaku 0:bfb6e89e28ae 2 #include "RTC8564.h"
misodengaku 0:bfb6e89e28ae 3
misodengaku 0:bfb6e89e28ae 4 I2C i2c(p9, p10);
misodengaku 0:bfb6e89e28ae 5
misodengaku 0:bfb6e89e28ae 6 void rtc_write(char address, char value)
misodengaku 0:bfb6e89e28ae 7 {
misodengaku 0:bfb6e89e28ae 8 i2c.start();
misodengaku 0:bfb6e89e28ae 9 i2c.write(RTC8564NB_ADR);
misodengaku 0:bfb6e89e28ae 10 i2c.write(address);
misodengaku 0:bfb6e89e28ae 11 i2c.write(value);
misodengaku 0:bfb6e89e28ae 12 i2c.stop();
misodengaku 0:bfb6e89e28ae 13 }
misodengaku 0:bfb6e89e28ae 14
misodengaku 0:bfb6e89e28ae 15 char rtc_read(char address)
misodengaku 0:bfb6e89e28ae 16 {
misodengaku 0:bfb6e89e28ae 17 char value;
misodengaku 0:bfb6e89e28ae 18 i2c.start();
misodengaku 0:bfb6e89e28ae 19 i2c.write(RTC8564NB_ADR);
misodengaku 0:bfb6e89e28ae 20 i2c.write(address);
misodengaku 0:bfb6e89e28ae 21 i2c.start();
misodengaku 0:bfb6e89e28ae 22 i2c.write(RTC8564NB_ADR | _READ);
misodengaku 0:bfb6e89e28ae 23 value = i2c.read(0);
misodengaku 0:bfb6e89e28ae 24 i2c.stop();
misodengaku 0:bfb6e89e28ae 25
misodengaku 0:bfb6e89e28ae 26 return value;
misodengaku 0:bfb6e89e28ae 27 }
misodengaku 0:bfb6e89e28ae 28
misodengaku 0:bfb6e89e28ae 29 void time_just()
misodengaku 0:bfb6e89e28ae 30 {
misodengaku 0:bfb6e89e28ae 31 char _min, _hour;
misodengaku 0:bfb6e89e28ae 32 _min = rtc_read(MINUTES);
misodengaku 0:bfb6e89e28ae 33 if (_min >= 0x30) {
misodengaku 0:bfb6e89e28ae 34 _hour = rtc_read(HOURS);
misodengaku 0:bfb6e89e28ae 35 if (_hour == 0x23)
misodengaku 0:bfb6e89e28ae 36 _hour = 0x00;
misodengaku 0:bfb6e89e28ae 37 else if ((_hour & 0x0F) == 0x09)
misodengaku 0:bfb6e89e28ae 38 _hour = (_hour & 0xF0) + 0x10;
misodengaku 0:bfb6e89e28ae 39 else
misodengaku 0:bfb6e89e28ae 40 _hour = _hour + 0x01;
misodengaku 0:bfb6e89e28ae 41 rtc_write(HOURS, _hour);
misodengaku 0:bfb6e89e28ae 42 }
misodengaku 0:bfb6e89e28ae 43 rtc_write(MINUTES, 0x00);
misodengaku 0:bfb6e89e28ae 44 rtc_write(SECONDS, 0x00);
misodengaku 0:bfb6e89e28ae 45 }
misodengaku 0:bfb6e89e28ae 46
misodengaku 0:bfb6e89e28ae 47
misodengaku 0:bfb6e89e28ae 48 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])
misodengaku 0:bfb6e89e28ae 49 {
misodengaku 0:bfb6e89e28ae 50 rtc_write(CONTROL1, 0x20); //stop
misodengaku 0:bfb6e89e28ae 51 rtc_write(CONTROL2, 0x00);
misodengaku 0:bfb6e89e28ae 52 rtc_write(YEARS, ((y[0]-0x30)<<4)+(y[1]-0x30));
misodengaku 0:bfb6e89e28ae 53 rtc_write(MONTHS, ((m[0]-0x30)<<4)+(m[1]-0x30));
misodengaku 0:bfb6e89e28ae 54 rtc_write(DAYS, ((d[0]-0x30)<<4)+(d[1]-0x30));
misodengaku 0:bfb6e89e28ae 55 rtc_write(HOURS, ((h[0]-0x30)<<4)+(h[1]-0x30));
misodengaku 0:bfb6e89e28ae 56 rtc_write(MINUTES, ((min[0]-0x30)<<4)+(min[1]-0x30));
misodengaku 0:bfb6e89e28ae 57 rtc_write(SECONDS, ((s[0]-0x30)<<4)+(s[1]-0x30));
misodengaku 0:bfb6e89e28ae 58 rtc_write(WEEKDAYS, week_val[0] - 0x30); //Sunday = "0", Monday = "1" ...
misodengaku 0:bfb6e89e28ae 59 rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
misodengaku 0:bfb6e89e28ae 60 rtc_write(TIMER_CONTROL, 0x00);
misodengaku 0:bfb6e89e28ae 61 rtc_write(CONTROL1, 0x00); //start
misodengaku 0:bfb6e89e28ae 62 return;
misodengaku 0:bfb6e89e28ae 63 }