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

Committer:
u0421793
Date:
Fri Aug 05 14:43:18 2011 +0000
Revision:
3:7dcce0aa2535
Parent:
2:f33d74cebc4c

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
u0421793 3:7dcce0aa2535 1 #include "mbed.h"
u0421793 3:7dcce0aa2535 2 #include "EthernetNetIf.h" // ethernet
u0421793 3:7dcce0aa2535 3 #include "NTPClient.h" // Network Time Protocol client
u0421793 3:7dcce0aa2535 4
u0421793 3:7dcce0aa2535 5 // rudimentary driving of a 20T202DA2JA vacuum fluorescent display, 20 chars x 2 lines
u0421793 3:7dcce0aa2535 6 // display has 5 pins
u0421793 3:7dcce0aa2535 7 // 1 - green - GND
u0421793 3:7dcce0aa2535 8 // 2 - yellow - Vcc
u0421793 3:7dcce0aa2535 9 // 3 - orange - SIO - serial data - MOSI - p5
u0421793 3:7dcce0aa2535 10 // 4 - red - /STB - active low device select - p10
u0421793 3:7dcce0aa2535 11 // 5 - brown - SCK - serial clock - sclk - p7
u0421793 3:7dcce0aa2535 12
u0421793 3:7dcce0aa2535 13 // Also, I have fitted a battery on my breadboard to the RTC, to keep the time across power downs.
u0421793 3:7dcce0aa2535 14
u0421793 3:7dcce0aa2535 15 // VFD
u0421793 3:7dcce0aa2535 16 SPI spi(p5, p6, p7); // mosi, miso, sclk (miso not used by display)
u0421793 3:7dcce0aa2535 17 DigitalOut cs(p10); // chip select (active low)
u0421793 3:7dcce0aa2535 18
u0421793 3:7dcce0aa2535 19 // alternatively, look on the ethernet interface to find an NTP server and set the time from that.
u0421793 3:7dcce0aa2535 20 EthernetNetIf eth; // instantiate an ethernet connection
u0421793 3:7dcce0aa2535 21 NTPClient ntp; // instantiate an NTP client
u0421793 3:7dcce0aa2535 22
u0421793 3:7dcce0aa2535 23 // summertime
u0421793 3:7dcce0aa2535 24 int summertime = true; // if this is true, then it's summer!
u0421793 3:7dcce0aa2535 25
u0421793 3:7dcce0aa2535 26 void DATA(unsigned char displaying) {
u0421793 3:7dcce0aa2535 27 // Select the device by setting chip select low
u0421793 3:7dcce0aa2535 28 cs = 0;
u0421793 3:7dcce0aa2535 29 spi.write(0xfa);
u0421793 3:7dcce0aa2535 30 spi.write(displaying);
u0421793 3:7dcce0aa2535 31 // Deselect the device
u0421793 3:7dcce0aa2535 32 cs = 1;
u0421793 3:7dcce0aa2535 33 }
u0421793 3:7dcce0aa2535 34
u0421793 3:7dcce0aa2535 35 void COM(unsigned char telling) {
u0421793 3:7dcce0aa2535 36 // Select the device by setting chip select low
u0421793 3:7dcce0aa2535 37 cs = 0;
u0421793 3:7dcce0aa2535 38 spi.write(0xf8);
u0421793 3:7dcce0aa2535 39 spi.write(telling);
u0421793 3:7dcce0aa2535 40 // Deselect the device
u0421793 3:7dcce0aa2535 41 cs = 1;
u0421793 3:7dcce0aa2535 42 }
u0421793 3:7dcce0aa2535 43
u0421793 3:7dcce0aa2535 44 int i = 0; // initalise the character steppers
u0421793 3:7dcce0aa2535 45
u0421793 3:7dcce0aa2535 46 void INITIALISE_DISPLAY() {
u0421793 3:7dcce0aa2535 47 COM(0x01); //clear all display and set DD-RAM address 0 in address counter
u0421793 3:7dcce0aa2535 48 COM(0x02); //move cursor to the original position
u0421793 3:7dcce0aa2535 49 COM(0x06); //set the cursor direction increment and cursor shift enabled
u0421793 3:7dcce0aa2535 50 COM(0x38); //set 8bit operation,2 line display and 100% brightness level
u0421793 3:7dcce0aa2535 51 COM(0x80); //set cursor to the first position of 1st line
u0421793 3:7dcce0aa2535 52 COM(0x0c); //set display on,cursor on,blinking off
u0421793 3:7dcce0aa2535 53 }
u0421793 3:7dcce0aa2535 54
u0421793 3:7dcce0aa2535 55 void printVFDLine1(char statusString[21]) {
u0421793 3:7dcce0aa2535 56 COM(0x80); // first line
u0421793 3:7dcce0aa2535 57 for (i=0; i<20; i++) {
u0421793 3:7dcce0aa2535 58 DATA(statusString[i]); // step through the top line
u0421793 3:7dcce0aa2535 59 }
u0421793 3:7dcce0aa2535 60 }
u0421793 3:7dcce0aa2535 61
u0421793 3:7dcce0aa2535 62 void printVFDLine2(char statusString[21]) {
u0421793 3:7dcce0aa2535 63 COM(0xc0); // second line
u0421793 3:7dcce0aa2535 64 for (i=0; i<20; i++) {
u0421793 3:7dcce0aa2535 65 DATA(statusString[i]); // step through the first part of the bottom line
u0421793 3:7dcce0aa2535 66 }
u0421793 3:7dcce0aa2535 67 }
u0421793 3:7dcce0aa2535 68
u0421793 3:7dcce0aa2535 69 char timeStringTop[21]; // used with the NTP code in the first line display code
u0421793 3:7dcce0aa2535 70 char timeStringBottom[21]; // used with the NTP code in the second line display code
u0421793 3:7dcce0aa2535 71 char statusString[41]; // place to put startup status text, etc
u0421793 3:7dcce0aa2535 72
u0421793 3:7dcce0aa2535 73 int main() {
u0421793 3:7dcce0aa2535 74 // Setup the spi for 8 bit data, high steady state clock,
u0421793 3:7dcce0aa2535 75 // second edge capture, with a 1MHz clock rate
u0421793 3:7dcce0aa2535 76 spi.format(8,3); // set SPI mode
u0421793 3:7dcce0aa2535 77 spi.frequency(1000000);
u0421793 3:7dcce0aa2535 78
u0421793 3:7dcce0aa2535 79 // Ethernet and NTP stuff begins (from http://mbed.org/cookbook/NTP-Client )
u0421793 3:7dcce0aa2535 80 INITIALISE_DISPLAY();
u0421793 3:7dcce0aa2535 81 char statusString1[21] = "Start ";
u0421793 3:7dcce0aa2535 82 printVFDLine1(statusString1);
u0421793 3:7dcce0aa2535 83 char statusString2[21] = "Setting up... ";
u0421793 3:7dcce0aa2535 84 printVFDLine2(statusString2);
u0421793 3:7dcce0aa2535 85
u0421793 3:7dcce0aa2535 86 EthernetErr ethErr = eth.setup();
u0421793 3:7dcce0aa2535 87 if (ethErr) {
u0421793 3:7dcce0aa2535 88 printf("Error %d in setup.\n\r", ethErr);
u0421793 3:7dcce0aa2535 89
u0421793 3:7dcce0aa2535 90 char statusString3[21] = "Error %d in setup.\n\r";
u0421793 3:7dcce0aa2535 91 printVFDLine1(statusString3);
u0421793 3:7dcce0aa2535 92 return -1;
u0421793 3:7dcce0aa2535 93 }
u0421793 3:7dcce0aa2535 94
u0421793 3:7dcce0aa2535 95 Host server(IpAddr(), 123, "0.uk.pool.ntp.org");
u0421793 3:7dcce0aa2535 96 ntp.setTime(server);
u0421793 3:7dcce0aa2535 97
u0421793 3:7dcce0aa2535 98 char statusString4[21] = "0.uk.pool.ntp.org ";
u0421793 3:7dcce0aa2535 99 printVFDLine1(statusString4);
u0421793 3:7dcce0aa2535 100 char statusString5[21] = "Setup OK ";
u0421793 3:7dcce0aa2535 101 printVFDLine2(statusString5);
u0421793 3:7dcce0aa2535 102
u0421793 3:7dcce0aa2535 103 // end of Ethernet and NTP stuff
u0421793 3:7dcce0aa2535 104
u0421793 3:7dcce0aa2535 105 INITIALISE_DISPLAY(); // call the function that initialises the VFD
u0421793 3:7dcce0aa2535 106
u0421793 3:7dcce0aa2535 107 while (1) {
u0421793 3:7dcce0aa2535 108
u0421793 3:7dcce0aa2535 109 time_t rtc_seconds = time(NULL); // somehow gets the current amount of seconds, evah!
u0421793 3:7dcce0aa2535 110
u0421793 3:7dcce0aa2535 111 if (summertime) { // if the summertime variable is true, we add 3600 seconds to rtc_seconds
u0421793 3:7dcce0aa2535 112 rtc_seconds = rtc_seconds + 3600;
u0421793 3:7dcce0aa2535 113 }
u0421793 3:7dcce0aa2535 114
u0421793 3:7dcce0aa2535 115 // build the two lines of the display separately
u0421793 3:7dcce0aa2535 116 strftime(timeStringTop, 21, "%a %d %b %T ", localtime(&rtc_seconds)); // put the hours:minutes:seconds, then full day, then am/pm
u0421793 3:7dcce0aa2535 117 printVFDLine1(timeStringTop);
u0421793 3:7dcce0aa2535 118
u0421793 3:7dcce0aa2535 119 // char thingStringBottom[21] = "This is a blank line";
u0421793 3:7dcce0aa2535 120 // printVFDLine2(thingStringBottom);
u0421793 3:7dcce0aa2535 121
u0421793 3:7dcce0aa2535 122 wait(1);
u0421793 3:7dcce0aa2535 123
u0421793 3:7dcce0aa2535 124 }
u0421793 3:7dcce0aa2535 125
u0421793 0:3ba54e2bf65d 126 }