/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / TIM4- OC Toggling frequency is inaccurate...

Username:     
Password:     
             

Forum

# 1   2011-04-13 07:32:53 TIM4- OC Toggling frequency is inaccurate...

saiRam
Member
Registered: 2011-03-21
Posts: 18

TIM4- OC Toggling frequency is inaccurate...

Hi,

I am trying to toggle 3 LEDs at rates of 30kHz, 35KHz and 40KHz. I am configuring the TIM4 for this purpose. I am using OC mode -Toggle for doing this.
When I connect Timer Channel pins to OScilloscope, I always see around 12.5Hz instead of the frequencies I configured. I am not sure if I am doing something
wrong here..

My SystemCoreClock is at 72MHZ and APB1 which drives TIM4 is at 36MHZ.

void Timer_CH_Toggle_Setup(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_OCInitTypeDef TIM_OCInitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE );

  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM4, ENABLE );

  GPIO_StructInit(&GPIO_InitStructure); // Reset init structure

  /* Remap PD13-15 to TIM4_CH2 to TIM2_CH4 respectively */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;            // Alt Function - Push Pull
  GPIO_Init( GPIOD, &GPIO_InitStructure );
  GPIO_PinRemapConfig( GPIO_Remap_TIM4, ENABLE );       

  TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );

  /* Prescaler Calculation: APB1 - 36MHZ
     TIM3 clock should be 840kHz. So, (36MHz/840KHz) = 43
   */
 
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = 43-1;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit( TIM4, &TIM_TimeBaseStructure );

 
  /* Output Compare Toggle Mode configuration: Channel4 */
    /* 840khz/28 = 30 khz */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 28;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC4Init( TIM4, &TIM_OCInitStructure );
  TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Disable);


  /* Channel 3 configuration  - 840kHz/24 = 35kHZ */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 24;
  TIM_OC3Init( TIM4, &TIM_OCInitStructure );
  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Disable);
 
  /* Channel 4 configuration - 840kHz/21 = 40 khz */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 21;
  TIM_OC2Init( TIM4, &TIM_OCInitStructure );
  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Disable);

  TIM_Cmd( TIM4, ENABLE );

  /* TIM IT enable */
  TIM_ITConfig(TIM4, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);

  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

  /* Enable the TIM3 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  return;
}


void TIM4_IRQHandler(void)
{
   /* TIM3_CH2 toggling with frequency = 366.2 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC2) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    capture = TIM_GetCapture2(TIM3);
    TIM_SetCompare2(TIM3, capture + 21);
  }

  /* TIM3_CH3 toggling with frequency = 732.4 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);
    capture = TIM_GetCapture3(TIM3);
    TIM_SetCompare3(TIM3, capture + 24);
  }

  /* TIM3_CH4 toggling with frequency = 1464.8 Hz */
  if (TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);
    capture = TIM_GetCapture4(TIM3);
    TIM_SetCompare4(TIM3, capture + 28);
  }

Offline

 

# 2   2011-04-13 08:35:28 TIM4- OC Toggling frequency is inaccurate...

saiRam
Member
Registered: 2011-03-21
Posts: 18

Re: TIM4- OC Toggling frequency is inaccurate...

Stupid me... The ISR was a copy+paste and the TIM3 is not changed to TIM4. I will change it and see what happens..

Offline

 

# 3   2013-05-08 07:26:12 TIM4- OC Toggling frequency is inaccurate...

FreedOx
New member
Registered: 2013-04-16
Posts: 7

Re: TIM4- OC Toggling frequency is inaccurate...

How are you managing the IRQ Handling please ? I want to manage the Overflow IRQ and i don^t find how ... if you have an idea ! Thanks smile

Offline

 

Board footer