cancel
Showing results for 
Search instead for 
Did you mean: 

Timer using external clock

jonmatt
Associate II
Posted on January 22, 2012 at 21:00

I'd like to see some sample code (or a link to it) that utilizes a timer fed by an external clock, not the system clock, to generate an output pulse.  All ST's sample code that I've seen so far uses the system clock. Here's what I'm trying to do:

Input clock = 5.12MHz, 50% duty cycle

Output clock = 1KHz, very low duty cycle

Once set up, the timer needs to free run, that is, not generate interrupts or require further software to maintain.

Any help is much appreciated.

#stm32vldiscovery #external-clock #stm32-timer #pwm-output #timer #stm32vldiscovery #stm32vldiscovery
3 REPLIES 3
Posted on January 23, 2012 at 19:34

You should be able to use one of the TIMx_CHx pins to pull in an external clock (mode 1), or TIMx_ETR pins to pull in an external clock (mode 2).

That would supply the timebase, then program one of the channels in PWM where you can program the pulse width (nee duty). If you use prescalers/dividers of one you'd get a programmable granularity of ~195 ns.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jonmatt
Associate II
Posted on January 24, 2012 at 02:06

Thanks Clive. ~195nS granularity would be sweet!

I have been able to get a 1KHz output, but only in toggle mode, so 50% d/c.  PWM modes aren't working for me so far.  I'll try to post some setup code in a bit.

Jon

PS At the same time, I'm also using all 3 UARTS in interrupt mode; they work great!

jonmatt
Associate II
Posted on January 24, 2012 at 03:58

Here is code that I have working so far (using Atollic TrueStudio). The external clock I'm using here is actually 10.24MHz; it's divided down to 5.12MHz for use elsewhere.

  //Enable Clock for Timer4

  RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);

  // TIM4_CH1 pin (PB.06) configuration

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  // TIM4_CH2 pin (PB.07) configuration

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  //Time base configuration

  TIM_TimeBaseStructure.TIM_Period = 5120;

  TIM_TimeBaseStructure.TIM_Prescaler = 0;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  // External Trigger set to External Clock, Mode 1

  TIM_ETRClockMode1Config (TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);

  // Input Trigger selection 

  TIM_TIxExternalClockConfig (TIM4, TIM_TS_TI2FP2, TIM_ICPolarity_Rising, 0) ;

  // TIM4 PWM2 Mode configuration: Channel1 

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = 2000;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

  // Output compare preload enable

  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable) ;

  TIM_OC1FastConfig (TIM4, TIM_OCFast_Enable);

  // One Pulse Mode selection

  TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Repetitive);

  // Output Trigger selection 

  TIM_SelectOutputTrigger(TIM4, TIM_TRGOSource_OC1);

  // Slave Mode selection: Trigger Mode 

  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1);

  TIM_Cmd (TIM4, ENABLE);

Thanks for the help.

Jon