Skip to content

Instantly share code, notes, and snippets.

@DanielOaks DanielOaks/grid.ino
Last active Nov 1, 2018

Embed
What would you like to do?
// thanks to https://www.glennklockwood.com/electronics/max7219.html and http://www.gammon.com.au/forum/?id=11516 for help
// and to the data sheet
#include <SPI.h>
#define selectPin D2
#define REG_NOOP 0x00
#define REG_DIGIT_0 0x01
#define REG_DIGIT_1 0x02
#define REG_DIGIT_2 0x03
#define REG_DIGIT_3 0x04
#define REG_DIGIT_4 0x05
#define REG_DIGIT_5 0x06
#define REG_DIGIT_6 0x07
#define REG_DIGIT_7 0x08
#define REG_DECODE_MODE 0x09
#define REG_INTENSITY 0x0A
#define REG_SCAN_LIMIT 0x0B
#define REG_SHUTDOWN 0x0C
#define REG_DISPLAY_TEST 0x0F
SPISettings MaxSpiSettings;
const int IMAGE_WAIT[] = {
2000,
1700,
1000,
1700,
2200,
1000,
};
const byte IMAGES[][8] = {
{
B01111110,
B11000011,
B10011001,
B10011001,
B10011001,
B10100101,
B10000001,
B01111110
},{
B00000000,
B10111011,
B10101010,
B10110010,
B10101010,
B10101010,
B10101011,
B00000000
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
},{
B00000000,
B10111011,
B10101010,
B10110010,
B10101010,
B10101010,
B10101011,
B00000000
},{
B00000000,
B00000110,
B00001001,
B00000001,
B10100110,
B10100001,
B01001001,
B00000110
},{
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000,
B00000000
}};
const int IMAGES_LEN = sizeof(IMAGES)/8;
int image = 0;
void setRegister(byte reg, byte value) {
// Serial.print(" setting 0x");
// Serial.print(reg, HEX);
// Serial.print(" to 0x");
// Serial.print(value, HEX);
// Serial.println("");
digitalWrite(selectPin, LOW);
SPI.beginTransaction(MaxSpiSettings);
SPI.transfer(reg);
SPI.transfer(value);
SPI.endTransaction();
digitalWrite(selectPin, HIGH);
}
void setup() {
MaxSpiSettings = SPISettings(1000000, MSBFIRST, SPI_MODE0);
SPI.begin();
Serial.begin(115200);
// enable chip select pin
pinMode(selectPin, OUTPUT);
digitalWrite(selectPin, HIGH);
// wait for output to stabilise before we start using it
Serial.print("\n");
Serial.println("waiting");
Serial.println(0x1 | 0x02, HEX);
delay(500);
// set scan limit register
setRegister(REG_SCAN_LIMIT, 0x07);
// set decode mode
setRegister(REG_DECODE_MODE, 0x00);
// disable display test
setRegister(REG_DISPLAY_TEST, 0x00);
// clear display
for (uint8_t a = 0x00; a < 0x08; ++a) {
setRegister(a + 0x01, 0x00);
}
// set scan limit register
setRegister(REG_INTENSITY, 0x00);
// set shutdown register
setRegister(REG_SHUTDOWN, 0x01);
// wait a bit
Serial.println("blanked");
delay(500);
}
void displayImage(int iNum) {
Serial.print("Displaying image ");
Serial.println(iNum);
byte column;
for (int x = 0; x < 8; ++x) {
// setRegister(x + 0x1, IMAGES[iNum][x]);
column = 0;
for (int y = 0; y < 8; ++y) {
column = column | (((IMAGES[iNum][y] >> (7 - x)) & B00000001) << y);
}
setRegister(x + 0x1, column);
}
}
void loop() {
displayImage(image);
delay(IMAGE_WAIT[image]);
if (++image >= IMAGES_LEN) {
image = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.