Phase correct PWM Mode: How to know if upcounting or downcounting

Hello,

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 ?

For now I do this:

bool UpCounting = false;
uint8_t TimerValue = TCNT0;

if (TCNT0 > TimerValue)
UpCounting = true;

Is there another way ? I can't find in the datasheet any flag which indicates if it is upcounting or downcounting.

Thanks for your help !

Why do you care? What are you trying to do?

Hi,

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.