a

Dependencies:   mbed

Committer:
bulmenwt
Date:
Wed Jan 28 07:54:03 2015 +0000
Revision:
0:4c51ee480cf6
a

Who changed what in which revision?

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