AnsweredAssumed Answered

Toggle mode confusion

Question asked by shrestha.durgesh on Nov 6, 2011
Latest reply on Nov 7, 2011 by Clive One
my IDE software shows following clock summary for my code:

HSECLK :8 Mhz
SYSCLK :72 Mhz
HCLK   :72 Mhz
PCLK1  :18 Mhz
PCLK2   :72 Mhz

I have following codes (only necessary lines are mentioned):

__IO uint16_t CCR1_Val = 1;
uint32_t PrescalerValue = 0;


under main after setting clock, gpio and nvic:

  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);



  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);

  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);
 
 
    
  TIM_Cmd(TIM3, ENABLE);


  TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);   //CCR1 for output compare
 
  while (1)
  {}
//end main


and under the ISR:

void TIM3_IRQHandler(void)
{
 if(TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)
    {
    
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
    
    capture=TIM_GetCapture1(TIM3);
    
    TIM_SetCompare1(TIM3, capture + CCR1_Val);
    
    }
}

So i get a pulse of width 625 us (micro second) and the period is around 1300 us.

I had understood the resolution of the timer is 90000/72Mhz=1.25ms
So when CCR1_val = 1, the timer interrupt should be accessed every 1.25ms.
So the pulse width should be 1.25ms but it is half.

Also if i increase the value of CCR1_val=2 the whole period is doubled and the pulse width becomes doubled and if CCR1_val=3 the pulse period is 4 ms and widht as 2 ms.

Can anyone explains me why is it so. Specially the toggling mode is not understandeable for me.
I had believed CCR1 value multiplied by the resoulution would be the time between which the interrupt service routine is served.
Accordingly the pulse widht had to be the duration specified by CCR1 but here it seems that the whole period is the duration indicated by CCR1 and the pulse width is always with 50% duty cycle.

Further if i make the following changes:
RCC_PCLK1Config(RCC_HCLK_Div2); //which was Div4 previously
CCR1_val=3;
then the pulse width is 1ms and pulse period is 2 ms.
I would be glad if someone could explain me.

Outcomes