Skip to content

Instantly share code, notes, and snippets.

@giljr
Created March 23, 2017 22:28
Show Gist options
  • Save giljr/4a076f646fe3a0971d26f2b973da4e4e to your computer and use it in GitHub Desktop.
Save giljr/4a076f646fe3a0971d26f2b973da4e4e to your computer and use it in GitHub Desktop.
Here is a minimal common code for Ping-Pong Game for Arduino - part 1/3
int n; // display row counter
void setup() {
for (int z = 2; z < 14; z++) {
pinMode(z, OUTPUT); // all pins output for driving the LED matrix
}
//digitalWrite(5, HIGH); // all pins output for driving the LED matrix
//digitalWrite(12, HIGH); // all pins output for driving the LED matrix
// intitialize interrupt timer 2
TCCR2A = 0; // TC2 Control Register A - no ocr or pwm ; normal operation
TCCR2B = 4; // TC2 Control Register b - Clock Select Bit 1<<CS22 | 0<<CS21 | 0<<CS20 //prescale 1:64
TIMSK2 = 1 << TOIE2; // TC2 Interrupt Mask Register - TOIE2: Timer/Counter2, Overflow Interrupt Enabled
TCNT2 = 0x06; // The counter value (TCNT2) are initialize in 6 decimal; see calculations below:
}
void loop() {
//delay(10000);
}
ISR(TIMER2_OVF_vect) { // Interrupt Routine:
TCNT2 = 0x06;
PORTD &= 0xE3; // clear PIN 2,3,4
PORTD |= (n << 2) & 0x1C; // set collumn trough the mux, see:
n++; // increase collumn counter
if (n > 7) { // count from 0 to 7
n = 0; // next image. Refreshrate = 1000/8 images/sec = 125Hz (8 x ISR)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment