My not-so-simple example prints voluminous data of Timer2 registers, both data values and interpretation of those values, to assist me (and hopefully others) to see what I mean.
The output, with microsecond stamped snapshots, shows TCNT2 not incrementing properly, and TOV2 being set at first tick instead of at TOP tick.
i could just dump a bunch of hex values, but that then requires pawing over the hundreds of pages of ATmega328P datasheet to see what those hex values mean
But it is completely unreadable to most. Partly because you did not use <code> tags but i suspect blockquotes, and partly because there isn't an empty line between functions and loads of compiler directives. Keep in mind that the timer functions as it should, but you most likely have made a mistake somewhere.
A more appropriate method of ensuring nothing happens until you say so, is to turn off interrupts. The method of turning off the timer is usually done from within an ISR, which disables interrupts as well.
My suggestion is to clean up your code and reduce it down to a basic example first, and add all of the test results thereafter separately.
This code completely masks any errors, and you're unlikely to get any help if you insist on using it.
I'd recommend using an example that only starts a timer and handles an interrupt. I think the entire code should be no more than 20 lines long.
The whole point of this code is that it does NOT use ANY interrupts. I have a requirement for CPU-intensive signal processing so need minimal overheads in getting elapsed time between events. I got a shock when I analysed the multi-microsecond ISR overheads merely in pushing and popping registers. Getting the elapsed time is reduced to merely checking that if TOV2 is set then return 0xFF, otherwise return TCNT2; this gives 64uS ticks up to 16mS. And all that in a handful of inline instructions that's quicker than setting up an ISR. BTW I have been writing interrupt routines and operating systems since the 1970's.
Does that include the lines for setting up the seven Timer2 registers? Do I put them all in one one line? And i see complaints about no lines between function definitions.
If you are trying to start a timer in a non-standard way, then, even more so, remove all unnecessary things from the code. You especially need to show a minimal example so that those helping you can understand your idea.
Considering that you don't need anything other than starting the timer, not even an interrupt, then 20 lines will be enough even if you add 2 lines between the functions.
Pleas seek competent advice on how to programme an ATmega328P timer. Did you know you can select more than a handful of prescale values? And choose from multiple PWM, counter, modes? Select alternative TOP values? Select 510 ticks for 8 bit timer before Overflow flag is set?
BTW I have reposted a sub-40 line example to hopefully keep you & others happy.
This I have done with an edit to my first post. I've also added cli() & sei() to keep pedantics happy.
Same (but shorter) results, of course, but now requires reading the datasheet to see which bit of which register is the TOV2 flag. Or is this something that Arduino purists already know?
Never done this again - do not edit your previous posts if it already answered by somebody.
By doing this, you disrupt the flow of the discussion and, in principle, this are considered disrespectful to other correspondents.
If you want to change the code, paste it in a new message.
This allows an easy way to pick different prescale values; the code shown os for prescaler 7. If I want to change to use prescaler 5 then I change code to
the issue is that, according to my reading of the Atmel datasheet, TOV2 is being incorrectly set and TCNT2 is not being incremented on the first clock tick. This is mentioned in both the topic header and post preamble before the example code. That issue has not changed.
Both of these constructs are potentially wrong, and always misleading, because logical ORing with zero does not unset a bit that might already be set in the register.