Skip to main content
ferhatyol-23
Senior
January 29, 2018
Question

How to Generate 0-500 Khz Variable Square Wave

  • January 29, 2018
  • 4 replies
  • 5567 views
Posted on January 29, 2018 at 22:03

Hi

I need to produce 0-500 kHz square wave on my STM32F103. How can we simply do this?

Thanks

#frequency-generator #generator #stm32f103 #frequency #stm32
This topic has been closed for replies.

4 replies

Tesla DeLorean
Guru
January 29, 2018
Posted on January 29, 2018 at 22:34

The integer dividers make this a little less than ideal but basically you need to factor

FreqOut = TIMCLK / (P * Q); // TIMCLK is nominally 72 MHz for either APB on F1

Where

Prescaler = Q-1; // And 0-65535, 16-bit

Period = P-1; // Again 0-65535 all F1 timers 16-bit

...

Pulse = P / 2; // 50/50 Duty

Simple? Maths a teenager old should be able to do.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
embedded_eng
Associate
November 10, 2023

It is simple when you only do the math.

But in circuitry, it fails to keep up with square wave. Because it affects other parameters and the square wave signal distorts and trensforms into pwm signal.

Bogdan Golab
Lead
January 29, 2018
Posted on January 29, 2018 at 22:37

Try this:

http://www.st.com/resource/en/application_note/cd00259245.pdf

 
T J
Senior III
January 29, 2018
Posted on January 30, 2018 at 00:37

Hi,

you could use a timer for 50% duty cycle,

any other duty cycle you would definitely want to use the PWM function.( which is the same but slightly different)

you would run the timer at the highest possible rate to give you the most steps upto 500KHz

set the Output Compare to toggle pin on overflow

no interrupts are needed,

this is a classic function of the timers.

.

Tesla DeLorean
Guru
January 30, 2018
Posted on January 30, 2018 at 03:07

The Period div 2 does however allow for odd periods where you are one cycle shy of 50/50, slewed in favour of a shorter mark vs space

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
T J
Senior III
January 30, 2018
Posted on January 30, 2018 at 03:31

I thought that when you use the timer overflow function with toggle, it would be inherently exactly 50/50 duty cycle...

ferhatyol-23
Senior
January 30, 2018
Posted on January 30, 2018 at 09:07

Thank you for the all reply, I will try this.