AnsweredAssumed Answered

About the output compare mode hunting

Question asked by yang.joonseok on Sep 10, 2014
Latest reply on Sep 11, 2014 by yang.joonseok
Hello Person who knows the solution.

I am Yang and I try to make cluster for the rpm output but I met the rpm hunting problem.
I need frequency modulation for the cluster.
The below are the code than I use.
STM32F407VG  with 8HMz external clock and 168Mhz is system clock after PLL.

I assign the PortC 4 for the output.
When I check the output with oscilloscope, some times it shows very short pulse and it makes the rpm gauage jupm up.
Is there something wrong in the STM32F407?
I use "STM32F4xx_StdPeriph_Driver".

I try to find the solution in the forum and I have a question about the "stm32f4xx_tim.c".
void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT)
{
  /* Check the parameters */
  assert_param(IS_TIM_ALL_PERIPH(TIMx));

  /* Clear the IT pending Bit */
  TIMx->SR = (uint16_t)~TIM_IT;
}

Is the command "TIMx->SR = (uint16_t)~TIM_IT;" correct?
If I want to clear the TIM_IT position, "TIMx->SR $= (uint16_t)~TIM_IT;" looks correct.

void timer5_set(void)
{
 PrescalerValue = (uint16_t) ((168000000 / 2) / 500000) - 1;

 // Time base configuration //
 TIM_TimeBaseStructure.TIM_Period = 65535;
 TIM_TimeBaseStructure.TIM_Prescaler = 0;
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

 TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);

 // Prescaler configuration //
 TIM_PrescalerConfig(TIM5, 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 = T3CCR1_Val;
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

 TIM_OC1Init(TIM5, &TIM_OCInitStructure);
 TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Disable);

 // Output Compare Timing Mode configuration: Channel4 //
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 TIM_OCInitStructure.TIM_Pulse = T3CCR2_Val;

 TIM_OC2Init(TIM5, &TIM_OCInitStructure);
 TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Disable);
 
 // Output Compare Timing Mode configuration: Channel4 //
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 TIM_OCInitStructure.TIM_Pulse = T3CCR2_Val;

 TIM_OC3Init(TIM5, &TIM_OCInitStructure);
 TIM_OC3PreloadConfig(TIM5, TIM_OCPreload_Disable);

 // Output Compare Timing Mode configuration: Channel4 //
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
 TIM_OCInitStructure.TIM_Pulse = T3CCR2_Val;

 TIM_OC4Init(TIM5, &TIM_OCInitStructure);
 TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Disable);

 //TIM Interrupts enable //
 TIM_ITConfig(TIM5, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);

 // TIM5 enable counter //
 TIM_Cmd(TIM5, ENABLE);
}

void TIM5_IRQHandler(void)
{
 if(TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)
 {
  TIM_ClearITPendingBit(TIM5, TIM_IT_CC1);
  if(B_ig_on)          GPIO_ToggleBits(GPIOD, GPIO_Pin_3);
  else             GPIO_SetBits(GPIOD, GPIO_Pin_3);

  capture1 = TIM_GetCapture1(TIM5);
  TIM_SetCompare1(TIM5, (u16)(capture1+cc1_val));  
 }
 else if(TIM_GetITStatus(TIM5, TIM_IT_CC2) != RESET)
 {
  TIM_ClearITPendingBit(TIM5, TIM_IT_CC2);
  if(B_ig_on)          GPIO_ToggleBits(GPIOE, GPIO_Pin_1);
  else             GPIO_SetBits(GPIOE, GPIO_Pin_1);

  capture2 = TIM_GetCapture2(TIM5);
  TIM_SetCompare2(TIM5, (u16)(capture2 + cc2_val));
 }
 else if(TIM_GetITStatus(TIM5, TIM_IT_CC3) != RESET)
 {
  TIM_ClearITPendingBit(TIM5, TIM_IT_CC3);
  if(B_ig_on)          GPIO_ToggleBits(GPIOE, GPIO_Pin_3);
  else             GPIO_SetBits(GPIOE, GPIO_Pin_3);
  
  capture3 = TIM_GetCapture3(TIM5);
  TIM_SetCompare3(TIM5, (u16)(capture3 + cc3_val));  
 }
 else if(TIM_GetITStatus(TIM5, TIM_IT_CC4) != RESET)
 {
  TIM_ClearITPendingBit(TIM5, TIM_IT_CC4);
  if(B_rpm)            GPIO_ToggleBits(GPIOC, GPIO_Pin_4); // && B_ig_on
  else              GPIO_SetBits(GPIOC, GPIO_Pin_4);
  
  capture4 = TIM_GetCapture4(TIM5);
  TIM_SetCompare4(TIM5, (u16)(capture4 + cc4_val));
 } 
}

Thank you.

Outcomes