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