Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I start and stop a timer on STM32?

Tags:

c

timer

stm32

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?

like image 512
Sandeerius Avatar asked May 11 '16 09:05

Sandeerius


2 Answers

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);”

like image 119
051026luyaok Avatar answered Oct 14 '22 21:10

051026luyaok


Using HAL, start:

HAL_TIM_Base_Start(&htim#);
HAL_TIM_Base_Start_IT(&htim#);

Stop:

HAL_TIM_Base_Stop(&htim#);
HAL_TIM_Base_Stop_IT(&htim#);

Where _IT is for timer interrupt mode. And you can reconfigure timer after stopping it.

like image 28
PolyGlot Avatar answered Oct 14 '22 22:10

PolyGlot