Network clock using 7 segment. add SHT11

Dependencies:   SHTx SNTPClient WIZnetInterface mbed

Files at this revision

API Documentation at this revision

Comitter:
nanjsk
Date:
Tue Apr 05 11:50:51 2016 +0000
Commit message:
first

Changed in this revision

SHTx.lib Show annotated file Show diff for this revision Revisions of this file
SNTPClient.lib Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
segment.cpp Show annotated file Show diff for this revision Revisions of this file
segment.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 179f2315915f SHTx.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SHTx.lib	Tue Apr 05 11:50:51 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/NegativeBlack/code/SHTx/#8465801be23f
diff -r 000000000000 -r 179f2315915f SNTPClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SNTPClient.lib	Tue Apr 05 11:50:51 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/xeon011/code/SNTPClient/#137fc24033c4
diff -r 000000000000 -r 179f2315915f WIZnetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnetInterface.lib	Tue Apr 05 11:50:51 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#c91884bd2713
diff -r 000000000000 -r 179f2315915f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Apr 05 11:50:51 2016 +0000
@@ -0,0 +1,187 @@
+#include "mbed.h"
+#include "segment.h"
+#include "EthernetInterface.h"
+#include "SNTPClient.h"
+#include "SHTx/sht15.hpp"
+
+Serial pc(USBTX,USBRX);
+EthernetInterface eth;
+datetime ntptime;
+
+InterruptIn SW1(P21);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+
+SHTx::SHT15 sensor(P22, P23);
+
+void SW1_Interrupt();
+void SNTP_Connect();
+void time_set();
+void time_update(char buffer[]);
+float update_sht11(uint8_t sensor_sel);
+
+struct tm timeinfo;
+
+uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x11, 0x22, 0xab};
+uint8_t display_mode = 0;
+uint8_t update_mode = 0;
+uint8_t Set_update[3] = {18, 00, 00};   //Hour,Minute,Second
+uint8_t SW_toggle = 0;
+
+void SW1_Interrupt(){
+    //led2=!led2;
+    display_mode = display_mode + 1;
+        
+    if(display_mode == 4)    display_mode = 0;
+    printf("Display Mode : %d\n\r",display_mode);
+}
+
+void 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");
+          led1 = 0;
+    }
+    else {
+        while(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");
+            led1 = 0;
+            break;
+        }
+    }
+}
+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;
+    time_t t =mktime(&timeinfo);
+    set_time(t);
+    t = time(NULL);
+}
+
+void time_update(char buffer[]) {
+    uint8_t h_buffer = ((buffer[0]-48)*10) + (buffer[1]-48);
+    uint8_t m_buffer = ((buffer[2]-48)*10) + (buffer[3]-48);
+    uint8_t s_buffer = ((buffer[4]-48)*10) + (buffer[5]-48);
+
+    if(update_mode == 0){
+        if(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2]){
+            SNTP_Connect();
+            time_set();
+            update_mode = 1;
+            printf("Time Update Completed.\n\r\n\r");
+        }
+    }
+    else if(update_mode == 1){
+        if(!(h_buffer == Set_update[0] && m_buffer == Set_update[1] && s_buffer == Set_update[2])){
+            update_mode = 0;
+        }
+    }
+}
+float update_sht11(uint8_t sensor_sel){
+    sensor.update();
+    sensor.setScale(false);
+    if(sensor_sel == 0) return sensor.getTemperature();
+    else if(sensor_sel == 1) return sensor.getHumidity();
+}
+
+int main() {    
+    pc.baud(115200);
+    wait(0.1);
+    printf("\n\rHello WIZwiki-W7500!\n\r");
+    printf("===========================================\n\r");
+    led1 = led2 = 1;
+    
+    Seg_Reset();
+    SW1.fall(&SW1_Interrupt);
+    
+    sensor.setOTPReload(false);
+    sensor.setResolution(true); 
+
+    eth.init(mac_addr); //Use DHCP
+    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());
+    
+    SNTP_Connect();
+    time_set();
+    
+    //ymd_buffer[0]~[3] : Year
+    //ymd_buffer[4]~[5] : Month
+    //ymd_buffer[6]~[7] : Day
+    char ymd_buffer[8];
+    //hms_buffer[0]~[1] : Hour
+    //hms_buffer[2]~[3] : Minute
+    //hms_buffer[4]~[5] : Second
+    char hms_buffer[6];
+    
+    float temp_val = 0;
+    float humi_val = 0;
+    int temp_mode = 0;
+    int humi_mode = 0;
+    
+    while(1) {
+        time_t seconds = time(NULL);
+        
+        strftime(hms_buffer, 6, "%H%M%S\n\r", localtime(&seconds));
+        strftime(ymd_buffer, 8, "%Y%m%d%\n\r", localtime(&seconds));
+        
+        if(hms_buffer[4] == '0' && hms_buffer[5] == '0'){
+            time_update(hms_buffer);
+        }
+        if(display_mode == 0) {
+            SW_toggle = 0;
+            display_hms(hms_buffer);
+        }
+        else if(display_mode == 1) {
+            SW_toggle = 0;
+            display_ymd(ymd_buffer);
+        }
+        else if(display_mode == 2) {
+            if(temp_mode == 0){
+                if(hms_buffer[5]%5 == 0){
+                    temp_val = update_sht11(0);
+                    temp_mode = 1;
+                }
+            }
+            if(temp_mode == 1){
+                if(hms_buffer[5]%5 != 0){
+                    temp_mode = 0;
+                }
+            }
+            display_temp(temp_val);
+        }
+        else if(display_mode == 3) {
+            if(humi_mode == 0){
+                if(hms_buffer[5]%5 == 0){
+                    humi_val = update_sht11(1);
+                    humi_mode = 1;
+                }
+            }
+            if(temp_mode == 1){
+                if(hms_buffer[5]%5 != 0){
+                    humi_mode = 0;
+                }
+            }
+            display_humi(humi_val);
+        }
+        else {
+            display_hms(hms_buffer);
+        }
+        //Seg_Test_2();
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 179f2315915f mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Apr 05 11:50:51 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/252557024ec3
\ No newline at end of file
diff -r 000000000000 -r 179f2315915f segment.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/segment.cpp	Tue Apr 05 11:50:51 2016 +0000
@@ -0,0 +1,164 @@
+#include "mbed.h"
+#include "segment.h"
+Serial pc1(USBTX,USBRX);
+
+BusOut Seg_num(P5,P6,P7,P8,P9,P10,P11,P12);
+BusOut Seg_Sel(P20,P19,P18,P17,P16,P15,P14,P13);
+
+
+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
+        0x89,        // 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_1()
+{
+    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_Test_2()
+{
+    Seg_num = 0x00;
+    Seg_Sel = 0xff;
+    wait(0.5);
+    Seg_num = 0xff;
+    Seg_Sel = 0x00;
+    wait(0.5);
+}
+
+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);
+    }
+}
+
+void display_temp(float temp_val){
+    //Seg_Reset();
+    int temp_val_int = (int)(temp_val*10);
+    char temp_val_arr[5]={0,0,0,36,12};
+    
+    temp_val_arr[0] = temp_val_int/100;    
+    temp_val_arr[1] = (temp_val_int-(temp_val_arr[0]*100))/10;    
+    temp_val_arr[2] = temp_val_int-((temp_val_arr[0]*100)+(temp_val_arr[1]*10));
+    
+    int dot = 0;
+    for(int i = 0 ; i < 5 ; i++)
+    {
+        if(i == 1)      dot = 1;
+        else    dot = 0;
+        Seg_Drive(i+3,temp_val_arr[i],dot);
+    }
+}
+void display_humi(float humi_val){
+    //Seg_Reset();
+    int humi_val_int = (int)(humi_val*10);
+    char humi_val_arr[5]={0,0,0,36,17};
+    
+    humi_val_arr[0] = humi_val_int/100;    
+    humi_val_arr[1] = (humi_val_int-(humi_val_arr[0]*100))/10;    
+    humi_val_arr[2] = humi_val_int-((humi_val_arr[0]*100)+(humi_val_arr[1]*10));
+    
+    int dot = 0;
+    for(int i = 0 ; i < 5 ; i++)
+    {
+        if(i == 1)      dot = 1;
+        else    dot = 0;
+        Seg_Drive(i+3,humi_val_arr[i],dot);
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 179f2315915f segment.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/segment.h	Tue Apr 05 11:50:51 2016 +0000
@@ -0,0 +1,9 @@
+
+void Seg_Drive(int seg, int num, int dot);
+void Seg_Test_1();
+void Seg_Test_2();
+void Seg_Reset();
+void display_ymd(char buffer[]);
+void display_hms(char buffer[]);
+void display_temp(float temp_val);
+void display_humi(float humi_val);
\ No newline at end of file