cancel
Showing results for 
Search instead for 
Did you mean: 

MHz frequencies on OUTPUT

Ala
Senior

hey there

I have a stm32f103 blue pill board which I wish to generate MHz frequencies on pinC13.

I have used TIM1. it starts and interrupts and on each interrupt I toggle PC13

like so

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	 if(htim->Instance == TIM1)
	 {
				HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
	 }
 }

also I set APB1 to 64MHz in Cube.

and here is my TIM1 config(this part is generated by Cube so I just bring up the Prescaler and Period part )

htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 1;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;

as you can see with these Prescaler and Period I should have frequency around 64000000/(0+1)(1+1)~32MHz

but on Oscilloscope I see around 125kHz!

I know my oscilloscope TIM/DEV is limited to us but at least I expect to see to parallel lines showing high frequency

what is the problem?

4 REPLIES 4

You should use the TIM to drive/toggle pin a directly, without interrupts, if you don't want to saturate the MCU.

D​epending on how efficient your interrupt code is, a few hundred KHz is the ceiling.

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

@Community member​ "You should use the TIM to drive/toggle pick a directly, without interrupts, if you don't want to saturate the MCU."

you mean should I use PWM connected to TIM1?

"D​epending on how efficient your interrupt code is, a few hundred KHz is the ceiling."

I guess it is quit efficient, cause I have nothing else in my code...

so how can I generate MHz frequencies on a pin?

As Clive said, multi-megahertz interrupt rates on STM32 are unrealistic.

Use PWM.

JW

Should have read pin, not pick, darn auto-complete..

The TIM should have PWM and Toggle modes. Doesn't have to be TIM1, pick a TIMx/CHx associated with a pin.

>>I guess it is quite efficient, cause.. 

You're using HAL, the processor is counting instructions, figure at least 2x the friction there.

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