Network clock using 7 segment.
Dependencies: SNTPClient WIZnetInterface mbed
Fork of mbed_blinky by
Revision 11:8a1ab8deea2b, committed 2016-03-01
- Comitter:
- nanjsk
- Date:
- Tue Mar 01 17:26:46 2016 +0000
- Parent:
- 10:3e42ca878d11
- Child:
- 12:e60d3d6f24e8
- Commit message:
- first
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SNTPClient.lib Tue Mar 01 17:26:46 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/xeon011/code/SNTPClient/#137fc24033c4
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WIZnetInterface.lib Tue Mar 01 17:26:46 2016 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#3b64df29662f
--- a/main.cpp Sun Jan 17 03:25:27 2016 +0000 +++ b/main.cpp Tue Mar 01 17:26:46 2016 +0000 @@ -1,12 +1,109 @@ #include "mbed.h" +#include "segment.h" +#include "EthernetInterface.h" +#include "SNTPClient.h" + +Serial pc(USBTX,USBRX); +EthernetInterface eth; +datetime ntptime; -DigitalOut myled(LED1); +InterruptIn SW1(PA_15); +DigitalOut ledR(LED1); +DigitalOut ledG(LED2); +DigitalOut ledB(LED3); + +struct tm timeinfo; + +uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0xab}; +//char ip_addr[4] = {222,98,173.222}; +//char mask_addr[4] = {255,255,255,0}; +//char gw_addr[4] = {222,98,173,254}; +uint8_t display_mode = 0; -int main() { - while(1) { - myled = 1; - wait(0.2); - myled = 0; - wait(0.2); +void time_set(){ + timeinfo.tm_mon = ntptime.mo-1; + timeinfo.tm_mday = ntptime.dd; + timeinfo.tm_hour = ntptime.hh; + timeinfo.tm_min = ntptime.mm; + timeinfo.tm_sec = ntptime.ss; + timeinfo.tm_year = ntptime.yy-1900; + //printf("%d-%d-%d, %02d:%02d:%02d\r\n", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec); + time_t t =mktime(&timeinfo); + set_time(t); + t = time(NULL); +} + +uint8_t SNTP_Connect(){ + printf("Getting time information by using NTP...\r\n"); + + SNTPClient sntp("time.nist.gov", 40); // timezone: Korea, Republic of + sntp.connect(); + if(sntp.getTime(&ntptime) == true) + { + printf("%d-%d-%d, %02d:%02d:%02d\r\n", ntptime.yy, ntptime.mo, ntptime.dd, ntptime.hh, ntptime.mm, ntptime.ss); + printf("Completed Get and Set Time\r\n\r\n"); + ledR = 0; + return 0; + } + else + { + while(sntp.getTime(&ntptime) == true) + { + break; + } + return 1; } } + +void SW1_Interrupt(){ + ledG=!ledG; + display_mode = display_mode + 1; + if(display_mode == 2) display_mode = 0; + pc.printf("Display Mode : %d\n\r",display_mode); +} + +int main() { + pc.baud(115200); + wait(0.5f); + printf("Hello WIZwiki-W7500!\n\r"); + printf("===========================================\n\r"); + ledR = ledG = ledB = 1; + + Seg_Reset(); + + eth.init(mac_addr); //Use DHCP + //eth.init(mac_addr, ip_addr, mask_addr, gw_addr); + printf("Check Ethernet Link\r\n"); + while(1) //Wait link up + { + if(eth.link() == true) + break; + } + printf("Link up\r\n"); + eth.connect(); + printf("My IP Address is %s\r\n", eth.getIPAddress()); + + //printf("SNTP_Connect : %d\r\n",SNTP_Connect()); + while(SNTP_Connect()); + time_set(); + + char ymd_buffer[8]; + char hms_buffer[6]; + + SW1.fall(&SW1_Interrupt); + while(1) { + time_t seconds = time(NULL); + //ymd_buffer[0]~[3] : Year + //ymd_buffer[4]~[5] : Month + //ymd_buffer[6]~[7] : Day + strftime(ymd_buffer, 8, "%Y%m%d%\n\r", localtime(&seconds)); + //hms_buffer[0]~[1] : Hour + //hms_buffer[2]~[3] : Minute + //hms_buffer[4]~[5] : Second + strftime(hms_buffer, 6, "%H%M%S\n\r", localtime(&seconds)); + + if(display_mode == 0) display_hms(hms_buffer); + else if(display_mode == 1) display_ymd(ymd_buffer); + else display_hms(hms_buffer); + } +} \ No newline at end of file
--- a/mbed.bld Sun Jan 17 03:25:27 2016 +0000 +++ b/mbed.bld Tue Mar 01 17:26:46 2016 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/6f327212ef96 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/252557024ec3 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/segment.cpp Tue Mar 01 17:26:46 2016 +0000 @@ -0,0 +1,108 @@ +#include "mbed.h" +#include "segment.h" +Serial pc1(USBTX,USBRX); + +BusOut Seg_num(D0,D1,D2,D3,D4,D5,D6,D7); +BusOut Seg_Sel(D8,D9,D10,D11,D12,D13,D14,D15); + +uint8_t SEGMENTS[37] = { + //********************************************************* + // 7segment pattern, + //********************************************************* + // seg: g f e d c b a + // bit: 6 5 4 3 2 1 0 + // -------------------- + 0xC0, // 0 1 0 0 0 0 0 0 + 0xF9, // 1 1 1 1 1 0 0 1 + 0xA4, // 2 0 1 0 0 1 0 0 + 0xB0, // 3 0 1 1 0 0 0 0 + 0x99, // 4 0 0 1 1 0 0 1 + 0x92, // 5 0 0 1 0 0 1 0 + 0x82, // 6 0 0 0 0 0 1 0 + 0xD8, // 7 1 0 1 1 0 0 0 + 0x80, // 8 0 0 0 0 0 0 0 + 0x90, // 9 0 0 1 0 0 0 0 + 0x88, // A 0 0 0 1 0 0 0 + 0x83, // B 0 0 0 0 0 1 1 + 0xC6, // C 1 0 0 0 1 1 0 + 0xA1, // D 0 1 0 0 0 0 1 + 0x86, // E 0 0 0 0 1 1 0 + 0x8E, // F 0 0 0 1 1 1 0 + 0xC2, // G 1 0 0 0 0 1 0 + 0x8B, // H 0 0 0 1 0 1 1 + 0xCF, // I 1 0 0 1 1 1 1 + 0xE1, // J 1 1 0 0 0 0 1 + 0x8A, // K 0 0 0 1 0 1 0 + 0xC7, // L 1 0 0 0 1 1 1 + 0xEA, // M 1 1 0 1 0 1 0 + 0xAB, // N 0 1 0 1 0 1 1 + 0xA3, // O 0 1 0 0 0 1 1 + 0x8C, // P 0 0 0 1 1 0 0 + 0x98, // Q 0 0 1 1 0 0 0 + 0xCC, // R 1 0 0 1 1 0 0 + 0x96, // S 0 0 1 0 1 1 0 + 0x87, // T 0 0 0 0 1 1 1 + 0xE3, // U 1 1 0 0 0 1 1 + 0xC1, // V 1 0 0 0 0 0 1 + 0xD5, // W 1 0 1 0 1 0 1 + 0x89, // X 0 0 0 1 0 0 1 + 0x91, // Y 0 0 1 0 0 0 1 + 0xB4, // Z 0 1 1 0 1 0 0 + 0xFF // 1 1 1 1 1 1 1 + }; + +uint8_t SEGMENT_SEL[9] = {0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; + +void Seg_Drive(int seg, int num, int dot) { + Seg_num = 0xff; + Seg_Sel = SEGMENT_SEL[seg]; + if(dot == 1){ + Seg_num = SEGMENTS[num] & 0x7F; + } + else { + Seg_num = SEGMENTS[num]; + } + wait_ms(1); +} + +void Seg_Test(){ + for(int j = 1 ; j <9 ; j++){ + pc1.printf("SEGMENT_SEL : %x\r\n",SEGMENT_SEL[j]); + for(int i = 0; i<10 ; i++){ + Seg_Drive(j,i,1); + pc1.printf("Seg Num : %d\r\n",i); + wait(0.05); + } + } + Seg_num = 0xff; +} + +void Seg_Reset(){ + Seg_num = 0xff; + Seg_Sel = 0x00; +} + +void display_ymd(char buffer[]){ + int dot = 0; + for(int i = 0 ; i <= 7 ; i++){ + if(i == 3) dot = 1; + else if(i == 5) dot = 1; + else if(i == 7) dot = 1; + else dot = 0; + buffer[i] -= 48; + Seg_Drive(i+1,buffer[i],dot); + } + +} + +void display_hms(char buffer[]){ + int dot = 0; + for(int i = 0 ; i < 6 ; i++){ + if(i == 1) dot = 1; + else if(i == 3) dot = 1; + else if(i == 5) dot = 1; + else dot = 0; + buffer[i] -= 48; + Seg_Drive(i+2,buffer[i],dot); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/segment.h Tue Mar 01 17:26:46 2016 +0000 @@ -0,0 +1,6 @@ + +void Seg_Drive(int seg, int num, int dot); +void Seg_Test(); +void Seg_Reset(); +void display_ymd(char buffer[]); +void display_hms(char buffer[]); \ No newline at end of file