cancel
Showing results for 
Search instead for 
Did you mean: 

Can you reset a timer counter from another timer's interrupt?

Richard Cooke
Associate II
Posted on November 20, 2017 at 04:30

Can you reset a counter from another timer?  I'm using TIM2 as a PWM input and in the callback() I'd like to reset the counter for TIM22 to stop it from triggering its interrupt.  Is this possible?

This is what I've tried but the TIM22 counter doesn't change:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)

{

    if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)

     {

        TIM22->CNT = 0;      // <--  This doesn't change the count.

        HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);         // gives me something to look at with the oscope

     }

}

Any help is greatly appreciated.

Richard

#timer-interrupts
4 REPLIES 4
Jan Waclawek
Senior II
Posted on November 20, 2017 at 06:58

TIM22->CNT = 0; // <-- This doesn't change the count.

How do you know?

JW

S.Ma
Principal
Posted on November 20, 2017 at 08:09

How about disabling the interrupt enable for the corresponding source? To me, playing with CNT is a bit crude. The less the HW gets discontinuities by SW, the easier it is to debug.

Posted on November 20, 2017 at 08:33

The OP neglected to tell us the mcu model he's using, but f there's a master-slave internal link between the timers, it could be used to reset the CNT. It exactly the same as writing zero to CNT so I fail to see why wouldn't that work.

JW

Posted on November 20, 2017 at 18:10

Hi Jan,

Sorry about not explaining my setup better.  I'm using the Nucleo32L031K6 board with Tim2 setup as a PWM input on Ch1 &Ch2.  I'm outputting a PWM using Tim21 and using Tim22 as a simple timer not tied to any pins or other timers.

I did finally get it working and I'm a bit embarrassed that I made such a rookie mistake but when I realized that I needed to make the period of Tim22 longer than the PWM input everything is working as planned.  When the PWM detects an input on PA0 the callback resets the counter for Tim22 so that never fires its interrupt.  As soon as the PWM input goes to zero the Tim22 timer times out and I get the HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) interrupt where I can clear my variable.

Thanks again,

Richard