Skip to content

Instantly share code, notes, and snippets.

@possan
Created September 10, 2014 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save possan/0101417cda0da58a3597 to your computer and use it in GitHub Desktop.
Save possan/0101417cda0da58a3597 to your computer and use it in GitHub Desktop.
64x32 ledpanel driver
// PINS
// OE A
// - B
// - -
// - CLK
// - SCLK
// - -
// - R
// - -
#include <TimerOne.h>
#include <SPI.h>
const int PIN_OE = 13;
const int PIN_A = 12;
const int PIN_B = 11;
const int PIN_SCLK = 9;
// const int PIN_CLK = 5;
// const int PIN_R = 6;
const int PANELWIDTH = 64;
const int PANELHEIGHT = 16;
const int PANELPLANES = 4;
const int PANELPLANESIZE = 8;
int frame = 0;
int scan_row = 0;
char *pixels =
//0 1 2 3 4 5 6 7
//0123456701234567012345670123456701234567012345670123456701234567
"................................................................" // 0
".1111...1111..111..1111..1...1.1................................"
".1...1.1.....1...1.1...1.1...1.1................................"
".1111..1111..11111.1...1..111..1................................"
".1...1.1.....1...1.1...1...1...................................." // 4
".1...1..1111.1...1.1111....1...1................................"
"................................................................"
"................................................................"
"................................................................" // 8
"................................................................"
"................................................................"
"................................................................"
"................................................................" // 12
"................................................................"
"................................................................"
"................................................................";
unsigned short pixelmap[64*16/4];
int pixelmap_size;
unsigned short pixelmapoffset[4];
void select_row(int r) {
digitalWrite(PIN_A, (r & 1) == 1);
digitalWrite(PIN_B, (r & 2) == 2);
}
/*
static inline void latch_pixel(int r) {
digitalWrite(PIN_CLK, 0);
digitalWrite(PIN_R, r > 0 ? LOW : HIGH);
digitalWrite(PIN_CLK, 1);
digitalWrite(PIN_CLK, 0);
}
*/
void scan_display() {
digitalWrite(PIN_SCLK, 0);
for(int i=0; i<pixelmap_size; i++) {
int o = pixelmap[i] + pixelmapoffset[scan_row];
int bo = 0;
for(int b=7; b>=0; b--) {
// one segment
char ch = pixels[o + b];
if (ch != '1') bo |= (1 << b);
}
SPI.transfer(bo);
}
digitalWrite(PIN_OE, 0);
digitalWrite(PIN_SCLK, 1);
select_row(scan_row);
digitalWrite(PIN_OE, 1);
delayMicroseconds(1);
}
void callback()
{
scan_display();
scan_row ++;
if (scan_row >= 4) {
scan_row = 0;
frame ++;
frame %= 64*16/4;
}
}
void setup() {
// put your setup code here, to run once:
pinMode(PIN_A, OUTPUT);
pinMode(PIN_B, OUTPUT);
pinMode(PIN_OE, OUTPUT);
pinMode(PIN_SCLK, OUTPUT);
// pinMode(PIN_CLK, OUTPUT);
// pinMode(PIN_R, OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0); // CPOL=0, CPHA=0
#ifdef __AVR__
SPI.setClockDivider(SPI_CLOCK_DIV4); // 4MHz clock
#else
SPI.setClockDivider(20); // 4.2MHz on Due
#endif
int oo = 0;
for(int c=(PANELWIDTH >> 3) - 1; c>=0; c--) {
int bx = 8 * c;
for(int r=0; r<4; r++) {
int by = (r * 4) + 3;
int o = (by * PANELWIDTH) + bx;
pixelmap[oo++] = o;
}
}
pixelmap_size = oo;
for(scan_row=0; scan_row<4; scan_row++) {
pixelmapoffset[scan_row] = -(PANELWIDTH) * scan_row;// * (scan_row * 8);
}
Timer1.initialize(1500);
Timer1.attachInterrupt(callback);
Serial.begin(57600);
}
int wr = 0;
void loop() {
if(Serial.available()){
unsigned char cmd = Serial.read();
if (cmd == 'C') {
// clear screen
for(int i=0; i<64*16; i++) {
pixels[i] = '.';
}
Serial.write('K');
}
else if (cmd == 'H') {
// home cursor
Serial.write('K');
}
else if (cmd == 'W') {
unsigned char start0 = Serial.read();
unsigned char start1 = Serial.read();
unsigned char bytes0 = Serial.read();
unsigned char bytes1 = Serial.read();
int bytes = (int)bytes1 * 256 + (int)bytes0;
int start = (int)start1 * 256 + (int)start0;
wr = start * 8;
for (int i=0; i<bytes; i++) {
unsigned char current = Serial.read();
for(int i=0; i<8; i++) {
unsigned char mask = 1 << i;
pixels[i + wr] = ((current & mask) == mask) ? '1' : '.';
}
wr += 8;
wr &= 1023;
}
Serial.write('K');
}
/*
for(int i=0; i<8; i++) {
int mask = 1<<i;
pixels[i + wr] = ((current & mask) == mask) ? '1' : '.';
}
wr += 8;
wr %= 1024;
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment