The London Hackspace bandwidth meter

Dependencies:   LPD8806 MODSERIAL mbed picojson

See:

vfd.cpp

Committer:
Jasper
Date:
2012-08-23
Revision:
3:7fca72f96711
Child:
4:7087ea3d13c1

File content as of revision 3:7fca72f96711:

/*
 * The VFD code
 *
 * for a futuba M204LD01AA
 * 
 *
 * p27 -> input, busy
 * p28 -> serial data in
 * p29 -> clock 
 * p30 -> latch
 *
 *
 */

#include "mbed.h"
#include "vfd.h"

// InturruptIn busy_intr(p27)

DigitalIn  vfd_p_busy(p27);
DigitalOut vfd_p_data(p28);
DigitalOut vfd_p_clock(p29);
DigitalOut vfd_p_latch(p30);

/* current state */
int  c_data;
bool c_a0;
bool c_blank;

void vfd_shift_clock(void) {
        vfd_p_clock = 0;
        wait_us(100);
        vfd_p_clock = 1;
        wait_us(100);
}

void vfd_doit(int data, int mode, int wr, int ss) {
    int i, bit;
    
    vfd_p_data = 0; // nc
    vfd_shift_clock();
    vfd_p_data = 0; // nc
    vfd_shift_clock();

    vfd_p_data = 1; // reset, 0 = in reset
    vfd_shift_clock();

    vfd_p_data = c_blank; // Blank, 0 = blanked
    vfd_shift_clock();
    
    vfd_p_data = ss; // SS, clockish
    vfd_shift_clock();

    vfd_p_data = 1; // rd
    vfd_shift_clock();

    vfd_p_data = mode; // A0, data/command
    vfd_shift_clock();

    vfd_p_data = wr; // WR
    vfd_shift_clock();

    for (i = 0 ; i < 8; i++) {
        bit = (data & (1 << i)) >> i;
        vfd_p_data = bit;
        vfd_shift_clock();
    }
        
    vfd_p_latch = 0;
    wait_us(10);
    vfd_p_latch = 1;
    wait_us(10);
}

/* mode == 1 if command */
void vfd_send(int data, int mode)
{
    data = data & 0xff;
    c_data = data;
    c_blank = 1;

    if (vfd_p_busy)
        printf("pre, busy: %d\r\n", vfd_p_busy.read());
    
    if (vfd_p_busy)
    {
        wait_ms(4); // should loop, 3.1 ms is max according to the data sheet.
        if (vfd_p_busy)
        {
            printf("still busy :(\r\n");
            return;
        }
    }
    
    /* wr,ss */
    vfd_doit(data, mode, 0, 0);
    wait_us(1);
    vfd_doit(data, mode, 1, 0);
    wait_us(1);
    vfd_doit(data, mode, 1, 1);
    wait_us(1);

    if (vfd_p_busy)
        printf("post1, busy: %d\r\n", vfd_p_busy.read());

    vfd_doit(data, mode, 0, 0);
    wait_ms(1); // loop and wait for not busy

    if (vfd_p_busy)
        printf("post2, busy: %d\r\n", vfd_p_busy.read());

    while (vfd_p_busy) {
        wait_us(250);
        printf("post-while, busy: %d\r\n", vfd_p_busy.read());
    }
}

void vfd_command(int data) {
    vfd_send(data, 1);
}

void vfd_data(int data) {
    vfd_send(data, 0);
}

void vfd_init(void) {
    vfd_data(0x0c); // clear
    vfd_data(0x1b); // esc
    vfd_data(0);    // v pos
    vfd_data(0);    // h pos
}

void vfd_blank(void) {
    /* blank */
}

void vfd_show(void) {
    /* unblank */
}