cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 100 Khz Timer

giorgio2
Associate II
Posted on December 05, 2012 at 17:34

Hi guys,

I am new to the STM32 Micro and I am dealing with a STM32F0 discovery board. 

I am messing around with the TIMER peripheral trying to get a 400 Khz, however I have some issues with the TIM3 settings, below my code form TrueStudio:

 int main(void)

{

  /*!< At this stage the microcontroller clock setting is already configured, 

       this is done through SystemInit() function which is called from startup

       file (startup_stm32f0xx.s) before to branch to application main.

       To reconfigure the default setting of SystemInit() function, refer to

       system_stm32f0xx.c file

     */

  /* TIM Configuration */

  TIM_Config();

  

  /* -----------------------------------------------------------------------

    TIM3 Configuration: Output Compare Timing Mode:

    

    In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1),  

      => TIM3CLK = PCLK1 = SystemCoreClock = 48 MHz

   Prescaler Value = 2

   CCR4_VAL = 120

   CCR3_VAL = xxx Don't Care

  ----------------------------------------------------------------------- */   

  /* Compute the prescaler value */

  PrescalerValue = (uint16_t)(SystemCoreClock  / 24000000) - 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(TIM3, &TIM_TimeBaseStructure);

  /* Prescaler configuration */

  TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);

  /* Output Compare Timing Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  /* Output Compare Timing Mode configuration: Channel3 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

  TIM_OC3Init(TIM3, &TIM_OCInitStructure);

  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);

  /* Output Compare Timing Mode configuration: Channel4 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

  TIM_OC4Init(TIM3, &TIM_OCInitStructure);

  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);

   

  /* TIM Interrupts enable */

  TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE);

  /* TIM3 enable counter */

  TIM_Cmd(TIM3, ENABLE);

  /* Infinite loop */

  while (1)

  {

  }

}

in the lab I see that for values of CCR4_VAL = 240 I expect a wave of 100 Khz instead I measure 50 Khz for CCR4_VAL = 120 the waveform is  183.5 Hz (?????).

Can you help me,please to understand what the is going on ??? Do I miss something ???

Thnak you very much in advance
2 REPLIES 2
Posted on December 05, 2012 at 20:11

Conveniently you don't show your interrupt handler. One might guess that a) you are toggling the output, and thus halving the frequency observed, and b) saturating the processor with too many interrupts.

The smarter way would be to have the hardware generate the signal, not software interrupts.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
giorgio2
Associate II
Posted on December 06, 2012 at 08:38

Hi Clive1 thanks for your reply. Following my ISR code:

void TIM3_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM3, TIM_IT_CC3) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC3);

    STM_EVAL_LEDToggle(LED3);

    capture = TIM_GetCapture3(TIM3);

    TIM_SetCompare3(TIM3, capture + CCR3_Val);

  }

  else if(TIM_GetITStatus(TIM3, TIM_IT_CC4) != RESET)

  {

    TIM_ClearITPendingBit(TIM3, TIM_IT_CC4);

    STM_EVAL_LEDToggle(LED4);

    capture = TIM_GetCapture4(TIM3);

    TIM_SetCompare4(TIM3, capture + CCR4_Val);

  }

}

a) The frequency that I have measured with oscilloscope are genereated from the toggle of output ( sorry for the dummy question, you are right!!! )

b) My goal is to have a counter with frequency of 100:200 Khz, in order to produce delays of of 5:2,5 uS. I have used the example provided by TrueStudio to tune the TIM3 timer as ''timer counter'' but with no result. Maybe I should use the polling instead the interrupt ?

Thank you for your reply.