Skip to main content
gocchan
Associate III
January 11, 2022
Solved

Timer error

  • January 11, 2022
  • 2 replies
  • 1771 views

..

    This topic has been closed for replies.
    Best answer by AA1

    Hi,

    ARR value should be one less than the desired value. In your case instead of 250 use 249.

    TIM4_TimeBaseInit(TIM4_PRESCALER_64, 249);

    2 replies

    gocchan
    gocchanAuthor
    Associate III
    January 12, 2022

    I accidentally posted it blank.

    I'm using TIM4 to develop a prototype product application, and the interval between interrupt events is about 0.4%.

    With the minimum required implementation as shown below, the interrupt event occurrence interval was 1.004 ms (The ideal is just 1 ms).

    For a 15 minute timer, there will be a large gap of +3.6 seconds.

    INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
    {
    	GPIO_WriteReverse(GPIOD, GPIO_PIN_5);
    	TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
    }
     
    void main(void)
    {
    	CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
    	GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);
     
    	TIM4_DeInit();
    	TIM4_TimeBaseInit(TIM4_PRESCALER_64, 250);
    	TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
    	TIM4_Cmd(ENABLE);
     
    	enableInterrupts();
     
    	while (1)
    	{
    	}
    }

    The MCU is STM8S003F3U6TR.

    I am using a 16MHz external oscillator with a deviation of ± 30ppm.

    What causes this gap? Can software handle it?

    Regards,

    Gotoda

    AA1
    AA1Best answer
    Senior III
    January 15, 2022

    Hi,

    ARR value should be one less than the desired value. In your case instead of 250 use 249.

    TIM4_TimeBaseInit(TIM4_PRESCALER_64, 249);