AnsweredAssumed Answered

Is this the right way to generate a one shot delay using TIM4?

Question asked by Ahmed Talaat on Oct 29, 2014
Latest reply on Oct 30, 2014 by Ahmed Talaat
I am trying to create a debounce delay (10ms at 8MHz clock) using TIM4CH1. Is the following the right way to do it?

The TIM4_Configuration function is called at program startup, then whenever I need to delay (when within the EXTI ISR), I call the enableDebounceTimer function which generates an interrupt upon timer expiration.

Within the Timer ISR, first thing I do is to call disableDebounceTimer function to disable the timer.

void TIM4_Configuration (){
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
NVIC_InitTypeDef NVIC_InitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE );
    /* Enable the TIM4 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    /* Time base configuration */
    TIM_TimeBaseInitStruct.TIM_Period = 65535;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = 80;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct);
    /* Output Compare Timing Mode configuration: Channel1 */
    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_Timing;
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_Pulse = 1000;                  // Core Clock 8Mhz, we need 10ms interrupt
    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
    /* configure output compare of TIM4 CH1 */
    TIM_OC1Init(TIM4, &TIM_OCInitStruct);
 * Loads the timer with a 10ms debouce delay, and configure it to generate an interrupt once expired
void enableDebounceTimer(void) {
    /* TIM4 enable counter */
    TIM_SetCounter(TIM4,0);                         // Reset its counter
    TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);         // Enable TIM Interrupt
    TIM_Cmd(TIM4, ENABLE);                          // Enable Timer
 * Disable timer, and clear all interrupts
void disableDebounceTimer(void) {
        TIM_Cmd(TIM4, DISABLE);                     // Disable Timer
    TIM_ITConfig(TIM4, TIM_IT_CC1, DISABLE);        // Enable TIM Interrupt
    TIM_SetCounter(TIM4,0);                     // Reset its counter
    TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);        // Clear any pending interrupt bit so that we do not come here again