The London Hackspace bandwidth meter
Dependencies: LPD8806 MODSERIAL mbed picojson
See:
- https://github.com/londonhackspace/net-o-meter
- http://wiki.london.hackspace.org.uk/view/Projects/BandwidthMeter
vfd.cpp@3:7fca72f96711, 2012-08-23 (annotated)
- Committer:
- Jasper
- Date:
- Thu Aug 23 00:17:04 2012 +0000
- Revision:
- 3:7fca72f96711
- Child:
- 4:7087ea3d13c1
got the vfd working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jasper | 3:7fca72f96711 | 1 | /* |
Jasper | 3:7fca72f96711 | 2 | * The VFD code |
Jasper | 3:7fca72f96711 | 3 | * |
Jasper | 3:7fca72f96711 | 4 | * for a futuba M204LD01AA |
Jasper | 3:7fca72f96711 | 5 | * |
Jasper | 3:7fca72f96711 | 6 | * |
Jasper | 3:7fca72f96711 | 7 | * p27 -> input, busy |
Jasper | 3:7fca72f96711 | 8 | * p28 -> serial data in |
Jasper | 3:7fca72f96711 | 9 | * p29 -> clock |
Jasper | 3:7fca72f96711 | 10 | * p30 -> latch |
Jasper | 3:7fca72f96711 | 11 | * |
Jasper | 3:7fca72f96711 | 12 | * |
Jasper | 3:7fca72f96711 | 13 | */ |
Jasper | 3:7fca72f96711 | 14 | |
Jasper | 3:7fca72f96711 | 15 | #include "mbed.h" |
Jasper | 3:7fca72f96711 | 16 | #include "vfd.h" |
Jasper | 3:7fca72f96711 | 17 | |
Jasper | 3:7fca72f96711 | 18 | // InturruptIn busy_intr(p27) |
Jasper | 3:7fca72f96711 | 19 | |
Jasper | 3:7fca72f96711 | 20 | DigitalIn vfd_p_busy(p27); |
Jasper | 3:7fca72f96711 | 21 | DigitalOut vfd_p_data(p28); |
Jasper | 3:7fca72f96711 | 22 | DigitalOut vfd_p_clock(p29); |
Jasper | 3:7fca72f96711 | 23 | DigitalOut vfd_p_latch(p30); |
Jasper | 3:7fca72f96711 | 24 | |
Jasper | 3:7fca72f96711 | 25 | /* current state */ |
Jasper | 3:7fca72f96711 | 26 | int c_data; |
Jasper | 3:7fca72f96711 | 27 | bool c_a0; |
Jasper | 3:7fca72f96711 | 28 | bool c_blank; |
Jasper | 3:7fca72f96711 | 29 | |
Jasper | 3:7fca72f96711 | 30 | void vfd_shift_clock(void) { |
Jasper | 3:7fca72f96711 | 31 | vfd_p_clock = 0; |
Jasper | 3:7fca72f96711 | 32 | wait_us(100); |
Jasper | 3:7fca72f96711 | 33 | vfd_p_clock = 1; |
Jasper | 3:7fca72f96711 | 34 | wait_us(100); |
Jasper | 3:7fca72f96711 | 35 | } |
Jasper | 3:7fca72f96711 | 36 | |
Jasper | 3:7fca72f96711 | 37 | void vfd_doit(int data, int mode, int wr, int ss) { |
Jasper | 3:7fca72f96711 | 38 | int i, bit; |
Jasper | 3:7fca72f96711 | 39 | |
Jasper | 3:7fca72f96711 | 40 | vfd_p_data = 0; // nc |
Jasper | 3:7fca72f96711 | 41 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 42 | vfd_p_data = 0; // nc |
Jasper | 3:7fca72f96711 | 43 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 44 | |
Jasper | 3:7fca72f96711 | 45 | vfd_p_data = 1; // reset, 0 = in reset |
Jasper | 3:7fca72f96711 | 46 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 47 | |
Jasper | 3:7fca72f96711 | 48 | vfd_p_data = c_blank; // Blank, 0 = blanked |
Jasper | 3:7fca72f96711 | 49 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 50 | |
Jasper | 3:7fca72f96711 | 51 | vfd_p_data = ss; // SS, clockish |
Jasper | 3:7fca72f96711 | 52 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 53 | |
Jasper | 3:7fca72f96711 | 54 | vfd_p_data = 1; // rd |
Jasper | 3:7fca72f96711 | 55 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 56 | |
Jasper | 3:7fca72f96711 | 57 | vfd_p_data = mode; // A0, data/command |
Jasper | 3:7fca72f96711 | 58 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 59 | |
Jasper | 3:7fca72f96711 | 60 | vfd_p_data = wr; // WR |
Jasper | 3:7fca72f96711 | 61 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 62 | |
Jasper | 3:7fca72f96711 | 63 | for (i = 0 ; i < 8; i++) { |
Jasper | 3:7fca72f96711 | 64 | bit = (data & (1 << i)) >> i; |
Jasper | 3:7fca72f96711 | 65 | vfd_p_data = bit; |
Jasper | 3:7fca72f96711 | 66 | vfd_shift_clock(); |
Jasper | 3:7fca72f96711 | 67 | } |
Jasper | 3:7fca72f96711 | 68 | |
Jasper | 3:7fca72f96711 | 69 | vfd_p_latch = 0; |
Jasper | 3:7fca72f96711 | 70 | wait_us(10); |
Jasper | 3:7fca72f96711 | 71 | vfd_p_latch = 1; |
Jasper | 3:7fca72f96711 | 72 | wait_us(10); |
Jasper | 3:7fca72f96711 | 73 | } |
Jasper | 3:7fca72f96711 | 74 | |
Jasper | 3:7fca72f96711 | 75 | /* mode == 1 if command */ |
Jasper | 3:7fca72f96711 | 76 | void vfd_send(int data, int mode) |
Jasper | 3:7fca72f96711 | 77 | { |
Jasper | 3:7fca72f96711 | 78 | data = data & 0xff; |
Jasper | 3:7fca72f96711 | 79 | c_data = data; |
Jasper | 3:7fca72f96711 | 80 | c_blank = 1; |
Jasper | 3:7fca72f96711 | 81 | |
Jasper | 3:7fca72f96711 | 82 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 83 | printf("pre, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 3:7fca72f96711 | 84 | |
Jasper | 3:7fca72f96711 | 85 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 86 | { |
Jasper | 3:7fca72f96711 | 87 | wait_ms(4); // should loop, 3.1 ms is max according to the data sheet. |
Jasper | 3:7fca72f96711 | 88 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 89 | { |
Jasper | 3:7fca72f96711 | 90 | printf("still busy :(\r\n"); |
Jasper | 3:7fca72f96711 | 91 | return; |
Jasper | 3:7fca72f96711 | 92 | } |
Jasper | 3:7fca72f96711 | 93 | } |
Jasper | 3:7fca72f96711 | 94 | |
Jasper | 3:7fca72f96711 | 95 | /* wr,ss */ |
Jasper | 3:7fca72f96711 | 96 | vfd_doit(data, mode, 0, 0); |
Jasper | 3:7fca72f96711 | 97 | wait_us(1); |
Jasper | 3:7fca72f96711 | 98 | vfd_doit(data, mode, 1, 0); |
Jasper | 3:7fca72f96711 | 99 | wait_us(1); |
Jasper | 3:7fca72f96711 | 100 | vfd_doit(data, mode, 1, 1); |
Jasper | 3:7fca72f96711 | 101 | wait_us(1); |
Jasper | 3:7fca72f96711 | 102 | |
Jasper | 3:7fca72f96711 | 103 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 104 | printf("post1, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 3:7fca72f96711 | 105 | |
Jasper | 3:7fca72f96711 | 106 | vfd_doit(data, mode, 0, 0); |
Jasper | 3:7fca72f96711 | 107 | wait_ms(1); // loop and wait for not busy |
Jasper | 3:7fca72f96711 | 108 | |
Jasper | 3:7fca72f96711 | 109 | if (vfd_p_busy) |
Jasper | 3:7fca72f96711 | 110 | printf("post2, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 3:7fca72f96711 | 111 | |
Jasper | 3:7fca72f96711 | 112 | while (vfd_p_busy) { |
Jasper | 3:7fca72f96711 | 113 | wait_us(250); |
Jasper | 3:7fca72f96711 | 114 | printf("post-while, busy: %d\r\n", vfd_p_busy.read()); |
Jasper | 3:7fca72f96711 | 115 | } |
Jasper | 3:7fca72f96711 | 116 | } |
Jasper | 3:7fca72f96711 | 117 | |
Jasper | 3:7fca72f96711 | 118 | void vfd_command(int data) { |
Jasper | 3:7fca72f96711 | 119 | vfd_send(data, 1); |
Jasper | 3:7fca72f96711 | 120 | } |
Jasper | 3:7fca72f96711 | 121 | |
Jasper | 3:7fca72f96711 | 122 | void vfd_data(int data) { |
Jasper | 3:7fca72f96711 | 123 | vfd_send(data, 0); |
Jasper | 3:7fca72f96711 | 124 | } |
Jasper | 3:7fca72f96711 | 125 | |
Jasper | 3:7fca72f96711 | 126 | void vfd_init(void) { |
Jasper | 3:7fca72f96711 | 127 | vfd_data(0x0c); // clear |
Jasper | 3:7fca72f96711 | 128 | vfd_data(0x1b); // esc |
Jasper | 3:7fca72f96711 | 129 | vfd_data(0); // v pos |
Jasper | 3:7fca72f96711 | 130 | vfd_data(0); // h pos |
Jasper | 3:7fca72f96711 | 131 | } |
Jasper | 3:7fca72f96711 | 132 | |
Jasper | 3:7fca72f96711 | 133 | void vfd_blank(void) { |
Jasper | 3:7fca72f96711 | 134 | /* blank */ |
Jasper | 3:7fca72f96711 | 135 | } |
Jasper | 3:7fca72f96711 | 136 | |
Jasper | 3:7fca72f96711 | 137 | void vfd_show(void) { |
Jasper | 3:7fca72f96711 | 138 | /* unblank */ |
Jasper | 3:7fca72f96711 | 139 | } |
Jasper | 3:7fca72f96711 | 140 | |
Jasper | 3:7fca72f96711 | 141 |