Driving a 20T202DA2JA VFD, using the (powered) RTC, pressing a button to ask the user to enter the date details over serial, displaying the date on the VFD. Now with added ethernet and NTP to update the time automatically on startup.

Dependencies:   EthernetNetIf mbed DNSResolver NTPClientMin

Revision:
3:7dcce0aa2535
Parent:
2:f33d74cebc4c
--- a/main.cpp	Sun Dec 12 01:07:29 2010 +0000
+++ b/main.cpp	Fri Aug 05 14:43:18 2011 +0000
@@ -1,161 +1,126 @@
-#include "mbed.h"
-
-// rudimentary driving of a 20T202DA2JA vacuum fluorescent display, 20 chars x 2 lines
-// display has 5 pins
-// 1 - green - GND
-// 2 - yellow - Vcc
-// 3 - orange - SIO - serial data - MOSI - p5
-// 4 - red - /STB - active low device select - p10
-// 5 - brown - SCK - serial clock - sclk - p7
-
-// Also, I have fitted a battery on my breadboard to the RTC, to keep the time across power downs.
-
-// VFD
-SPI spi(p5, p6, p7); // mosi, miso, sclk (miso not used by display)
-DigitalOut cs(p10); // chip select (active low)
-
-// button (push)
-DigitalIn set_the_time_button (p11); // “set the time” button (active low, pullup resistor)
-
-void DATA(unsigned char displaying) {
-    // Select the device by setting chip select low
-    cs = 0;
-    spi.write(0xfa);
-    spi.write(displaying);
-    // Deselect the device
-    cs = 1;
-}
-
-void COM(unsigned char telling) {
-    // Select the device by setting chip select low
-    cs = 0;
-    spi.write(0xf8);
-    spi.write(telling);
-    // Deselect the device
-    cs = 1;
-}
-
-void INITIALISE_DISPLAY() {
-    COM(0x01); //clear all display and set DD-RAM address 0 in address counter
-    COM(0x02); //move cursor to the original position
-    COM(0x06); //set the cursor direction increment and cursor shift enabled
-    COM(0x38); //set 8bit operation,2 line display and 100% brightness level
-    COM(0x80); //set cursor to the first position of 1st line
-    COM(0x0c); //set display on,cursor on,blinking off
-}
-
-tm ASK_FOR_THE_TIME(struct tm t) {
-    // get the current time from the terminal by asking the user to enter
-    // unfortunately, there's no echo back, so they can't see what they type
-
-    //year
-    printf("Enter year:\n"); // ask to enter year
-    printf("YYYY[enter]\n"); // as YYYY
-    scanf("%d", &t.tm_year); // put the typing into here
-
-    //month
-    printf("Enter month:\n"); // ask to enter month
-    printf("MM[enter]\n"); // as MM
-    scanf("%d", &t.tm_mon); // put the typing into here
-
-    //date
-    printf("Enter date:\n"); // ask to enter date
-    printf("DD[enter]\n"); // as DD
-    scanf("%d", &t.tm_mday); // put the typing into here
-
-    //hours
-    printf("Enter hours:\n"); // ask to enter hours
-    printf("HH[enter]\n"); // as HH
-    scanf("%d", &t.tm_hour); // put the typing into here
-
-    //minutes
-    printf("Enter minutes:\n"); // ask to enter minutes
-    printf("MM[enter]\n"); // as MM
-    scanf("%d", &t.tm_min); // put the typing into here
-
-    //seconds
-    printf("Enter seconds:\n"); // ask to enter seconds
-    printf("SS[enter]\n"); // as SS
-    scanf("%d", &t.tm_sec); // put the typing into here
-
-    // adjust for tm structure required values
-    t.tm_year = t.tm_year - 1900;
-    t.tm_mon = t.tm_mon - 1;
-
-    // set the time
-    set_time(mktime(&t));
-    //set_time(1289676600);  // Set RTC time to Sat, 13 Nov 2010 19:30:00 in case of testing
-
-    return t;
-
-}
-// spare display testing string
-// char theString[41] = {"20T202DA2JA VFD driven by mbed using SPI"}; // forty characters max
-char timeStringTop[21], timeStringBottom1[3], timeStringBottom2[17]; // string to put the time representation into
-
-char day_suffix[3] =  "th"; // suffix string for days like 1st 2nd 3rd
-char first[3]= "st", second[3] = "nd", third[3] = "rd"; // holding strings for the suffixes
-
-int main() {
-    // Setup the spi for 8 bit data, high steady state clock,
-    // second edge capture, with a 1MHz clock rate
-    spi.format(8,3); // set SPI mode
-    spi.frequency(1000000);
-
-    INITIALISE_DISPLAY(); // call the function that initialises the VFD
-
-    while (1) {
-        struct tm t;
-
-        if (set_the_time_button == false) {
-            ASK_FOR_THE_TIME(t);
-        }
-
-        time_t rtc_seconds = time(NULL); // somehow gets the current amount of seconds, evah!
-
-        int i = 0; // initalise the character steppers
-
-        // build the two lines of the display separately
-        strftime(timeStringTop, 20, "%X %A %p  ", localtime(&rtc_seconds)); // put the hours:minutes:seconds, then full day, then am/pm
-
-        COM(0x80); // first line
-        for (i=0; i<20; i++) {
-            DATA(timeStringTop[i]); // step through the top line
-        }
-
-        // this builds the second line in three pieces
-        strftime(timeStringBottom1, 2, "%e", localtime(&rtc_seconds)); // put the date in
-        strftime(timeStringBottom2, 16, " %B %Y  ", localtime(&rtc_seconds)); // put a space then the full month then the year then spaces
-
-        // first do a quick check to see if our date needs a 'st'; 'nd'; 'rd' or 'th' suffix
-        if ((t.tm_mday < 10) | (t.tm_mday > 20)) { // if the date is below the 10th or above the 20th
-            switch (t.tm_mday%10) {
-                case 1:
-                    strcpy (day_suffix,first); // is it a day that needs a "st"?
-                    break;
-                case 2:
-                    strcpy (day_suffix,second); // is it a day that needs a "nd"
-                    break;
-                case 3:
-                    strcpy (day_suffix,third); // is it a day that needs a "rd"
-                    break;
-                default:
-                    break; // leave 'day_suffix' saying "th" then.
-            }
-        }
-
-        COM(0xc0); // second line
-        for (i=0; i<2; i++) {
-            DATA(timeStringBottom1[i]); // step through the first part of the bottom line
-        }
-        for (i=0; i<2; i++) {
-            DATA(day_suffix[i]); // then add the day suffix
-        }
-        for (i=0; i<17; i++) {
-            DATA(timeStringBottom2[i]); // finish stepping through the bottom line
-        }
-
-        wait(1);
-    }
-
+#include "mbed.h"
+#include "EthernetNetIf.h" // ethernet 
+#include "NTPClient.h" // Network Time Protocol client
+
+// rudimentary driving of a 20T202DA2JA vacuum fluorescent display, 20 chars x 2 lines
+// display has 5 pins
+// 1 - green - GND
+// 2 - yellow - Vcc
+// 3 - orange - SIO - serial data - MOSI - p5
+// 4 - red - /STB - active low device select - p10
+// 5 - brown - SCK - serial clock - sclk - p7
+
+// Also, I have fitted a battery on my breadboard to the RTC, to keep the time across power downs.
+
+// VFD
+SPI spi(p5, p6, p7); // mosi, miso, sclk (miso not used by display)
+DigitalOut cs(p10); // chip select (active low)
+
+// alternatively, look on the ethernet interface to find an NTP server and set the time from that.
+EthernetNetIf eth; // instantiate an ethernet connection
+NTPClient ntp; // instantiate an NTP client
+
+// summertime
+int summertime = true; // if this is true, then it's summer!
+
+void DATA(unsigned char displaying) {
+    // Select the device by setting chip select low
+    cs = 0;
+    spi.write(0xfa);
+    spi.write(displaying);
+    // Deselect the device
+    cs = 1;
+}
+
+void COM(unsigned char telling) {
+    // Select the device by setting chip select low
+    cs = 0;
+    spi.write(0xf8);
+    spi.write(telling);
+    // Deselect the device
+    cs = 1;
+}
+
+int i = 0; // initalise the character steppers
+
+void INITIALISE_DISPLAY() {
+    COM(0x01); //clear all display and set DD-RAM address 0 in address counter
+    COM(0x02); //move cursor to the original position
+    COM(0x06); //set the cursor direction increment and cursor shift enabled
+    COM(0x38); //set 8bit operation,2 line display and 100% brightness level
+    COM(0x80); //set cursor to the first position of 1st line
+    COM(0x0c); //set display on,cursor on,blinking off
+}
+
+void printVFDLine1(char statusString[21]) {
+    COM(0x80); // first line
+    for (i=0; i<20; i++) {
+        DATA(statusString[i]); // step through the top line
+    }
+}
+
+void printVFDLine2(char statusString[21]) {
+    COM(0xc0); // second line
+    for (i=0; i<20; i++) {
+        DATA(statusString[i]); // step through the first part of the bottom line
+    }
+}
+
+char timeStringTop[21]; // used with the NTP code in the first line display code
+char timeStringBottom[21]; // used with the NTP code in the second line display code
+char statusString[41]; // place to put startup status text, etc
+
+int main() {
+    // Setup the spi for 8 bit data, high steady state clock,
+    // second edge capture, with a 1MHz clock rate
+    spi.format(8,3); // set SPI mode
+    spi.frequency(1000000);
+
+    // Ethernet and NTP stuff begins (from http://mbed.org/cookbook/NTP-Client )
+    INITIALISE_DISPLAY();
+    char statusString1[21] = "Start               ";
+    printVFDLine1(statusString1);
+    char statusString2[21] = "Setting up...       ";
+    printVFDLine2(statusString2);
+
+    EthernetErr ethErr = eth.setup();
+    if (ethErr) {
+        printf("Error %d in setup.\n\r", ethErr);
+
+        char statusString3[21] = "Error %d in setup.\n\r";
+        printVFDLine1(statusString3);
+        return -1;
+    }
+
+    Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
+    ntp.setTime(server);
+
+    char statusString4[21] = "0.uk.pool.ntp.org   ";
+    printVFDLine1(statusString4);
+    char statusString5[21] = "Setup OK            ";
+    printVFDLine2(statusString5);
+
+    // end of Ethernet and NTP stuff
+
+    INITIALISE_DISPLAY(); // call the function that initialises the VFD
+
+    while (1) {
+
+        time_t rtc_seconds = time(NULL); // somehow gets the current amount of seconds, evah!
+
+        if (summertime) { // if the summertime variable is true, we add 3600 seconds to rtc_seconds
+            rtc_seconds = rtc_seconds + 3600;
+        }
+
+        // build the two lines of the display separately
+        strftime(timeStringTop, 21, "%a %d %b %T ", localtime(&rtc_seconds)); // put the hours:minutes:seconds, then full day, then am/pm
+        printVFDLine1(timeStringTop);
+
+        //  char thingStringBottom[21] = "This is a blank line";
+        //  printVFDLine2(thingStringBottom);
+
+        wait(1);
+
+    }
+
 }
\ No newline at end of file