Test program for Adafruit's 32x16 RGB LED matrix. Should look like this: [[https://www.youtube.com/watch?v=YjY66pOaO_M]]

Dependencies:   mbed

Test program for Adafruit's 32x16 RGB LED matrix. Should look like this: https://www.youtube.com/watch?v=YjY66pOaO_M

Committer:
RRacer
Date:
Mon Nov 19 02:08:11 2012 +0000
Revision:
0:1f58ecec51d6
Child:
1:dd0dcd303d6d
Verified test program for Adafruits 16x32 RGB LED matrix.; http://www.adafruit.com/products/420

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 0:1f58ecec51d6 6 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 7 No error checking or out-of-bounds checking is done. Use at your own peril.
RRacer 0:1f58ecec51d6 8 For more detailed information on the driver chip, see http://www.bjtopspace.com/ziliao/CYT62726.pdf
RRacer 0:1f58ecec51d6 9 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 10 Feel free to use all or parts of this work.
RRacer 0:1f58ecec51d6 11 If you choose to do so, I would appreciate a small mentioning in the scrolling opening credits ;)
RRacer 0:1f58ecec51d6 12
RRacer 0:1f58ecec51d6 13 Best regards,
RRacer 0:1f58ecec51d6 14 Hugo Harming
RRacer 0:1f58ecec51d6 15 upgraded@hotmail.com
RRacer 0:1f58ecec51d6 16 */
RRacer 0:1f58ecec51d6 17
RRacer 0:1f58ecec51d6 18 #include "mbed.h"
RRacer 0:1f58ecec51d6 19 #define LOW 0
RRacer 0:1f58ecec51d6 20 #define HIGH 1
RRacer 0:1f58ecec51d6 21
RRacer 0:1f58ecec51d6 22 BusOut ABC(p24,p25,p26); // Row address.
RRacer 0:1f58ecec51d6 23 DigitalOut CLK(p21); // Data clock - rising edge
RRacer 0:1f58ecec51d6 24 DigitalOut LAT(p22); // Data latch - active low (pulse up after data load)
RRacer 0:1f58ecec51d6 25 DigitalOut OE(p23); // Output enable - active low (hold high during data load, bring low after LAT pulse)
RRacer 0:1f58ecec51d6 26 DigitalOut R1(p27); // RED Serial in for upper half
RRacer 0:1f58ecec51d6 27 DigitalOut R2(p28); // RED Serial in for lower half
RRacer 0:1f58ecec51d6 28 DigitalOut G1(p5); // GREEN Serial in for upper half
RRacer 0:1f58ecec51d6 29 DigitalOut G2(p6); // GREEN Serial in for lower half
RRacer 0:1f58ecec51d6 30 DigitalOut B1(p7); // BLUE Serial in for upper half
RRacer 0:1f58ecec51d6 31 DigitalOut B2(p8); // BLUE Serial in for lower half
RRacer 0:1f58ecec51d6 32
RRacer 0:1f58ecec51d6 33 unsigned char gm[32][6]; // Buffer with 32x6 bytes. Graphics memory if you like.
RRacer 0:1f58ecec51d6 34 unsigned long CT; // Counter for demo code
RRacer 0:1f58ecec51d6 35
RRacer 0:1f58ecec51d6 36 void MkPattern() // Fill graphics buffer with colorful test pattern.
RRacer 0:1f58ecec51d6 37 {
RRacer 0:1f58ecec51d6 38 unsigned int col,r,g,b;
RRacer 0:1f58ecec51d6 39 r=0x0f0f0f0f;
RRacer 0:1f58ecec51d6 40 g=0x33333333;
RRacer 0:1f58ecec51d6 41 b=0x55555555;
RRacer 0:1f58ecec51d6 42 for(col=0; col<32; col++) {
RRacer 0:1f58ecec51d6 43 gm[col][0]=(r>>(col % 8)) & 0xff; // Shift red value and mask out LSB.
RRacer 0:1f58ecec51d6 44 gm[col][1]=(g>>(col % 8)) & 0xff; // Shift green value and mask out LSB.
RRacer 0:1f58ecec51d6 45 gm[col][2]=(b>>(col % 8)) & 0xff; // Shift blue value and mask out LSB.
RRacer 0:1f58ecec51d6 46 gm[col][3]=gm[col][0]; // Copy top red byte to bottom red byte.
RRacer 0:1f58ecec51d6 47 gm[col][4]=gm[col][1]; // Copy top green byte to bottom green byte.
RRacer 0:1f58ecec51d6 48 gm[col][5]=gm[col][2]; // Copy top blue byte to bottom blue byte.
RRacer 0:1f58ecec51d6 49 }
RRacer 0:1f58ecec51d6 50 }
RRacer 0:1f58ecec51d6 51
RRacer 0:1f58ecec51d6 52 void Init()
RRacer 0:1f58ecec51d6 53 {
RRacer 0:1f58ecec51d6 54 // Set up things to a known state
RRacer 0:1f58ecec51d6 55 CLK = LOW;
RRacer 0:1f58ecec51d6 56 LAT = LOW;
RRacer 0:1f58ecec51d6 57 OE = HIGH; //display off
RRacer 0:1f58ecec51d6 58 ABC = 0;
RRacer 0:1f58ecec51d6 59 CT=0;
RRacer 0:1f58ecec51d6 60 MkPattern();
RRacer 0:1f58ecec51d6 61 }
RRacer 0:1f58ecec51d6 62
RRacer 0:1f58ecec51d6 63 void ShiftRight()
RRacer 0:1f58ecec51d6 64 {
RRacer 0:1f58ecec51d6 65 unsigned char i,r,g,b;
RRacer 0:1f58ecec51d6 66 r=gm[31][0]; // Save value of last column (top half)
RRacer 0:1f58ecec51d6 67 g=gm[31][1];
RRacer 0:1f58ecec51d6 68 b=gm[31][2];
RRacer 0:1f58ecec51d6 69
RRacer 0:1f58ecec51d6 70 for(i=31; i>0; i--) { // Copy value of column n to column n-1
RRacer 0:1f58ecec51d6 71 gm[i][0]=gm[i-1][0];
RRacer 0:1f58ecec51d6 72 gm[i][1]=gm[i-1][1];
RRacer 0:1f58ecec51d6 73 gm[i][2]=gm[i-1][2];
RRacer 0:1f58ecec51d6 74 }
RRacer 0:1f58ecec51d6 75 gm[0][0]=r; // Paste saved values from last column to first column
RRacer 0:1f58ecec51d6 76 gm[0][1]=g;
RRacer 0:1f58ecec51d6 77 gm[0][2]=b;
RRacer 0:1f58ecec51d6 78 // Do it again for the lower half
RRacer 0:1f58ecec51d6 79 r=gm[31][3];
RRacer 0:1f58ecec51d6 80 g=gm[31][4];
RRacer 0:1f58ecec51d6 81 b=gm[31][5];
RRacer 0:1f58ecec51d6 82
RRacer 0:1f58ecec51d6 83 for(i=31; i>0; i--) {
RRacer 0:1f58ecec51d6 84 gm[i][3]=gm[i-1][3];
RRacer 0:1f58ecec51d6 85 gm[i][4]=gm[i-1][4];
RRacer 0:1f58ecec51d6 86 gm[i][5]=gm[i-1][5];
RRacer 0:1f58ecec51d6 87 }
RRacer 0:1f58ecec51d6 88 gm[0][3]=r;
RRacer 0:1f58ecec51d6 89 gm[0][4]=g;
RRacer 0:1f58ecec51d6 90 gm[0][5]=b;
RRacer 0:1f58ecec51d6 91 }
RRacer 0:1f58ecec51d6 92
RRacer 0:1f58ecec51d6 93 void WrRow(unsigned char Row)
RRacer 0:1f58ecec51d6 94 {
RRacer 0:1f58ecec51d6 95 // Write specified row (and row+8) to display. Valid input: 0 to 7.
RRacer 0:1f58ecec51d6 96 ABC=7-Row; // Set row address
RRacer 0:1f58ecec51d6 97 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 98 R1 = gm[col][0] & (1<<Row); // Red bit, upper half
RRacer 0:1f58ecec51d6 99 G1 = gm[col][1] & (1<<Row); // Green bit, upper half
RRacer 0:1f58ecec51d6 100 B1 = gm[col][2] & (1<<Row); // Blue bit, upper half
RRacer 0:1f58ecec51d6 101 R2 = gm[col][3] & (1<<Row); // Red bit, lower half
RRacer 0:1f58ecec51d6 102 G2 = gm[col][4] & (1<<Row); // Green bit, lower half
RRacer 0:1f58ecec51d6 103 B2 = gm[col][5] & (1<<Row); // Blue bit, lower half
RRacer 0:1f58ecec51d6 104 CLK = HIGH; // tick (clock bit in)
RRacer 0:1f58ecec51d6 105 CLK = LOW; // tock
RRacer 0:1f58ecec51d6 106 }
RRacer 0:1f58ecec51d6 107 LAT = HIGH; // Latch entire row
RRacer 0:1f58ecec51d6 108 LAT = LOW;
RRacer 0:1f58ecec51d6 109 }
RRacer 0:1f58ecec51d6 110
RRacer 0:1f58ecec51d6 111 void Pset(unsigned char x,unsigned char y, unsigned char c)
RRacer 0:1f58ecec51d6 112 {
RRacer 0:1f58ecec51d6 113 // Set pixel (x,y) to color c
RRacer 0:1f58ecec51d6 114 // Manipulates graphics memory, so you won't see any change til you Paint() it.
RRacer 0:1f58ecec51d6 115 unsigned char ud,l,r0,g0,b0;
RRacer 0:1f58ecec51d6 116 ud=(y & 8)>>3; // 0 = upper half, 1 = lower half
RRacer 0:1f58ecec51d6 117 l=y & 7; // Extract row in upper/lower half
RRacer 0:1f58ecec51d6 118 r0=(c & 4) >>2; // Extract red bit from color
RRacer 0:1f58ecec51d6 119 g0=(c & 2) >>1; // Extract green bit from color
RRacer 0:1f58ecec51d6 120 b0=(c & 1); // Extract blue bit from color
RRacer 0:1f58ecec51d6 121 // *******Removes current bit ******* *Adds bit**
RRacer 0:1f58ecec51d6 122 gm[x][0+3*ud]=(gm[x][0+3*ud] & (255-(1<<(7-l))))+(r0<<(7-l)); // Red byte
RRacer 0:1f58ecec51d6 123 gm[x][1+3*ud]=(gm[x][1+3*ud] & (255-(1<<(7-l))))+(g0<<(7-l)); // Green byte
RRacer 0:1f58ecec51d6 124 gm[x][2+3*ud]=(gm[x][2+3*ud] & (255-(1<<(7-l))))+(b0<<(7-l)); // Blue byte
RRacer 0:1f58ecec51d6 125 }
RRacer 0:1f58ecec51d6 126
RRacer 0:1f58ecec51d6 127 void Paint()
RRacer 0:1f58ecec51d6 128 {
RRacer 0:1f58ecec51d6 129 // Write graphics memory to display
RRacer 0:1f58ecec51d6 130 for(int Row=0; Row<8; Row++) {
RRacer 0:1f58ecec51d6 131 OE = HIGH; // Disable output
RRacer 0:1f58ecec51d6 132 WrRow(Row);
RRacer 0:1f58ecec51d6 133 OE = LOW; // Enable output
RRacer 0:1f58ecec51d6 134 wait_us(500); // Wasting some time. Use for whatever else. Probably better with a ticker for the display refresh.
RRacer 0:1f58ecec51d6 135 }
RRacer 0:1f58ecec51d6 136 }
RRacer 0:1f58ecec51d6 137
RRacer 0:1f58ecec51d6 138 int main()
RRacer 0:1f58ecec51d6 139 {
RRacer 0:1f58ecec51d6 140 Init(); // Set things up
RRacer 0:1f58ecec51d6 141 while(1) { // Messy demo loop following...
RRacer 0:1f58ecec51d6 142 CT++;
RRacer 0:1f58ecec51d6 143 if((CT<=2560)||(CT>=2880 && CT<=4160)) {
RRacer 0:1f58ecec51d6 144 Paint(); // Refresh display
RRacer 0:1f58ecec51d6 145 if((CT % 20)==0) ShiftRight(); // After every 20 refresh, do a ShiftRight
RRacer 0:1f58ecec51d6 146 }
RRacer 0:1f58ecec51d6 147 if(CT==2560) {
RRacer 0:1f58ecec51d6 148 for(int c=0; c<8; c++) {
RRacer 0:1f58ecec51d6 149 for(int x=c; x<(31-c); x++) {// Top side
RRacer 0:1f58ecec51d6 150 Pset(x,c,c);
RRacer 0:1f58ecec51d6 151 Paint(); // Display refresh time sets loop duration.
RRacer 0:1f58ecec51d6 152 }
RRacer 0:1f58ecec51d6 153 for(int y=c; y<(15-c); y++) {// Right side
RRacer 0:1f58ecec51d6 154 Pset(31-c,y,c);
RRacer 0:1f58ecec51d6 155 Paint();
RRacer 0:1f58ecec51d6 156 }
RRacer 0:1f58ecec51d6 157 for(int x=(31-c); x>=c; x--) {// Bottom side
RRacer 0:1f58ecec51d6 158 Pset(x,(15-c),c);
RRacer 0:1f58ecec51d6 159 Paint();
RRacer 0:1f58ecec51d6 160 }
RRacer 0:1f58ecec51d6 161 for(int y=(15-c); y>=c; y--) { // Left side
RRacer 0:1f58ecec51d6 162 Pset(c,y,c);
RRacer 0:1f58ecec51d6 163 Paint();
RRacer 0:1f58ecec51d6 164 }
RRacer 0:1f58ecec51d6 165 }
RRacer 0:1f58ecec51d6 166 }
RRacer 0:1f58ecec51d6 167 if(CT>4160) {
RRacer 0:1f58ecec51d6 168 MkPattern(); // Restore original priceless artwork
RRacer 0:1f58ecec51d6 169 CT=0; // Start all over.
RRacer 0:1f58ecec51d6 170 }
RRacer 0:1f58ecec51d6 171 }
RRacer 0:1f58ecec51d6 172 }