Skip to content

Instantly share code, notes, and snippets.

@jboecker
Created January 13, 2015 06:34
Show Gist options
  • Save jboecker/abc26c4fb3ebd8396828 to your computer and use it in GitHub Desktop.
Save jboecker/abc26c4fb3ebd8396828 to your computer and use it in GitHub Desktop.
/*
Connections:
CLOCK: Pin 2 (interrupt 0)
LOAD: Pin 3 (interrupt 1)
DATA IN: Pin 4
This sketch emulates the SPI interface of the
MAX7219 chip and constantly prints the values
of the internal 16 registers to the serial
port (followed by a latch counter).
*/
volatile unsigned int shiftreg = 0;
volatile unsigned int latchcounter = 0;
volatile unsigned char regs[16];
void printRegs() {
unsigned char i, j;
for (i=0; i<16; i++) {
for (j=0; j<8; j++) {
if (regs[i] & (128>>j)) {
Serial.write('1');
} else {
Serial.write('_');
}
}
Serial.print(" ");
}
Serial.println(latchcounter);
}
void onRisingClk() {
shiftreg <<= 1;
if (PIND & (1<<4))
shiftreg |= 1;
}
void onRisingLoad() {
unsigned int reg = (shiftreg & 0x0f00) >> 8;
unsigned int value = shiftreg & 0x00ff;
regs[reg] = value;
latchcounter++;
}
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
attachInterrupt(0, onRisingClk, RISING);
attachInterrupt(1, onRisingLoad, RISING);
Serial.begin(115200);
Serial.println("MAX7219 Emulation");
printRegs();
}
void loop() {
// put your main code here, to run repeatedly:
printRegs();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment