Simply Clock

Dependencies:   EthernetNetIf NTPClient_NetServices TextLCD mbed

Committer:
soramimi
Date:
Mon Mar 28 21:49:06 2011 +0000
Revision:
1:7974d199ed39
Parent:
0:7e901efc73e7

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
soramimi 0:7e901efc73e7 1 #include "mbed.h"
soramimi 0:7e901efc73e7 2 #include "time.h"
soramimi 0:7e901efc73e7 3 #include "TextLCD.h"
soramimi 0:7e901efc73e7 4 #include "EthernetNetIf.h"
soramimi 0:7e901efc73e7 5 #include "NTPClient.h"
soramimi 0:7e901efc73e7 6
soramimi 0:7e901efc73e7 7 #define TIMEZONE (9 * 60 * 60)
soramimi 0:7e901efc73e7 8
soramimi 0:7e901efc73e7 9 TextLCD lcd(p21, p22, p23, p24, p25, p26);
soramimi 0:7e901efc73e7 10
soramimi 0:7e901efc73e7 11 EthernetNetIf eth;
soramimi 0:7e901efc73e7 12 NTPClient ntp;
soramimi 0:7e901efc73e7 13
soramimi 0:7e901efc73e7 14 unsigned long convert_gregorian_to_julian(int year, int month, int day)
soramimi 0:7e901efc73e7 15 {
soramimi 1:7974d199ed39 16 if (month < 3) {
soramimi 1:7974d199ed39 17 month += 9;
soramimi 1:7974d199ed39 18 year--;
soramimi 1:7974d199ed39 19 } else {
soramimi 1:7974d199ed39 20 month -= 3;
soramimi 1:7974d199ed39 21 }
soramimi 1:7974d199ed39 22 year += 4800;
soramimi 1:7974d199ed39 23 int c = year / 100;
soramimi 1:7974d199ed39 24 return c * 146097 / 4 + (year - c * 100) * 1461 / 4 + (153 * month + 2) / 5 + day - 32045;
soramimi 0:7e901efc73e7 25 }
soramimi 0:7e901efc73e7 26
soramimi 0:7e901efc73e7 27 void convert_julian_to_gregorian(unsigned long j, int *year, int *month, int *day)
soramimi 0:7e901efc73e7 28 {
soramimi 1:7974d199ed39 29 int y, m, d;
soramimi 1:7974d199ed39 30 y = (j * 4 + 128179) / 146097;
soramimi 1:7974d199ed39 31 d = (j * 4 - y * 146097 + 128179) / 4 * 4 + 3;
soramimi 1:7974d199ed39 32 j = d / 1461;
soramimi 1:7974d199ed39 33 d = (d - j * 1461) / 4 * 5 + 2;
soramimi 1:7974d199ed39 34 m = d / 153;
soramimi 1:7974d199ed39 35 d = (d - m * 153) / 5 + 1;
soramimi 1:7974d199ed39 36 y = (y - 48) * 100 + j;
soramimi 1:7974d199ed39 37 if (m < 10) {
soramimi 1:7974d199ed39 38 m += 3;
soramimi 1:7974d199ed39 39 } else {
soramimi 1:7974d199ed39 40 m -= 9;
soramimi 1:7974d199ed39 41 y++;
soramimi 1:7974d199ed39 42 }
soramimi 1:7974d199ed39 43 *year = y;
soramimi 1:7974d199ed39 44 *month = m;
soramimi 1:7974d199ed39 45 *day = d;
soramimi 0:7e901efc73e7 46 }
soramimi 0:7e901efc73e7 47
soramimi 0:7e901efc73e7 48 void display(int year, int month, int day, int hour, int minute, int second)
soramimi 0:7e901efc73e7 49 {
soramimi 0:7e901efc73e7 50 lcd.cls();
soramimi 0:7e901efc73e7 51 lcd.printf("%04u-%02u-%02u\n", year, month, day);
soramimi 0:7e901efc73e7 52 lcd.printf("%02u:%02u:%02u\n", hour, minute, second);
soramimi 0:7e901efc73e7 53 }
soramimi 0:7e901efc73e7 54
soramimi 0:7e901efc73e7 55 int main()
soramimi 0:7e901efc73e7 56 {
soramimi 0:7e901efc73e7 57 eth.setup();
soramimi 0:7e901efc73e7 58
soramimi 0:7e901efc73e7 59 Host server(IpAddr(), 123, "ntp.jst.mfeed.ad.jp");
soramimi 1:7974d199ed39 60
soramimi 0:7e901efc73e7 61 bool adjust = true;
soramimi 0:7e901efc73e7 62
soramimi 0:7e901efc73e7 63 double last = 0;
soramimi 0:7e901efc73e7 64
soramimi 0:7e901efc73e7 65 while (1) {
soramimi 0:7e901efc73e7 66 if (adjust) {
soramimi 0:7e901efc73e7 67 ntp.setTime(server);
soramimi 0:7e901efc73e7 68 adjust = false;
soramimi 1:7974d199ed39 69 } else {
soramimi 1:7974d199ed39 70 wait(0.1);
soramimi 0:7e901efc73e7 71 }
soramimi 1:7974d199ed39 72
soramimi 0:7e901efc73e7 73 time_t t = time(0);
soramimi 0:7e901efc73e7 74 double s = t + 2440588.0 * 24 * 60 * 60; // chronological julian second
soramimi 0:7e901efc73e7 75
soramimi 0:7e901efc73e7 76 if (s > last) {
soramimi 0:7e901efc73e7 77 last = s;
soramimi 0:7e901efc73e7 78
soramimi 0:7e901efc73e7 79 s += TIMEZONE;
soramimi 0:7e901efc73e7 80
soramimi 0:7e901efc73e7 81 unsigned long cjd = (unsigned long)(s / (24 * 60 * 60)); // chronological julian day
soramimi 0:7e901efc73e7 82 int year, month, day, hour, minute, second;
soramimi 0:7e901efc73e7 83 convert_julian_to_gregorian(cjd, &year, &month, &day);
soramimi 0:7e901efc73e7 84 second = (int)fmod(s, 24 * 60 * 60);
soramimi 0:7e901efc73e7 85 hour = second / (60 * 60);
soramimi 0:7e901efc73e7 86 minute = second / 60 % 60;
soramimi 0:7e901efc73e7 87 second %= 60;
soramimi 0:7e901efc73e7 88
soramimi 0:7e901efc73e7 89 display(year, month, day, hour, minute, second);
soramimi 0:7e901efc73e7 90
soramimi 1:7974d199ed39 91 if (minute == 59 && second == 30) {
soramimi 0:7e901efc73e7 92 adjust = true;
soramimi 0:7e901efc73e7 93 }
soramimi 0:7e901efc73e7 94 }
soramimi 0:7e901efc73e7 95 }
soramimi 0:7e901efc73e7 96
soramimi 0:7e901efc73e7 97 return 0;
soramimi 0:7e901efc73e7 98 }
soramimi 0:7e901efc73e7 99