How can I start and stop a timer on STM32?

Sandeerius picture Sandeerius · May 11, 2016 · Viewed 19.2k times · Source

I have a big problem. I don't know how I can stop a timer with a button and restart the timer with another button.

This is the code that I have for it so far:


This code is the interrupt handler for the button that starts the timer. I thought that it is possible by enabling the timer, that works so far.

void EXTI0_1_IRQHandler(void)
{
    if ((EXTI->PR & EXTI_PR_PR1) == EXTI_PR_PR1)  /* Check line 1 has triggered the IT */
    {
        EXTI->PR = EXTI_PR_PR1; /* Clear the pending bit */
        NVIC_EnableIRQ(TIM7_IRQn);

    }
}

This code is the interrupt handler for the button that stops the timer. This piece of code doesn't work, and the timer stays on.

void EXTI4_15_IRQHandler(void)
{
    if ((EXTI->PR & EXTI_PR_PR4) == EXTI_PR_PR4)  /* Check line 1 has triggered the IT */
    {
        EXTI->PR = EXTI_PR_PR4; /* Clear the pending bit */
        NVIC_DisableIRQ(TIM7_IRQn);
    }
}

Does anyone have some tips or know how it must be?

Answer

051026luyaok picture 051026luyaok · May 11, 2016

I think “NVIC_DisableIRQ(TIM7_IRQn);” just disable the timer's interrupt but not stop the timer. You may need: "TIM_Cmd(TIM7, DISABLE);" instead of “NVIC_DisableIRQ(TIM7_IRQn);”