Skip to content

Instantly share code, notes, and snippets.

@frederikbrudy
Created January 14, 2016 17:28
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 frederikbrudy/bae890d89b28396ac03d to your computer and use it in GitHub Desktop.
Save frederikbrudy/bae890d89b28396ac03d to your computer and use it in GitHub Desktop.
/*
* Example for 8x8 LED matrix
* Original source http://hastebin.com/raw/wazuhujoga, linked from http://www.amazon.co.uk/forum/-/Tx3UWNLLPMFDQT6/ref=ask_dp_dpmw_al_hza?asin=B00GKEMQUM
*/
#define MAX7219_DIN 5
#define MAX7219_CS 3
#define MAX7219_CLK 2
const int wait = 100;
void initialise()
{
digitalWrite(MAX7219_CS, HIGH);
pinMode(MAX7219_DIN, OUTPUT);
pinMode(MAX7219_CS, OUTPUT);
pinMode(MAX7219_CLK, OUTPUT);
}
void output(byte address, byte data)
{
digitalWrite(MAX7219_CS, LOW);
shiftOut(MAX7219_DIN, MAX7219_CLK, MSBFIRST, address);
shiftOut(MAX7219_DIN, MAX7219_CLK, MSBFIRST, data);
digitalWrite(MAX7219_CS, HIGH);
}
void setTestMode(boolean on)
{
output(0x0f, on ? 0x01 : 0x00);
}
void setShutdown(boolean off)
{
output(0x0c, off ? 0x00 : 0x01); //shutdown register - normal operation
}
void showDigits(byte numDigits)
{
output(0x0b, numDigits-1); //scan limit register
}
void setBrightness(byte brightness)
{
output(0x0a, brightness); //intensity register - max brightness
}
void putByte(byte data) {
byte i = 8;
byte mask;
while(i > 0) {
mask = 0x01 << (i - 1); // get bitmask
digitalWrite(MAX7219_CLK, LOW); // tick
if (data & mask){ // choose bit
digitalWrite(MAX7219_DIN, HIGH);// send 1
}else{
digitalWrite(MAX7219_DIN, LOW); // send 0
}
digitalWrite(MAX7219_CLK, HIGH); // tock
--i; // move to lesser bit
}
}
void maxSingle(byte reg, byte col) {
digitalWrite(MAX7219_CS, LOW); // CS has to transition from LOW to HIGH
putByte(reg); // specify register
putByte(col); // put data
digitalWrite(MAX7219_CS, LOW); // Load by switching CS HIGH
digitalWrite(MAX7219_CS, HIGH);
}
void write8x8(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h){
maxSingle(1,a);
maxSingle(2,b);
maxSingle(3,c);
maxSingle(4,d);
maxSingle(5,e);
maxSingle(6,f);
maxSingle(7,g);
maxSingle(8,h);
delay(wait);
}
void setup() {
initialise();
setTestMode(false);
setShutdown(false);
setBrightness(1); // Brightness range 1..0x0f
showDigits(8); // Make sure all digits are visible
output(0x09, 0); // using an led matrix (not digits)
}
void loop() {
// Generate bytes with something like the LED Byte Generator Chrome App: https://goo.gl/w4xhzm
write8x8(0x0,0x42,0x42,0x42,0x42,0x42,0x42,0x3c);
/*
write8x8(0x0,0x1,0x1,0x1,0x1,0x1,0x1,0x0);
write8x8(0x0,0x2,0x3,0x2,0x2,0x2,0x2,0x0);
write8x8(0x0,0x4,0x6,0x5,0x4,0x4,0x4,0x0);
write8x8(0x0,0x8,0xc,0xa,0x9,0x8,0x8,0x0);
write8x8(0x0,0x10,0x18,0x14,0x12,0x11,0x10,0x0);
write8x8(0x0,0x21,0x31,0x29,0x25,0x23,0x21,0x0);
write8x8(0x0,0x42,0x62,0x52,0x4a,0x46,0x42,0x0);
write8x8(0x0,0x85,0xc5,0xa5,0x95,0x8d,0x85,0x0);
write8x8(0x0,0xa,0x8a,0x4a,0x2a,0x1a,0xa,0x0);
write8x8(0x0,0x14,0x15,0x95,0x55,0x35,0x14,0x0);
write8x8(0x0,0x29,0x2a,0x2a,0xaa,0x6a,0x29,0x0);
write8x8(0x0,0x53,0x54,0x54,0x54,0xd4,0x53,0x0);
write8x8(0x0,0xa6,0xa9,0xa8,0xa8,0xa9,0xa6,0x0);
write8x8(0x0,0x4c,0x52,0x50,0x50,0x52,0x4c,0x0);
write8x8(0x0,0x99,0xa5,0xa1,0xa1,0xa5,0x99,0x0);
write8x8(0x0,0x32,0x4a,0x43,0x43,0x4a,0x32,0x0);
write8x8(0x0,0x64,0x95,0x86,0x86,0x95,0x64,0x0);
write8x8(0x0,0xc9,0x2a,0xc,0xc,0x2a,0xc9,0x0);
write8x8(0x0,0x92,0x54,0x18,0x18,0x54,0x92,0x0);
write8x8(0x0,0x24,0xa8,0x30,0x30,0xa8,0x24,0x0);
write8x8(0x0,0x48,0x50,0x60,0x60,0x50,0x48,0x0);
write8x8(0x0,0x90,0xa0,0xc0,0xc0,0xa0,0x90,0x0);
write8x8(0x0,0x20,0x40,0x80,0x80,0x40,0x20,0x0);
write8x8(0x0,0x40,0x80,0x0,0x0,0x80,0x40,0x0);
write8x8(0x0,0x80,0x0,0x0,0x0,0x0,0x80,0x0);
write8x8(0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0);
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment