FRMD KL25Z <=i2c=> NXP PCF8563 (RTC) FRMD KL25Z SPI=> MCP23S17 => (GLCD) T6963C FRMD KL25Z Tsi => 3 button : <--> <enter> <++> FRMD KL25Z => Bip()
Fork of RTC8564NB_Clock by
main.cpp@0:50e7bcac5eb9, 2011-04-26 (annotated)
- Committer:
- jf1vrr
- Date:
- Tue Apr 26 07:53:06 2011 +0000
- Revision:
- 0:50e7bcac5eb9
- Child:
- 1:7c1d3790f08d
Rev. 0.01A 2011/04/26
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jf1vrr | 0:50e7bcac5eb9 | 1 | /*This is the clock which used highly quality RTC module RT8564NB. |
jf1vrr | 0:50e7bcac5eb9 | 2 | This module is I2C controllable. At the time of poweron/reset, the start |
jf1vrr | 0:50e7bcac5eb9 | 3 | time received from ntp server. |
jf1vrr | 0:50e7bcac5eb9 | 4 | */ |
jf1vrr | 0:50e7bcac5eb9 | 5 | #include "mbed.h" |
jf1vrr | 0:50e7bcac5eb9 | 6 | #include "TextLCD.h" |
jf1vrr | 0:50e7bcac5eb9 | 7 | #include "EthernetNetIf.h" |
jf1vrr | 0:50e7bcac5eb9 | 8 | #include "NTPClient.h" |
jf1vrr | 0:50e7bcac5eb9 | 9 | |
jf1vrr | 0:50e7bcac5eb9 | 10 | #define RTC8564NB_ADR 0xA2 |
jf1vrr | 0:50e7bcac5eb9 | 11 | |
jf1vrr | 0:50e7bcac5eb9 | 12 | #define CONTROL1 0x00 |
jf1vrr | 0:50e7bcac5eb9 | 13 | #define CONTROL2 0x01 |
jf1vrr | 0:50e7bcac5eb9 | 14 | #define SECONDS 0x02 |
jf1vrr | 0:50e7bcac5eb9 | 15 | #define MINUTES 0x03 |
jf1vrr | 0:50e7bcac5eb9 | 16 | #define HOURS 0x04 |
jf1vrr | 0:50e7bcac5eb9 | 17 | #define DAYS 0x05 |
jf1vrr | 0:50e7bcac5eb9 | 18 | #define WEEKDAYS 0x06 |
jf1vrr | 0:50e7bcac5eb9 | 19 | #define MONTHS 0x07 |
jf1vrr | 0:50e7bcac5eb9 | 20 | #define YEARS 0x08 |
jf1vrr | 0:50e7bcac5eb9 | 21 | #define MINUTE_ALARM 0x09 |
jf1vrr | 0:50e7bcac5eb9 | 22 | #define HOUR_ALARM 0x0A |
jf1vrr | 0:50e7bcac5eb9 | 23 | #define DAY_ALARM 0x0B |
jf1vrr | 0:50e7bcac5eb9 | 24 | #define WEEKDAY_ALARM 0x0C |
jf1vrr | 0:50e7bcac5eb9 | 25 | #define CLOCKOUT_FREQ 0x0D |
jf1vrr | 0:50e7bcac5eb9 | 26 | #define TIMER_CINTROL 0x0E |
jf1vrr | 0:50e7bcac5eb9 | 27 | #define TIMER 0x0F |
jf1vrr | 0:50e7bcac5eb9 | 28 | #define _READ 0x01 |
jf1vrr | 0:50e7bcac5eb9 | 29 | |
jf1vrr | 0:50e7bcac5eb9 | 30 | TextLCD lcd(p24, p26, p27, p28, p29, p30); |
jf1vrr | 0:50e7bcac5eb9 | 31 | EthernetNetIf eth; |
jf1vrr | 0:50e7bcac5eb9 | 32 | NTPClient ntp; |
jf1vrr | 0:50e7bcac5eb9 | 33 | I2C i2c(p9, p10); |
jf1vrr | 0:50e7bcac5eb9 | 34 | int offset_JAPAN = 32400; |
jf1vrr | 0:50e7bcac5eb9 | 35 | |
jf1vrr | 0:50e7bcac5eb9 | 36 | char year, month, day, week; |
jf1vrr | 0:50e7bcac5eb9 | 37 | char hour, minute, sec; |
jf1vrr | 0:50e7bcac5eb9 | 38 | char ntp_year[3], ntp_month[3], ntp_day[3], ntp_week[4]; |
jf1vrr | 0:50e7bcac5eb9 | 39 | char ntp_hour[3], ntp_minute[3], ntp_sec[3]; |
jf1vrr | 0:50e7bcac5eb9 | 40 | char week_val; |
jf1vrr | 0:50e7bcac5eb9 | 41 | |
jf1vrr | 0:50e7bcac5eb9 | 42 | char week_chr[7][4] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; |
jf1vrr | 0:50e7bcac5eb9 | 43 | |
jf1vrr | 0:50e7bcac5eb9 | 44 | void rtc_write(char address, char value) |
jf1vrr | 0:50e7bcac5eb9 | 45 | { |
jf1vrr | 0:50e7bcac5eb9 | 46 | i2c.start(); |
jf1vrr | 0:50e7bcac5eb9 | 47 | i2c.write(RTC8564NB_ADR); |
jf1vrr | 0:50e7bcac5eb9 | 48 | i2c.write(address); |
jf1vrr | 0:50e7bcac5eb9 | 49 | i2c.write(value); |
jf1vrr | 0:50e7bcac5eb9 | 50 | i2c.stop(); |
jf1vrr | 0:50e7bcac5eb9 | 51 | } |
jf1vrr | 0:50e7bcac5eb9 | 52 | |
jf1vrr | 0:50e7bcac5eb9 | 53 | char rtc_read(char address) |
jf1vrr | 0:50e7bcac5eb9 | 54 | { |
jf1vrr | 0:50e7bcac5eb9 | 55 | char value; |
jf1vrr | 0:50e7bcac5eb9 | 56 | i2c.start(); |
jf1vrr | 0:50e7bcac5eb9 | 57 | i2c.write(RTC8564NB_ADR); |
jf1vrr | 0:50e7bcac5eb9 | 58 | i2c.write(address); |
jf1vrr | 0:50e7bcac5eb9 | 59 | i2c.start(); |
jf1vrr | 0:50e7bcac5eb9 | 60 | i2c.write(RTC8564NB_ADR | _READ); |
jf1vrr | 0:50e7bcac5eb9 | 61 | value = i2c.read(0); |
jf1vrr | 0:50e7bcac5eb9 | 62 | i2c.stop(); |
jf1vrr | 0:50e7bcac5eb9 | 63 | |
jf1vrr | 0:50e7bcac5eb9 | 64 | return value; |
jf1vrr | 0:50e7bcac5eb9 | 65 | } |
jf1vrr | 0:50e7bcac5eb9 | 66 | |
jf1vrr | 0:50e7bcac5eb9 | 67 | int main() { |
jf1vrr | 0:50e7bcac5eb9 | 68 | lcd.cls(); |
jf1vrr | 0:50e7bcac5eb9 | 69 | lcd.printf("RTC8564NB CLOCK" ); |
jf1vrr | 0:50e7bcac5eb9 | 70 | wait(2.0); |
jf1vrr | 0:50e7bcac5eb9 | 71 | |
jf1vrr | 0:50e7bcac5eb9 | 72 | /* Set up Ethernet */ |
jf1vrr | 0:50e7bcac5eb9 | 73 | lcd.cls(); |
jf1vrr | 0:50e7bcac5eb9 | 74 | lcd.printf("Setting up Eth\n"); |
jf1vrr | 0:50e7bcac5eb9 | 75 | EthernetErr ethErr = eth.setup(); |
jf1vrr | 0:50e7bcac5eb9 | 76 | if (ethErr) { |
jf1vrr | 0:50e7bcac5eb9 | 77 | lcd.cls(); |
jf1vrr | 0:50e7bcac5eb9 | 78 | lcd.printf("Error with Eth\nNum: %d", ethErr); |
jf1vrr | 0:50e7bcac5eb9 | 79 | return -1; |
jf1vrr | 0:50e7bcac5eb9 | 80 | } |
jf1vrr | 0:50e7bcac5eb9 | 81 | |
jf1vrr | 0:50e7bcac5eb9 | 82 | /* Set up NTP */ |
jf1vrr | 0:50e7bcac5eb9 | 83 | lcd.printf("Setting up NTP\n"); |
jf1vrr | 0:50e7bcac5eb9 | 84 | Host server(IpAddr(), 123, "ntp1.jst.mfeed.ad.jp"); |
jf1vrr | 0:50e7bcac5eb9 | 85 | ntp.setTime(server); |
jf1vrr | 0:50e7bcac5eb9 | 86 | |
jf1vrr | 0:50e7bcac5eb9 | 87 | time_t seconds = time(NULL)+offset_JAPAN; |
jf1vrr | 0:50e7bcac5eb9 | 88 | |
jf1vrr | 0:50e7bcac5eb9 | 89 | lcd.cls(); |
jf1vrr | 0:50e7bcac5eb9 | 90 | strftime(ntp_year, 16, "%y", localtime(&seconds)); |
jf1vrr | 0:50e7bcac5eb9 | 91 | strftime(ntp_month, 16, "%m", localtime(&seconds)); |
jf1vrr | 0:50e7bcac5eb9 | 92 | strftime(ntp_day, 16, "%d", localtime(&seconds)); |
jf1vrr | 0:50e7bcac5eb9 | 93 | strftime(ntp_week, 16, "%a", localtime(&seconds)); |
jf1vrr | 0:50e7bcac5eb9 | 94 | strftime(ntp_hour, 16, "%H", localtime(&seconds)); |
jf1vrr | 0:50e7bcac5eb9 | 95 | strftime(ntp_minute, 16, "%M", localtime(&seconds)); |
jf1vrr | 0:50e7bcac5eb9 | 96 | strftime(ntp_sec, 16, "%S", localtime(&seconds)); |
jf1vrr | 0:50e7bcac5eb9 | 97 | |
jf1vrr | 0:50e7bcac5eb9 | 98 | switch (ntp_week[0]){ |
jf1vrr | 0:50e7bcac5eb9 | 99 | case 'S': |
jf1vrr | 0:50e7bcac5eb9 | 100 | switch (ntp_week[1]) { |
jf1vrr | 0:50e7bcac5eb9 | 101 | case 'u': week_val = 0x00; break; |
jf1vrr | 0:50e7bcac5eb9 | 102 | case 'a': week_val = 0x06; break; |
jf1vrr | 0:50e7bcac5eb9 | 103 | } |
jf1vrr | 0:50e7bcac5eb9 | 104 | break; |
jf1vrr | 0:50e7bcac5eb9 | 105 | case 'M': week_val = 0x01; break; |
jf1vrr | 0:50e7bcac5eb9 | 106 | case 'T': |
jf1vrr | 0:50e7bcac5eb9 | 107 | switch (ntp_week[1]) { |
jf1vrr | 0:50e7bcac5eb9 | 108 | case 'u': week_val = 0x02; break; |
jf1vrr | 0:50e7bcac5eb9 | 109 | case 'h': week_val = 0x04; break; |
jf1vrr | 0:50e7bcac5eb9 | 110 | } |
jf1vrr | 0:50e7bcac5eb9 | 111 | break; |
jf1vrr | 0:50e7bcac5eb9 | 112 | case 'W': week_val = 0x03; break; |
jf1vrr | 0:50e7bcac5eb9 | 113 | case 'F': week_val = 0x05; break; |
jf1vrr | 0:50e7bcac5eb9 | 114 | } |
jf1vrr | 0:50e7bcac5eb9 | 115 | |
jf1vrr | 0:50e7bcac5eb9 | 116 | rtc_write(CONTROL1, 0x20); //stop |
jf1vrr | 0:50e7bcac5eb9 | 117 | rtc_write(CONTROL2, 0x00); |
jf1vrr | 0:50e7bcac5eb9 | 118 | rtc_write(YEARS, ((ntp_year[0]-0x30)<<4)+(ntp_year[1]-0x30)); |
jf1vrr | 0:50e7bcac5eb9 | 119 | rtc_write(MONTHS, ((ntp_month[0]-0x30)<<4)+(ntp_month[1]-0x30)); |
jf1vrr | 0:50e7bcac5eb9 | 120 | rtc_write(DAYS, ((ntp_day[0]-0x30)<<4)+(ntp_day[1]-0x30)); |
jf1vrr | 0:50e7bcac5eb9 | 121 | rtc_write(HOURS, ((ntp_hour[0]-0x30)<<4)+(ntp_hour[1]-0x30)); |
jf1vrr | 0:50e7bcac5eb9 | 122 | rtc_write(MINUTES, ((ntp_minute[0]-0x30)<<4)+(ntp_minute[1]-0x30)); |
jf1vrr | 0:50e7bcac5eb9 | 123 | rtc_write(SECONDS, ((ntp_sec[0]-0x30)<<4)+(ntp_sec[1]-0x30)); |
jf1vrr | 0:50e7bcac5eb9 | 124 | rtc_write(WEEKDAYS, week_val); |
jf1vrr | 0:50e7bcac5eb9 | 125 | rtc_write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz |
jf1vrr | 0:50e7bcac5eb9 | 126 | rtc_write(TIMER_CINTROL, 0x00); |
jf1vrr | 0:50e7bcac5eb9 | 127 | rtc_write(CONTROL1, 0x00); //start |
jf1vrr | 0:50e7bcac5eb9 | 128 | |
jf1vrr | 0:50e7bcac5eb9 | 129 | while(1) { |
jf1vrr | 0:50e7bcac5eb9 | 130 | year = rtc_read(YEARS); |
jf1vrr | 0:50e7bcac5eb9 | 131 | month = rtc_read(MONTHS); |
jf1vrr | 0:50e7bcac5eb9 | 132 | day = rtc_read(DAYS); |
jf1vrr | 0:50e7bcac5eb9 | 133 | week = rtc_read(WEEKDAYS); |
jf1vrr | 0:50e7bcac5eb9 | 134 | hour = rtc_read(HOURS); |
jf1vrr | 0:50e7bcac5eb9 | 135 | minute = rtc_read(MINUTES); |
jf1vrr | 0:50e7bcac5eb9 | 136 | sec = rtc_read(SECONDS); |
jf1vrr | 0:50e7bcac5eb9 | 137 | lcd.locate(0,0); |
jf1vrr | 0:50e7bcac5eb9 | 138 | lcd.printf("20%c%c/%c%c/%c%c %s", |
jf1vrr | 0:50e7bcac5eb9 | 139 | ((year >> 4) & 0x03) + 0x30, (year & 0x0F) + 0x30, |
jf1vrr | 0:50e7bcac5eb9 | 140 | ((month >> 4) & 0x01) + 0x30, (month & 0x0F) + 0x30, |
jf1vrr | 0:50e7bcac5eb9 | 141 | ((day >> 4) & 0x03)+ 0x30, (day & 0x0F) + 0x30, |
jf1vrr | 0:50e7bcac5eb9 | 142 | week_chr[week & 0x07]); |
jf1vrr | 0:50e7bcac5eb9 | 143 | lcd.locate(0,1); |
jf1vrr | 0:50e7bcac5eb9 | 144 | lcd.printf("%c%c:%c%c:%c%c", |
jf1vrr | 0:50e7bcac5eb9 | 145 | ((hour >> 4) & 0x03) + 0x30, (hour & 0x0F) + 0x30, |
jf1vrr | 0:50e7bcac5eb9 | 146 | (minute >> 4) + 0x30, (minute & 0x0F) + 0x30, |
jf1vrr | 0:50e7bcac5eb9 | 147 | (sec >> 4) + 0x30, (sec & 0x0F) + 0x30 ); |
jf1vrr | 0:50e7bcac5eb9 | 148 | wait(1.0); |
jf1vrr | 0:50e7bcac5eb9 | 149 | } |
jf1vrr | 0:50e7bcac5eb9 | 150 | } |