a

Dependencies:   mbed

Committer:
bulmenwt
Date:
Wed Jan 28 07:54:38 2015 +0000
Revision:
0:8c0fc9c8ea79
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bulmenwt 0:8c0fc9c8ea79 1
bulmenwt 0:8c0fc9c8ea79 2
bulmenwt 0:8c0fc9c8ea79 3 /*This is the clock which used highly quality RTC module RT8564NB.
bulmenwt 0:8c0fc9c8ea79 4 This module is I2C controllable. At the time of poweron/reset, the start
bulmenwt 0:8c0fc9c8ea79 5 time received from ntp server.
bulmenwt 0:8c0fc9c8ea79 6 */
bulmenwt 0:8c0fc9c8ea79 7 #include "mbed.h"
bulmenwt 0:8c0fc9c8ea79 8
bulmenwt 0:8c0fc9c8ea79 9
bulmenwt 0:8c0fc9c8ea79 10
bulmenwt 0:8c0fc9c8ea79 11 #define RTC8564NB_ADR 0xA2
bulmenwt 0:8c0fc9c8ea79 12
bulmenwt 0:8c0fc9c8ea79 13 #define CONTROL1 0x00
bulmenwt 0:8c0fc9c8ea79 14 #define CONTROL2 0x01
bulmenwt 0:8c0fc9c8ea79 15 #define SECONDS 0x02
bulmenwt 0:8c0fc9c8ea79 16 #define MINUTES 0x03
bulmenwt 0:8c0fc9c8ea79 17 #define HOURS 0x04
bulmenwt 0:8c0fc9c8ea79 18 #define DAYS 0x05
bulmenwt 0:8c0fc9c8ea79 19 #define WEEKDAYS 0x06
bulmenwt 0:8c0fc9c8ea79 20 #define MONTHS 0x07
bulmenwt 0:8c0fc9c8ea79 21 #define YEARS 0x08
bulmenwt 0:8c0fc9c8ea79 22 #define MINUTE_ALARM 0x09
bulmenwt 0:8c0fc9c8ea79 23 #define HOUR_ALARM 0x0A
bulmenwt 0:8c0fc9c8ea79 24 #define DAY_ALARM 0x0B
bulmenwt 0:8c0fc9c8ea79 25 #define WEEKDAY_ALARM 0x0C
bulmenwt 0:8c0fc9c8ea79 26 #define CLOCKOUT_FREQ 0x0D
bulmenwt 0:8c0fc9c8ea79 27 #define TIMER_CINTROL 0x0E
bulmenwt 0:8c0fc9c8ea79 28 #define TIMER 0x0F
bulmenwt 0:8c0fc9c8ea79 29 #define _READ 0x01
bulmenwt 0:8c0fc9c8ea79 30
bulmenwt 0:8c0fc9c8ea79 31 Serial pc(USBTX, USBRX); // tx, rx
bulmenwt 0:8c0fc9c8ea79 32
bulmenwt 0:8c0fc9c8ea79 33
bulmenwt 0:8c0fc9c8ea79 34 I2C i2c(p28, p27);
bulmenwt 0:8c0fc9c8ea79 35 int offset_JAPAN = 32400;
bulmenwt 0:8c0fc9c8ea79 36
bulmenwt 0:8c0fc9c8ea79 37 char year, month, day, week;
bulmenwt 0:8c0fc9c8ea79 38 char hour, minute, sec;
bulmenwt 0:8c0fc9c8ea79 39 char ntp_year[3], ntp_month[3], ntp_day[3], ntp_week[4];
bulmenwt 0:8c0fc9c8ea79 40 char ntp_hour[3], ntp_minute[3], ntp_sec[3];
bulmenwt 0:8c0fc9c8ea79 41 char week_val;
bulmenwt 0:8c0fc9c8ea79 42
bulmenwt 0:8c0fc9c8ea79 43 char week_chr[7][4] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
bulmenwt 0:8c0fc9c8ea79 44
bulmenwt 0:8c0fc9c8ea79 45 void rtc_write(char address, char value)
bulmenwt 0:8c0fc9c8ea79 46 {
bulmenwt 0:8c0fc9c8ea79 47 i2c.start();
bulmenwt 0:8c0fc9c8ea79 48 i2c.write(RTC8564NB_ADR);
bulmenwt 0:8c0fc9c8ea79 49 i2c.write(address);
bulmenwt 0:8c0fc9c8ea79 50 i2c.write(value);
bulmenwt 0:8c0fc9c8ea79 51 i2c.stop();
bulmenwt 0:8c0fc9c8ea79 52 }
bulmenwt 0:8c0fc9c8ea79 53
bulmenwt 0:8c0fc9c8ea79 54 char rtc_read(char address)
bulmenwt 0:8c0fc9c8ea79 55 {
bulmenwt 0:8c0fc9c8ea79 56 char value;
bulmenwt 0:8c0fc9c8ea79 57 i2c.start();
bulmenwt 0:8c0fc9c8ea79 58 i2c.write(RTC8564NB_ADR);
bulmenwt 0:8c0fc9c8ea79 59 i2c.write(address);
bulmenwt 0:8c0fc9c8ea79 60 i2c.start();
bulmenwt 0:8c0fc9c8ea79 61 i2c.write(RTC8564NB_ADR | _READ);
bulmenwt 0:8c0fc9c8ea79 62 value = i2c.read(0);
bulmenwt 0:8c0fc9c8ea79 63 i2c.stop();
bulmenwt 0:8c0fc9c8ea79 64
bulmenwt 0:8c0fc9c8ea79 65 return value;
bulmenwt 0:8c0fc9c8ea79 66 }
bulmenwt 0:8c0fc9c8ea79 67
bulmenwt 0:8c0fc9c8ea79 68 int main() {
bulmenwt 0:8c0fc9c8ea79 69
bulmenwt 0:8c0fc9c8ea79 70 pc.printf("RTC8564NB CLOCK" );
bulmenwt 0:8c0fc9c8ea79 71 wait(2.0);
bulmenwt 0:8c0fc9c8ea79 72
bulmenwt 0:8c0fc9c8ea79 73
bulmenwt 0:8c0fc9c8ea79 74
bulmenwt 0:8c0fc9c8ea79 75 /* Set up NTP */
bulmenwt 0:8c0fc9c8ea79 76 pc.printf("Setting up NTP\n");
bulmenwt 0:8c0fc9c8ea79 77
bulmenwt 0:8c0fc9c8ea79 78
bulmenwt 0:8c0fc9c8ea79 79 //time_t seconds = time(NULL)+offset_JAPAN;
bulmenwt 0:8c0fc9c8ea79 80
bulmenwt 0:8c0fc9c8ea79 81
bulmenwt 0:8c0fc9c8ea79 82
bulmenwt 0:8c0fc9c8ea79 83 /*
bulmenwt 0:8c0fc9c8ea79 84 strftime(ntp_year, 16, "%y", localtime(&seconds));
bulmenwt 0:8c0fc9c8ea79 85 strftime(ntp_month, 16, "%m", localtime(&seconds));
bulmenwt 0:8c0fc9c8ea79 86 strftime(ntp_day, 16, "%d", localtime(&seconds));
bulmenwt 0:8c0fc9c8ea79 87 strftime(ntp_week, 16, "%a", localtime(&seconds));
bulmenwt 0:8c0fc9c8ea79 88 strftime(ntp_hour, 16, "%H", localtime(&seconds));
bulmenwt 0:8c0fc9c8ea79 89 strftime(ntp_minute, 16, "%M", localtime(&seconds));
bulmenwt 0:8c0fc9c8ea79 90 strftime(ntp_sec, 16, "%S", localtime(&seconds));
bulmenwt 0:8c0fc9c8ea79 91
bulmenwt 0:8c0fc9c8ea79 92 switch (ntp_week[0]){
bulmenwt 0:8c0fc9c8ea79 93 case 'S':
bulmenwt 0:8c0fc9c8ea79 94 switch (ntp_week[1]) {
bulmenwt 0:8c0fc9c8ea79 95 case 'u': week_val = 0x00; break;
bulmenwt 0:8c0fc9c8ea79 96 case 'a': week_val = 0x06; break;
bulmenwt 0:8c0fc9c8ea79 97 }
bulmenwt 0:8c0fc9c8ea79 98 break;
bulmenwt 0:8c0fc9c8ea79 99 case 'M': week_val = 0x01; break;
bulmenwt 0:8c0fc9c8ea79 100 case 'T':
bulmenwt 0:8c0fc9c8ea79 101 switch (ntp_week[1]) {
bulmenwt 0:8c0fc9c8ea79 102 case 'u': week_val = 0x02; break;
bulmenwt 0:8c0fc9c8ea79 103 case 'h': week_val = 0x04; break;
bulmenwt 0:8c0fc9c8ea79 104 }
bulmenwt 0:8c0fc9c8ea79 105 break;
bulmenwt 0:8c0fc9c8ea79 106 case 'W': week_val = 0x03; break;
bulmenwt 0:8c0fc9c8ea79 107 case 'F': week_val = 0x05; break;
bulmenwt 0:8c0fc9c8ea79 108 }
bulmenwt 0:8c0fc9c8ea79 109
bulmenwt 0:8c0fc9c8ea79 110 */
bulmenwt 0:8c0fc9c8ea79 111
bulmenwt 0:8c0fc9c8ea79 112 week_val = 0x04;
bulmenwt 0:8c0fc9c8ea79 113
bulmenwt 0:8c0fc9c8ea79 114 //
bulmenwt 0:8c0fc9c8ea79 115 /*
bulmenwt 0:8c0fc9c8ea79 116 rtc_write(CONTROL1, 0x20); //stop
bulmenwt 0:8c0fc9c8ea79 117 rtc_write(CONTROL2, 0x00);
bulmenwt 0:8c0fc9c8ea79 118 rtc_write(YEARS, (0x15));
bulmenwt 0:8c0fc9c8ea79 119 rtc_write(MONTHS, (0x01));
bulmenwt 0:8c0fc9c8ea79 120 rtc_write(DAYS, (0x23));
bulmenwt 0:8c0fc9c8ea79 121 rtc_write(HOURS, (0x17));
bulmenwt 0:8c0fc9c8ea79 122 rtc_write(MINUTES, (0x41));
bulmenwt 0:8c0fc9c8ea79 123 rtc_write(SECONDS, (0x00));
bulmenwt 0:8c0fc9c8ea79 124 rtc_write(WEEKDAYS, week_val);
bulmenwt 0:8c0fc9c8ea79 125 rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz
bulmenwt 0:8c0fc9c8ea79 126 rtc_write(TIMER_CINTROL, 0x00);
bulmenwt 0:8c0fc9c8ea79 127 rtc_write(CONTROL1, 0x00); //start
bulmenwt 0:8c0fc9c8ea79 128
bulmenwt 0:8c0fc9c8ea79 129 */
bulmenwt 0:8c0fc9c8ea79 130
bulmenwt 0:8c0fc9c8ea79 131 while(1) {
bulmenwt 0:8c0fc9c8ea79 132 year = rtc_read(YEARS);
bulmenwt 0:8c0fc9c8ea79 133 month = rtc_read(MONTHS);
bulmenwt 0:8c0fc9c8ea79 134 day = rtc_read(DAYS);
bulmenwt 0:8c0fc9c8ea79 135 week = rtc_read(WEEKDAYS);
bulmenwt 0:8c0fc9c8ea79 136 hour = rtc_read(HOURS);
bulmenwt 0:8c0fc9c8ea79 137 minute = rtc_read(MINUTES);
bulmenwt 0:8c0fc9c8ea79 138 sec = rtc_read(SECONDS);
bulmenwt 0:8c0fc9c8ea79 139 /*
bulmenwt 0:8c0fc9c8ea79 140 pc.printf("20%c%c/%c%c/%c%c %s\n",
bulmenwt 0:8c0fc9c8ea79 141 ((year >> 4) & 0x03) + 0x30, (year & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 142 ((month >> 4) & 0x01) + 0x30, (month & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 143 ((day >> 4) & 0x03)+ 0x30, (day & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 144 week_chr[week & 0x07]);
bulmenwt 0:8c0fc9c8ea79 145
bulmenwt 0:8c0fc9c8ea79 146
bulmenwt 0:8c0fc9c8ea79 147 pc.printf("%c%c:%c%c:%c%c\n",
bulmenwt 0:8c0fc9c8ea79 148 ((hour >> 4) & 0x03) + 0x30, (hour & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 149 (minute >> 4) + 0x30, (minute & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 150 (sec >> 4) + 0x30, (sec & 0x0F) + 0x30 );
bulmenwt 0:8c0fc9c8ea79 151
bulmenwt 0:8c0fc9c8ea79 152
bulmenwt 0:8c0fc9c8ea79 153 */
bulmenwt 0:8c0fc9c8ea79 154
bulmenwt 0:8c0fc9c8ea79 155 pc.printf("20%c%c/%c%c/%c%c %s\n",
bulmenwt 0:8c0fc9c8ea79 156 ((year >> 4) & 0x03) + 0x30, (year & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 157 ((month >> 4) & 0x01) + 0x30, (month & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 158 ((day >> 4) & 0x03)+ 0x30, (day & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 159 week_chr[week & 0x07]);
bulmenwt 0:8c0fc9c8ea79 160
bulmenwt 0:8c0fc9c8ea79 161
bulmenwt 0:8c0fc9c8ea79 162 pc.printf("%c%c:%c%c:%c%c\n",
bulmenwt 0:8c0fc9c8ea79 163 ((hour >> 4) & 0x03) + 0x30, (hour & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 164 (minute >> 4) + 0x30, (minute & 0x0F) + 0x30,
bulmenwt 0:8c0fc9c8ea79 165 (sec >> 4) + 0x30, (sec & 0x0F) + 0x30 );
bulmenwt 0:8c0fc9c8ea79 166
bulmenwt 0:8c0fc9c8ea79 167 wait(1.0);
bulmenwt 0:8c0fc9c8ea79 168 }
bulmenwt 0:8c0fc9c8ea79 169 }
bulmenwt 0:8c0fc9c8ea79 170
bulmenwt 0:8c0fc9c8ea79 171