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:
1:abc0162903bf
Parent:
0:3ba54e2bf65d
Child:
2:f33d74cebc4c
--- a/main.cpp	Sat Nov 13 23:42:18 2010 +0000
+++ b/main.cpp	Wed Nov 24 22:12:52 2010 +0000
@@ -8,10 +8,15 @@
 // 4 - red - /STB - active low device select - p8
 // 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(p8); // chip select (active low)
 
+// button (push)
+DigitalIn set_the_time_button (p30); // “set the time” button (active low, pullup resistor)
+
 void DATA(unsigned char displaying) {
     // Select the device by setting chip select low
     cs = 0;
@@ -39,24 +44,10 @@
     COM(0x0c); //set display on,cursor on,blinking off
 }
 
-// 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
-
+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
-    struct tm t;
+
     //year
     printf("Enter year:\n"); // ask to enter year
     printf("YYYY[enter]\n"); // as YYYY
@@ -95,12 +86,49 @@
     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 ((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"?
@@ -116,20 +144,6 @@
             }
         }
 
-        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
-
         COM(0xc0); // second line
         for (i=0; i<2; i++) {
             DATA(timeStringBottom1[i]); // step through the first part of the bottom line