Skip to content

Instantly share code, notes, and snippets.

@JakubVanek
Last active December 28, 2016 11:17
Show Gist options
  • Save JakubVanek/274c06135553d3b56f762d6938dbd7d6 to your computer and use it in GitHub Desktop.
Save JakubVanek/274c06135553d3b56f762d6938dbd7d6 to your computer and use it in GitHub Desktop.
Fast 8x8 LED dot matrix + SN74HC595 + Arduino Uno R3 counter
inline void activateCol(byte n) __attribute__((always_inline));
inline void setbit(byte value) __attribute__((always_inline));
inline void shift() __attribute__((always_inline));
inline void latch() __attribute__((always_inline));
inline void clear() __attribute__((always_inline));
byte buffer[8];
void setup() {
DDRD |= B11111100;
DDRB |= B00111111;
PORTD = (PORTD & B00000011) | B11100000;
PORTB = (PORTB & B11000000) | B00111111;
TIMSK0 = 0;
clear();
}
void loop() {
for (unsigned long counter = 0; counter < 0xFFFFFFFF; counter++) {
*((long*)buffer) = counter;
draw(buffer);
}
}
////////////////////////
// LED MATRIX HELPERS //
////////////////////////
//
// Optimized block draw
//
inline void draw(const byte *data) {
setbit(true);
shift();
setbit(false);
for (byte row = 0; row < 8; row++) {
byte arr = data[row];
latch();
for (int col = 1; col < 0x200; col <<= 1) {
bool on = arr & 0x1;
arr >>= 1;
activateCol(on ? col : 0x0);
}
shift();
}
//latch();
}
//
// Direct addressing
//
inline void activateCol(byte n) {
byte val = ~n;
PORTD = PORTD & B00011111 | (val << 5);
PORTB = PORTB & B11100000 | (val >> 3);
}
////////////////////////////
// SHIFT REGISTER CONTROL //
////////////////////////////
inline void setbit(bool value) {
PORTD = PORTD & B11111011 | (value << 2);
}
inline void shift() {
PORTD |= B00010000;
PORTD &= B11101111;
}
inline void latch() {
PORTD |= B00001000;
PORTD &= B11110111;
}
inline void clear() {
PORTB &= B11011111;
PORTB |= B00100000;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment