cancel
Showing results for
Did you mean:

# How to Generate 0-500 Khz Variable Square Wave

Senior
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
8 REPLIES 8
Guru
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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 29, 2018 at 22:37

Try this:

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

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.

.

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
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...

Posted on January 30, 2018 at 03:59

Well yes, toggle would infer 50/50, and you'd run at twice the rate.

Problem is that a period of 11 cycles isn't going to toggle at 5.5, but would work with a 5 and 6 for mark/space, for example.

The integers tend to limit the available choices, you're likely to need to make some compromises and pick the best fit for the desired frequency, and this may include one that is marginally off the 50/50 duty.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Senior
Posted on January 30, 2018 at 09:07

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

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.