AnsweredAssumed Answered

Timer Output Compare Frequency Problem

Question asked by asl.sean on Dec 24, 2013
Latest reply on Dec 25, 2013 by Clive One
Hi , 

I am trying to generate an output pulse of 100 Hz to 300khz using time output compare where i will toggle a bit in the interrupt service routinue .

I referred to one of the example which states the following 

/* ---------------------------------------------------------------------------
    TIM9 Configuration: Output Compare Toggle Mode:
    TIM9CLK = SystemCoreClock (72MHz),
    The objective is to get TIM9 counter clock at 24 MHz:
     - Prescaler = (TIM9CLK / TIM9 counter clock) - 1
    CC1 update rate = TIM9 counter clock / CCR1Val = 732.4 Hz
    CC2 update rate = TIM9 counter clock / CCR2Val = 1464.8 Hz
  ----------------------------------------------------------------------------*/

I first attempted to generate a 250khz pulse . 

So i set my TIMCLK = 1 Mhz 

My CCRVal will be = 1Mhz / 4 = 250khz 

But i get 7.6Hz instead. 

My code are as follows. Thanks ! 

uint16_t CCR1_Val = 4;
uint16_t capture = 0;
  
#define TIM_CLK             RCC_APB1Periph_TIM2
#define TIM                 TIM2
#define TIM_IRQ             TIM2_IRQn
#define TIM_IRQ_HANDLER     TIM2_IRQHandler
  
#define TIM_GPIO_PORT           GPIOG
#define TIM_GPIO_PIN            GPIO_Pin_11
  
void Init_TIM (){   
  TIM_RCC_Configuration();
  TIM_NVIC_Configuration();
  TIM_Configuration();
}
  
void TIM_Configuration(){
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    uint16_t PrescalerValue = 0;
      
  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1;
  
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  
  TIM_TimeBaseInit(TIM, &TIM_TimeBaseStructure);
  
  /* Prescaler configuration */
  TIM_PrescalerConfig(TIM, PrescalerValue, TIM_PSCReloadMode_Immediate);
  
  /* Output Compare Timing Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  
  TIM_OC1Init(TIM, &TIM_OCInitStructure);
  
  TIM_OC1PreloadConfig(TIM, TIM_OCPreload_Disable);
  
  /* TIM IT enable */
  TIM_ITConfig(TIM2, TIM_IT_CC1,ENABLE);
  
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
  
}
  
void TIM_Disable(){
    /* TIM2 disable counter */
  TIM_Cmd(TIM, DISABLE);
}
  
void TIM_RCC_Configuration(void)
{
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}
  
void TIM_NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  
  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM_IRQ;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  
  NVIC_Init(&NVIC_InitStructure);
}
  
void TIM_IRQ_HANDLER(void)
{
  if (TIM_GetITStatus(TIM, TIM_IT_CC1) != RESET)
  {
    TIM_ClearITPendingBit(TIM, TIM_IT_CC1);
 
    GPIO_WriteBit(TIM_GPIO_PORT, TIM_GPIO_PIN, (BitAction)(1 - GPIO_ReadOutputDataBit(TIM_GPIO_PORT, TIM_GPIO_PIN)));
    capture = TIM_GetCapture1(TIM);
    TIM_SetCompare1(TIM, capture + CCR1_Val);
  }
}
 Thanks .

Outcomes