Aliexpressなどで販売されている64x32のフルカラードットマトリクスLED2枚とNucleo F401REを利用して、 E233系の駅停車時、路線名表示ありのLED側面行先表示を再現するプログラムです。 3秒間隔、3段階切替で、路線名、種別、行先、次停車駅を個別に指定することが可能です。

Dependencies:   SDFileSystem mbed

Committer:
chirashi
Date:
Sun Oct 19 11:16:58 2014 +0000
Revision:
3:6dbbc0130e96
Parent:
2:c1a9a2a0885d
Child:
4:245f17936b1a
delay

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RRacer 0:1f58ecec51d6 1 /*
RRacer 0:1f58ecec51d6 2 The goal of this program is to show the basic connections and workings of Adafruits 32x16 RGB LED matrix board (http://www.adafruit.com/products/420),
RRacer 0:1f58ecec51d6 3 also sold on other places, for instance http://www.ebay.com/itm/PH6-RGB-Full-Color-LED-16-32-Dot-Matrix-Display-Module-/310303408628?pt=LH_DefaultDomain_0&hash=item483f8641f4 (no
RRacer 0:1f58ecec51d6 4 affiliation with either of them).
RRacer 0:1f58ecec51d6 5 This program is not intended to be highly optimized or a guideline in C programming in any way (more of the opposite actually).
RRacer 1:dd0dcd303d6d 6 To have more than 7 colors on this thing, you need to implement software PWM of some sort. I have obviously not done that, but if YOU do, please let me know!
RRacer 1:dd0dcd303d6d 7 Adafruit have a wicked demo program for an arduino - www.youtube.com/watch?v=lY-flFEfsHo
RRacer 0:1f58ecec51d6 8 There are probably lots of ways to make this perform better, perhaps by using Neal Hormans port of the Adafruit_GFX library (http://mbed.org/users/nkhorman/code/Adafruit_GFX/).
RRacer 0:1f58ecec51d6 9 No error checking or out-of-bounds checking is done. Use at your own peril.
RRacer 0:1f58ecec51d6 10 For more detailed information on the driver chip, see http://www.bjtopspace.com/ziliao/CYT62726.pdf
RRacer 0:1f58ecec51d6 11 Although the chips on my board says jx15020, I've been informed that they are equvivalent to the CYT62726, and so far it's a match.
RRacer 0:1f58ecec51d6 12 Feel free to use all or parts of this work.
RRacer 0:1f58ecec51d6 13 If you choose to do so, I would appreciate a small mentioning in the scrolling opening credits ;)
RRacer 0:1f58ecec51d6 14
RRacer 0:1f58ecec51d6 15 Best regards,
RRacer 0:1f58ecec51d6 16 Hugo Harming
RRacer 0:1f58ecec51d6 17 upgraded@hotmail.com
RRacer 0:1f58ecec51d6 18 */
RRacer 0:1f58ecec51d6 19
RRacer 0:1f58ecec51d6 20 #include "mbed.h"
RRacer 0:1f58ecec51d6 21 #define LOW 0
RRacer 0:1f58ecec51d6 22 #define HIGH 1
RRacer 0:1f58ecec51d6 23
chirashi 2:c1a9a2a0885d 24 BusOut ABC(D8,D9,D10); // Row address.
chirashi 2:c1a9a2a0885d 25 DigitalOut CLK(D11); // Data clock - rising edge
chirashi 2:c1a9a2a0885d 26 DigitalOut LAT(D12); // Data latch - active low (pulse up after data load)
chirashi 2:c1a9a2a0885d 27 DigitalOut OE(D13); // Output enable - active low (hold high during data load, bring low after LAT pulse)
chirashi 2:c1a9a2a0885d 28 DigitalOut R1(D2); // RED Serial in for upper half
chirashi 2:c1a9a2a0885d 29 DigitalOut R2(D3); // RED Serial in for lower half
chirashi 2:c1a9a2a0885d 30 DigitalOut G1(D4); // GREEN Serial in for upper half
chirashi 2:c1a9a2a0885d 31 DigitalOut G2(D5); // GREEN Serial in for lower half
chirashi 2:c1a9a2a0885d 32 DigitalOut B1(D6); // BLUE Serial in for upper half
chirashi 2:c1a9a2a0885d 33 DigitalOut B2(D7); // BLUE Serial in for lower half
RRacer 0:1f58ecec51d6 34
RRacer 0:1f58ecec51d6 35 unsigned char gm[32][6]; // Buffer with 32x6 bytes. Graphics memory if you like.
RRacer 0:1f58ecec51d6 36 unsigned long CT; // Counter for demo code
RRacer 0:1f58ecec51d6 37
RRacer 0:1f58ecec51d6 38 void MkPattern() // Fill graphics buffer with colorful test pattern.
RRacer 0:1f58ecec51d6 39 {
RRacer 0:1f58ecec51d6 40 unsigned int col,r,g,b;
RRacer 0:1f58ecec51d6 41 r=0x0f0f0f0f;
RRacer 0:1f58ecec51d6 42 g=0x33333333;
RRacer 0:1f58ecec51d6 43 b=0x55555555;
RRacer 0:1f58ecec51d6 44 for(col=0; col<32; col++) {
RRacer 0:1f58ecec51d6 45 gm[col][0]=(r>>(col % 8)) & 0xff; // Shift red value and mask out LSB.
RRacer 0:1f58ecec51d6 46 gm[col][1]=(g>>(col % 8)) & 0xff; // Shift green value and mask out LSB.
RRacer 0:1f58ecec51d6 47 gm[col][2]=(b>>(col % 8)) & 0xff; // Shift blue value and mask out LSB.
RRacer 0:1f58ecec51d6 48 gm[col][3]=gm[col][0]; // Copy top red byte to bottom red byte.
RRacer 0:1f58ecec51d6 49 gm[col][4]=gm[col][1]; // Copy top green byte to bottom green byte.
RRacer 0:1f58ecec51d6 50 gm[col][5]=gm[col][2]; // Copy top blue byte to bottom blue byte.
RRacer 0:1f58ecec51d6 51 }
RRacer 0:1f58ecec51d6 52 }
RRacer 0:1f58ecec51d6 53
RRacer 0:1f58ecec51d6 54 void Init()
RRacer 0:1f58ecec51d6 55 {
RRacer 0:1f58ecec51d6 56 // Set up things to a known state
RRacer 0:1f58ecec51d6 57 CLK = LOW;
RRacer 0:1f58ecec51d6 58 LAT = LOW;
RRacer 0:1f58ecec51d6 59 OE = HIGH; //display off
RRacer 0:1f58ecec51d6 60 ABC = 0;
RRacer 0:1f58ecec51d6 61 CT=0;
RRacer 0:1f58ecec51d6 62 MkPattern();
RRacer 0:1f58ecec51d6 63 }
RRacer 0:1f58ecec51d6 64
RRacer 0:1f58ecec51d6 65 void ShiftRight()
RRacer 0:1f58ecec51d6 66 {
RRacer 0:1f58ecec51d6 67 unsigned char i,r,g,b;
RRacer 0:1f58ecec51d6 68 r=gm[31][0]; // Save value of last column (top half)
RRacer 0:1f58ecec51d6 69 g=gm[31][1];
RRacer 0:1f58ecec51d6 70 b=gm[31][2];
RRacer 0:1f58ecec51d6 71
RRacer 0:1f58ecec51d6 72 for(i=31; i>0; i--) { // Copy value of column n to column n-1
RRacer 0:1f58ecec51d6 73 gm[i][0]=gm[i-1][0];
RRacer 0:1f58ecec51d6 74 gm[i][1]=gm[i-1][1];
RRacer 0:1f58ecec51d6 75 gm[i][2]=gm[i-1][2];
RRacer 0:1f58ecec51d6 76 }
RRacer 0:1f58ecec51d6 77 gm[0][0]=r; // Paste saved values from last column to first column
RRacer 0:1f58ecec51d6 78 gm[0][1]=g;
RRacer 0:1f58ecec51d6 79 gm[0][2]=b;
RRacer 0:1f58ecec51d6 80 // Do it again for the lower half
RRacer 0:1f58ecec51d6 81 r=gm[31][3];
RRacer 0:1f58ecec51d6 82 g=gm[31][4];
RRacer 0:1f58ecec51d6 83 b=gm[31][5];
RRacer 0:1f58ecec51d6 84
RRacer 0:1f58ecec51d6 85 for(i=31; i>0; i--) {
RRacer 0:1f58ecec51d6 86 gm[i][3]=gm[i-1][3];
RRacer 0:1f58ecec51d6 87 gm[i][4]=gm[i-1][4];
RRacer 0:1f58ecec51d6 88 gm[i][5]=gm[i-1][5];
RRacer 0:1f58ecec51d6 89 }
RRacer 0:1f58ecec51d6 90 gm[0][3]=r;
RRacer 0:1f58ecec51d6 91 gm[0][4]=g;
RRacer 0:1f58ecec51d6 92 gm[0][5]=b;
RRacer 0:1f58ecec51d6 93 }
RRacer 0:1f58ecec51d6 94
RRacer 0:1f58ecec51d6 95 void WrRow(unsigned char Row)
RRacer 0:1f58ecec51d6 96 {
RRacer 0:1f58ecec51d6 97 // Write specified row (and row+8) to display. Valid input: 0 to 7.
chirashi 2:c1a9a2a0885d 98 ABC = 7-Row; // Set row address
RRacer 0:1f58ecec51d6 99 for(int col=0; col<32; col++) { // To daisychain more displays, I guess you would have to increase this counter to n*32 columns. Might mirror though.
RRacer 0:1f58ecec51d6 100 R1 = gm[col][0] & (1<<Row); // Red bit, upper half
RRacer 0:1f58ecec51d6 101 G1 = gm[col][1] & (1<<Row); // Green bit, upper half
RRacer 0:1f58ecec51d6 102 B1 = gm[col][2] & (1<<Row); // Blue bit, upper half
RRacer 0:1f58ecec51d6 103 R2 = gm[col][3] & (1<<Row); // Red bit, lower half
RRacer 0:1f58ecec51d6 104 G2 = gm[col][4] & (1<<Row); // Green bit, lower half
RRacer 0:1f58ecec51d6 105 B2 = gm[col][5] & (1<<Row); // Blue bit, lower half
RRacer 0:1f58ecec51d6 106 CLK = HIGH; // tick (clock bit in)
RRacer 0:1f58ecec51d6 107 CLK = LOW; // tock
RRacer 0:1f58ecec51d6 108 }
RRacer 0:1f58ecec51d6 109 LAT = HIGH; // Latch entire row
RRacer 0:1f58ecec51d6 110 LAT = LOW;
RRacer 0:1f58ecec51d6 111 }
RRacer 0:1f58ecec51d6 112
chirashi 3:6dbbc0130e96 113 void WrRowOFF(unsigned char Row)
chirashi 3:6dbbc0130e96 114 {
chirashi 3:6dbbc0130e96 115 // Write specified row (and row+8) to display. Valid input: 0 to 7.
chirashi 3:6dbbc0130e96 116 ABC = 7-Row; // Set row address
chirashi 3:6dbbc0130e96 117 for(int col=0; col<32; col++) { // To daisychain more displays, I guess you would have to increase this counter to n*32 columns. Might mirror though.
chirashi 3:6dbbc0130e96 118 R1 = 0; // Red bit, upper half
chirashi 3:6dbbc0130e96 119 G1 = 0; // Green bit, upper half
chirashi 3:6dbbc0130e96 120 B1 = 0; // Blue bit, upper half
chirashi 3:6dbbc0130e96 121 R2 = 0; // Red bit, lower half
chirashi 3:6dbbc0130e96 122 G2 = 0; // Green bit, lower half
chirashi 3:6dbbc0130e96 123 B2 = 0; // Blue bit, lower half
chirashi 3:6dbbc0130e96 124 CLK = HIGH; // tick (clock bit in)
chirashi 3:6dbbc0130e96 125 CLK = LOW; // tock
chirashi 3:6dbbc0130e96 126 }
chirashi 3:6dbbc0130e96 127 LAT = HIGH; // Latch entire row
chirashi 3:6dbbc0130e96 128 LAT = LOW;
chirashi 3:6dbbc0130e96 129 }
chirashi 3:6dbbc0130e96 130
RRacer 0:1f58ecec51d6 131 void Pset(unsigned char x,unsigned char y, unsigned char c)
RRacer 0:1f58ecec51d6 132 {
RRacer 0:1f58ecec51d6 133 // Set pixel (x,y) to color c
RRacer 0:1f58ecec51d6 134 // Manipulates graphics memory, so you won't see any change til you Paint() it.
RRacer 0:1f58ecec51d6 135 unsigned char ud,l,r0,g0,b0;
RRacer 0:1f58ecec51d6 136 ud=(y & 8)>>3; // 0 = upper half, 1 = lower half
RRacer 0:1f58ecec51d6 137 l=y & 7; // Extract row in upper/lower half
RRacer 0:1f58ecec51d6 138 r0=(c & 4) >>2; // Extract red bit from color
RRacer 0:1f58ecec51d6 139 g0=(c & 2) >>1; // Extract green bit from color
RRacer 0:1f58ecec51d6 140 b0=(c & 1); // Extract blue bit from color
RRacer 0:1f58ecec51d6 141 // *******Removes current bit ******* *Adds bit**
RRacer 0:1f58ecec51d6 142 gm[x][0+3*ud]=(gm[x][0+3*ud] & (255-(1<<(7-l))))+(r0<<(7-l)); // Red byte
RRacer 0:1f58ecec51d6 143 gm[x][1+3*ud]=(gm[x][1+3*ud] & (255-(1<<(7-l))))+(g0<<(7-l)); // Green byte
RRacer 0:1f58ecec51d6 144 gm[x][2+3*ud]=(gm[x][2+3*ud] & (255-(1<<(7-l))))+(b0<<(7-l)); // Blue byte
RRacer 0:1f58ecec51d6 145 }
RRacer 0:1f58ecec51d6 146
RRacer 0:1f58ecec51d6 147 void Paint()
RRacer 0:1f58ecec51d6 148 {
RRacer 0:1f58ecec51d6 149 // Write graphics memory to display
RRacer 0:1f58ecec51d6 150 for(int Row=0; Row<8; Row++) {
RRacer 0:1f58ecec51d6 151 OE = HIGH; // Disable output
RRacer 0:1f58ecec51d6 152 WrRow(Row);
RRacer 0:1f58ecec51d6 153 OE = LOW; // Enable output
RRacer 0:1f58ecec51d6 154 wait_us(500); // Wasting some time. Use for whatever else. Probably better with a ticker for the display refresh.
RRacer 0:1f58ecec51d6 155 }
RRacer 0:1f58ecec51d6 156 }
RRacer 0:1f58ecec51d6 157
chirashi 3:6dbbc0130e96 158
chirashi 3:6dbbc0130e96 159 void PaintOFF()
chirashi 3:6dbbc0130e96 160 {
chirashi 3:6dbbc0130e96 161 // Write graphics memory to display
chirashi 3:6dbbc0130e96 162 for(int Row=0; Row<8; Row++) {
chirashi 3:6dbbc0130e96 163 OE = HIGH; // Disable output
chirashi 3:6dbbc0130e96 164 WrRowOFF(Row);
chirashi 3:6dbbc0130e96 165 OE = LOW; // Enable output
chirashi 3:6dbbc0130e96 166 wait_us(500); // Wasting some time. Use for whatever else. Probably better with a ticker for the display refresh.
chirashi 3:6dbbc0130e96 167 }
chirashi 3:6dbbc0130e96 168 }
chirashi 3:6dbbc0130e96 169
chirashi 3:6dbbc0130e96 170
chirashi 3:6dbbc0130e96 171
RRacer 0:1f58ecec51d6 172 int main()
RRacer 0:1f58ecec51d6 173 {
RRacer 0:1f58ecec51d6 174 Init(); // Set things up
RRacer 0:1f58ecec51d6 175 while(1) { // Messy demo loop following...
RRacer 0:1f58ecec51d6 176 CT++;
chirashi 3:6dbbc0130e96 177
RRacer 0:1f58ecec51d6 178 if((CT<=2560)||(CT>=2880 && CT<=4160)) {
RRacer 0:1f58ecec51d6 179 Paint(); // Refresh display
RRacer 0:1f58ecec51d6 180 if((CT % 20)==0) ShiftRight(); // After every 20 refresh, do a ShiftRight
RRacer 0:1f58ecec51d6 181 }
chirashi 3:6dbbc0130e96 182
RRacer 0:1f58ecec51d6 183 if(CT==2560) {
RRacer 0:1f58ecec51d6 184 for(int c=0; c<8; c++) {
RRacer 0:1f58ecec51d6 185 for(int x=c; x<(31-c); x++) {// Top side
RRacer 0:1f58ecec51d6 186 Pset(x,c,c);
RRacer 0:1f58ecec51d6 187 Paint(); // Display refresh time sets loop duration.
RRacer 0:1f58ecec51d6 188 }
RRacer 0:1f58ecec51d6 189 for(int y=c; y<(15-c); y++) {// Right side
RRacer 0:1f58ecec51d6 190 Pset(31-c,y,c);
RRacer 0:1f58ecec51d6 191 Paint();
RRacer 0:1f58ecec51d6 192 }
RRacer 0:1f58ecec51d6 193 for(int x=(31-c); x>=c; x--) {// Bottom side
RRacer 0:1f58ecec51d6 194 Pset(x,(15-c),c);
RRacer 0:1f58ecec51d6 195 Paint();
RRacer 0:1f58ecec51d6 196 }
RRacer 0:1f58ecec51d6 197 for(int y=(15-c); y>=c; y--) { // Left side
RRacer 0:1f58ecec51d6 198 Pset(c,y,c);
RRacer 0:1f58ecec51d6 199 Paint();
RRacer 0:1f58ecec51d6 200 }
RRacer 0:1f58ecec51d6 201 }
RRacer 0:1f58ecec51d6 202 }
chirashi 3:6dbbc0130e96 203
RRacer 0:1f58ecec51d6 204 if(CT>4160) {
RRacer 0:1f58ecec51d6 205 MkPattern(); // Restore original priceless artwork
RRacer 0:1f58ecec51d6 206 CT=0; // Start all over.
RRacer 0:1f58ecec51d6 207 }
chirashi 3:6dbbc0130e96 208
chirashi 3:6dbbc0130e96 209 PaintOFF();
chirashi 3:6dbbc0130e96 210 wait_us(100);
RRacer 0:1f58ecec51d6 211 }
chirashi 3:6dbbc0130e96 212
RRacer 0:1f58ecec51d6 213 }