cancel
Showing results for 
Search instead for 
Did you mean: 

Generate a square wave in output compare mode

christophe2
Associate II
Posted on March 10, 2015 at 14:34

Hi,

I'm using the STM32F411RE Nucleo board and I would like to generate

a 25 MHz square wave by using the output compare of timer3 in toggle mode.

Timer is configure as follow :

  TimHandle.Instance = TIM3;

  TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

  TimHandle.Init.Period = 65535;

  TimHandle.Init.Prescaler = 0;

  Config.OCMode = TIM_OCMODE_TOGGLE;

  Config.OCPolarity = TIM_OCPOLARITY_HIGH;

  Config.Pulse = 1;

  if(HAL_TIM_OC_ConfigChannel(&TimHandle,&Config,TIM_CHANNEL_1) != HAL_OK)

  {

    Error_Handler();

  }

  if(HAL_TIM_OC_Start_IT(&TimHandle,TIM_CHANNEL_1) != HAL_OK)

  {

    Error_Handler();

  }

The pulse value is updated in the callback function after two period of timer clock (cnt = capture compare), so that generate a 25 MHz which is half timer clock frequency.

But when i'm watching the output pin on oscilloscope, the signal appears to have a half period of 1.3 ms, which is the value of 65535 clock cycles, while i'm expecting to have a capture compare value incremented by one and changing the state of the pin every 20 ns.

Thanks in advance,

Regards

Christophe

1 REPLY 1
Posted on March 10, 2015 at 15:58

This is NOT how to generate 25 MHz

You'd need to pull the period in, and you're not going to be able to interrupt in the 10's of MHz

Either use an MCO pin (PA8 PLL/4) or use PWM mode.

The ''Pulse'' value controls the PHASE. The Period value the PERIOD

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..