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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h" // ethernet 
00003 #include "NTPClient.h" // Network Time Protocol client
00004 
00005 // rudimentary driving of a 20T202DA2JA vacuum fluorescent display, 20 chars x 2 lines
00006 // display has 5 pins
00007 // 1 - green - GND
00008 // 2 - yellow - Vcc
00009 // 3 - orange - SIO - serial data - MOSI - p5
00010 // 4 - red - /STB - active low device select - p10
00011 // 5 - brown - SCK - serial clock - sclk - p7
00012 
00013 // Also, I have fitted a battery on my breadboard to the RTC, to keep the time across power downs.
00014 
00015 // VFD
00016 SPI spi(p5, p6, p7); // mosi, miso, sclk (miso not used by display)
00017 DigitalOut cs(p10); // chip select (active low)
00018 
00019 // alternatively, look on the ethernet interface to find an NTP server and set the time from that.
00020 EthernetNetIf eth; // instantiate an ethernet connection
00021 NTPClient ntp; // instantiate an NTP client
00022 
00023 // summertime
00024 int summertime = true; // if this is true, then it's summer!
00025 
00026 void DATA(unsigned char displaying) {
00027     // Select the device by setting chip select low
00028     cs = 0;
00029     spi.write(0xfa);
00030     spi.write(displaying);
00031     // Deselect the device
00032     cs = 1;
00033 }
00034 
00035 void COM(unsigned char telling) {
00036     // Select the device by setting chip select low
00037     cs = 0;
00038     spi.write(0xf8);
00039     spi.write(telling);
00040     // Deselect the device
00041     cs = 1;
00042 }
00043 
00044 int i = 0; // initalise the character steppers
00045 
00046 void INITIALISE_DISPLAY() {
00047     COM(0x01); //clear all display and set DD-RAM address 0 in address counter
00048     COM(0x02); //move cursor to the original position
00049     COM(0x06); //set the cursor direction increment and cursor shift enabled
00050     COM(0x38); //set 8bit operation,2 line display and 100% brightness level
00051     COM(0x80); //set cursor to the first position of 1st line
00052     COM(0x0c); //set display on,cursor on,blinking off
00053 }
00054 
00055 void printVFDLine1(char statusString[21]) {
00056     COM(0x80); // first line
00057     for (i=0; i<20; i++) {
00058         DATA(statusString[i]); // step through the top line
00059     }
00060 }
00061 
00062 void printVFDLine2(char statusString[21]) {
00063     COM(0xc0); // second line
00064     for (i=0; i<20; i++) {
00065         DATA(statusString[i]); // step through the first part of the bottom line
00066     }
00067 }
00068 
00069 char timeStringTop[21]; // used with the NTP code in the first line display code
00070 char timeStringBottom[21]; // used with the NTP code in the second line display code
00071 char statusString[41]; // place to put startup status text, etc
00072 
00073 int main() {
00074     // Setup the spi for 8 bit data, high steady state clock,
00075     // second edge capture, with a 1MHz clock rate
00076     spi.format(8,3); // set SPI mode
00077     spi.frequency(1000000);
00078 
00079     // Ethernet and NTP stuff begins (from http://mbed.org/cookbook/NTP-Client )
00080     INITIALISE_DISPLAY();
00081     char statusString1[21] = "Start               ";
00082     printVFDLine1(statusString1);
00083     char statusString2[21] = "Setting up...       ";
00084     printVFDLine2(statusString2);
00085 
00086     EthernetErr ethErr = eth.setup();
00087     if (ethErr) {
00088         printf("Error %d in setup.\n\r", ethErr);
00089 
00090         char statusString3[21] = "Error %d in setup.\n\r";
00091         printVFDLine1(statusString3);
00092         return -1;
00093     }
00094 
00095     Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
00096     ntp.setTime(server);
00097 
00098     char statusString4[21] = "0.uk.pool.ntp.org   ";
00099     printVFDLine1(statusString4);
00100     char statusString5[21] = "Setup OK            ";
00101     printVFDLine2(statusString5);
00102 
00103     // end of Ethernet and NTP stuff
00104 
00105     INITIALISE_DISPLAY(); // call the function that initialises the VFD
00106 
00107     while (1) {
00108 
00109         time_t rtc_seconds = time(NULL); // somehow gets the current amount of seconds, evah!
00110 
00111         if (summertime) { // if the summertime variable is true, we add 3600 seconds to rtc_seconds
00112             rtc_seconds = rtc_seconds + 3600;
00113         }
00114 
00115         // build the two lines of the display separately
00116         strftime(timeStringTop, 21, "%a %d %b %T ", localtime(&rtc_seconds)); // put the hours:minutes:seconds, then full day, then am/pm
00117         printVFDLine1(timeStringTop);
00118 
00119         //  char thingStringBottom[21] = "This is a blank line";
00120         //  printVFDLine2(thingStringBottom);
00121 
00122         wait(1);
00123 
00124     }
00125 
00126 }