void timer1CTC_Init(int max_count, int my_div) {
TCCR1A = 0;
TCCR1B = 0;
// clear all bits
// clear all bits
OCR1A = max_count- 1; // maximum count- 1
TCNT1 = 0x0;
TIFR1 = 0x7;
TIMSK1 = 0;
TCCR1B |= 0x8;
if (my_div == 1) {
// reset counter
// clear all interrupt flags
// disable all interrupts call to MCU
// set WGM12 to 1, mode 4 CTC
TCCR1B |= 0x01; // set CS to dividy clk by 1
} else if (my_div == 8) {
TCCR1B |= 0x02; // set CS to dividy clk by 8
} else if (my_div == 64) {
TCCR1B |= 0x03; // set CS to dividy clk by 64
} else if (my_div == 256) {
TCCR1B |= 0x04; // set CS to dividy clk by 256
} else if (my_div == 1024) {
TCCR1B |= 0x05; // set CS to dividy clk by 1024
}
}
void setup() {
DDRB = 0xF0;
// set PB7:4 to output
PORTB &= 0x0F; // clear PB7:4
// set timer 1 count frequency to 2 Hz (0.5 second)
// by counting 31250 times at 62500 Hz (16 MHz divided by 256)
timer1CTC_Init(31250 ,256);
}
void loop() {
if (TIFR1 & 0x2) { // check OCR1A match interrupt
}
TIFR1 = 0x2; // clear OCF1A match interrupt flag
PORTB ^= 0x80; // toggle PB7
}