cancel
Showing results for 
Search instead for 
Did you mean: 

Strange tim4 interrupts?

hpare
Associate

The chip is stm8s103f3p6, and I wanna use lsi as clk and tim4 to make a timer, there are two strange things happened.

  1. when I set prescaler to TIM4_PRESCALER_4 and ARR to 32, I thought it should interrupt every 1ms (128k/4/32=1kHz), but it seems break every 2ms.
  2. when I set prescaler to TIM4_PRESCALER_4 and ARR to 16, it always freezes at enableInterrupts() ...

I have already set LSI_EN to enable, can anyone help find what happened?

Thx

the code:

unsigned long elapseTime = 0;
 
 
void DelayMS(unsigned int ms) {
  unsigned long lmttime = elapseTime + ms;
  while(lmttime >= elapseTime);
  return;
}
 
int main( void )
{
  CLK_DeInit();
  
  CLK_LSICmd(ENABLE);
  CLK_ClockSwitchConfig(CLK_SWITCHMODE_MANUAL, CLK_SOURCE_LSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
  while(CLK_GetFlagStatus(CLK_FLAG_LSIRDY)==RESET);
  CLK_ClockSwitchCmd(ENABLE);  
  while(CLK_GetSYSCLKSource() != CLK_SOURCE_LSI){
  }
  
  TIM4_DeInit();
  TIM4_TimeBaseInit(TIM4_PRESCALER_4, 32);
  TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
  TIM4_SetCounter(0x00);
  TIM4_Cmd(ENABLE);
  
 
  GPIO_DeInit(GPIOB);
  GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);
  
  GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
  
  enableInterrupts();
  
  while (1) {
    DelayMS(2000);
    GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
  }
  
  return 0;
}
 
 INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
 {
   elapseTime++;
   TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
 }

1 REPLY 1
AAntu.1781
Associate II

Hello, I have the same problem. Managed to solve?