Hello.
I would like to use timer2 to count an amount of time and set a flag after this time has elapsed.
But no interruptions.
I find that CTC mode is what I want so I configure it like that:
void configureTimer2(void) {
// initialize timer2
noInterrupts(); // disable all interrupts
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
TCCR2A |= (1 << WGM21); // Configure timer 2 for CTC mode
TCCR2B |= (1 << CS22); // 1024 prescaler
TCCR2B |= (1 << CS20); // 1024 prescaler
TIMSK2 &= ~(1<<OCIE2A); //Disable timer compare interrupt.
OCR2A = 163; //163 // compare match to achieve 96Hz. Xtal 16Mhz, Prescaler 1024.
interrupts();
}
The timer is supposed to set to 1 some flag bit in the TIFR2 register.
Now what I do not know is which one of the bits is supposed to be on.
OCF2B, OCF2A or TOV2
My guess is that it is OCF2A but I had unexpected behaviours using that approach.
Something else sounds wierd to me:
"OCF2A can also be cleared by writing a logic one to the flag."
-To clear it, a 1 has to be written into, sound a bit extrange for me.
To check for this flag, what I do is:
*Polling for the bit OCF2A until it is 1.
*Clear the flag by writting again 1 into it.
*Do the stuff....
*Again Polling for the bit OCF2A until it is 1...
This way the "stuff" is done at a frequency of timer2 but with no Timer2 interruption.
Is it sensible, for some reasson it is not working and I will try to dig into it a little bit more tomorrow, I just wanted to share it to have a 2nd oppinion.
Ill post back if I find a solution.