Ian Tindale / Mbed 2 deprecated SPIVFDclock

Dependencies:   EthernetNetIf mbed DNSResolver NTPClientMin

main.cpp

Committer:
u0421793
Date:
2010-11-24
Revision:
1:abc0162903bf
Parent:
0:3ba54e2bf65d
Child:
2:f33d74cebc4c

File content as of revision 1:abc0162903bf:

#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 - 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;
    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);
    }

}