volatile uint8_t timer0_count = 0;
volatile uint8_t timer2_count = 0;
void setupTimer0() {
noInterrupts();
// Clear registers
TCCR0A = 0;
TCCR0B = 0;
TCNT0 = 0;
OCR0A = 155; // 1 Hz
TCCR0A |= (1 << WGM01); // CTC
TCCR0B |= (1 << CS02) | (1 << CS00);
TIMSK0 |= (1 << OCIE0A); // Output Compare Match A Interrupt Enable
interrupts();
}
void setupTimer1() {
noInterrupts();
// Clear registers
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 7812; // 2 Hz
TCCR1B |= (1 << WGM12); // CTC
TCCR1B |= (1 << CS12) | (1 << CS10);
TIMSK1 |= (1 << OCIE1A); // Output Compare Match A Interrupt Enable
interrupts();
}
void setupTimer2() {
noInterrupts();
// Clear registers
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
OCR2A = 155; // 5 Hz
TCCR2B |= (1 << WGM21); // CTC
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
TIMSK2 |= (1 << OCIE2A); // Output Compare Match A Interrupt Enable
interrupts();
}
void setupTimer3() {
noInterrupts();
// Clear registers
TCCR3A = 0;
TCCR3B = 0;
TCNT3 = 0;
OCR3A = 1562; // 10 Hz
TCCR3B |= (1 << WGM32); // CTC
TCCR3B |= (1 << CS32) | (1 << CS30);
TIMSK3 |= (1 << OCIE3A); // Output Compare Match A Interrupt Enable
interrupts();
}
void setup() {
// put your setup code here, to run once:
DDRC = 0XFF;
PORTC = 0x00;
setupTimer0();
setupTimer1();
setupTimer2();
setupTimer3();
}
void loop() {
// put your main code here, to run repeatedly:
}
// Timer0 ISR - 1 Hz (100Hz base divided by 100)
ISR(TIMER0_COMPA_vect) {
timer0_count++;
if (timer0_count >= 100) { // Divide by 100 to get 1 Hz
timer0_count = 0;
PORTC ^= (1 << PC0) | (1 << PC1); // Toggle pins PC0 and PC1
}
}
// Timer1 ISR - 2 Hz
ISR(TIMER1_COMPA_vect) {
PORTC ^= (1 << PC2) | (1 << PC3); // Toggle pins PC2 and PC3
}
ISR(TIMER2_COMPA_vect) {
timer2_count++;
if (timer2_count >= 20) { // Divide by 20 to get 5 Hz
timer2_count = 0;
PORTC ^= (1 << PC4) | (1 << PC5); // Toggle pins PC4 and PC5
}
}
// Timer3 ISR - 10 Hz
ISR(TIMER3_COMPA_vect) {
PORTC ^= (1 << PC6) | (1 << PC7); // Toggle pins PC6 and PC7
}