Hi Freaks,
I am running an M328 at 8MHz. I need an output of 4 MHz. I want this to be controllable as I am trying to sync multiple operations with this. To achieve this, I am using the timer interrupts. Here is what I have so far:
void timer_init() { TCCR1B |= (1 << WGM12); TIMSK1 |= (1 << OCIE1A); sei(); OCR1A = 1; TCCR1B |= (1 << CS10); } ISR(TIMER1_COMPA_vect) { PORTB ^= (1 <<PORTB5); //Set flag here and process in main }
However, with this PORTB5 shows a frequency of about 102 kHz. I checked the AVR frequency on the CLKO pin by setting the fuse and it does read 8MHz.
Why am I losing almost 78 cycles? Is this because of the XORing in the interrupt?
Is there a faster way to do this so that I can get 4MHz?
Thanks.