I have a question regarding Phase correct PWM Mode on atmega328p. So it's a symetrical Timer, first upcounting until 255 and then downcounting until 0.
How to know for example for Timer0, when I read TCNT0, if the Timer is upcounting or downcounting ?
I am making an ESC based on an atmega328P and Timer0, 1 and 2 are already used by my mosfets driver for the 3 phases of the BLDC motor.
Micros() function is not usable because I am in Phase correct PWM mode, so I am trying to find a way to measure time.
Let say that TCNT0 = 200, if it is upcounting it is 200 timer ticks since last timer0 overflow, but if it is downcounting it is 255 + (255 - 200) = 310 timer ticks since last timer0 overflow.
So that's why I need to know if I am upconting or downcounting.
I don't know if it is any better than your method, but I think you should be able to work with the overflow flag which is automatically set at bottom and a compare match interrupt at top. If you clear the overflow flag in the isr at top, then you know that if the flag is set, you are up counting.