Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by medelec35 »

Flowcode attached will generate a frequency of 500 to 3000Hz in 25Hz Steps.
Duty is maintained at 50%.
The frequency can go a lot higher as flowchart does not use lookup tables or is limited to clock frequencies.
Only limited to 3000Hz as that was requested in this post.
It will use any clock frequency, so can use with 8MHz internal oscillator or 19660800 external crystal e.t.c.
With 32MHz clock frequency, PWM frequency 100'sKHz can be achieved.
So long as the

Code: Select all

 TargetFrequencyHz
variable is set to match clock speed:
Clock frequency.png
(17.2 KiB) Downloaded 4676 times
Flowchart does not use timer interrupts, using built-in PWM component instead.
Accuracy is about +/-10Hz.
The flowchart is educational as if you follow the maths you will learn for a PWM duty of 50%:
how to calculate PWM Period value, required duty value and prescaler value for a given frequency.
Hope this is of some use:
Frequency Gen.png
(211.25 KiB) Downloaded 4646 times
Attachments
PWM Frequency Generator.fcfx
(14.13 KiB) Downloaded 421 times
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by medelec35 »

Added Error trapping, if PWM frequency is set too high or too low in relation to Clock Frequency:
Error PWM.png
(47.85 KiB) Downloaded 4624 times
The PWM frequency is set directly in HZ.
The formula

Code: Select all

PWMFrequencyHz = (PWMFrequencyHz * 39 / 99) * 25 + 500
is only to set the PWM frequency via pot from 500 to 3000Hz
Any other range will require either:
1. Different formula.
2. Look-up table.
3. Map function when available (Easiest).

For accuracy the lower the period overflow, the less accurate PWM frequency will be.
That's dure to PWM frequency being set higher than it should be.
For the attached flowchart, assuming clock frequency is 19660800Hz.
Then if period overflow is 30, the PWM frequency will be approximately

Code: Select all

Clock frequency/(Prescaler*(Period Overflow+1))/4
= 158554.8Hz or 158KHz.
At 32MHz, Max PWM = 258064.5161 or 258KHz
If you want higher frequencies then you will need to lower the 30 in the Decision branch:

Code: Select all

If .PeriodOverflow < 30
You can't change the lowest value PWM will go as that is set internally within the microcontroller.
Attachments
PWM Frequency Generator V2.fcfx
(17.09 KiB) Downloaded 414 times
Martin

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by Steve001 »

Thanks Martin,

You mentioned in Jays Post you might be able to convert to V7, Would that be possible ? as i dont have V8
Can think of lots of uses for testing :) how easy would it be to make the frequency go down to 25 Hz

I ask before having a look at the chart sorry . Not to familiar with PWM Either :(

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by medelec35 »

Hi Steve,
Your welcome.
I have designed the flowchart to work with V7.
Like i stated the minimum frequency is not determined by my error settings but by the hardware itself.
THe only way to try is to set the clock speed for as low as possible e.g. 2MHz.
Not tried it, so cant sat if that clock speed is still too high for 25Hz. my guess it is.
So you will need to use timer interrupt for a PWM frequency that low.
That will be based on the link I posted above.
Martin

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by Steve001 »

Thanks martin,

would a johnson counter work on the output ? as the duty is 50% ish

ie 4020, 4017

https://www.ti.com/lit/ds/symlink/cd4017b.pdf

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by medelec35 »

Steve001 wrote:would a johnson counter work on the output ?
I don't see why not.
Also as the PWM is directly in Hz. there is now reason why you cant use a keypad fir direct entry, ur use two switches.
One to increment say 10Hz on short press or rapidly increase (decrease with another switch) frequency.
If you want to set up the hardware, I can help you to progress.

Don't forget as a VC member you can get very good discounts on Matrix products.
So maybe worth sending Steve a PM to make enquiries about V8?

Edit: Just thought.
We may be able to to this without additional hardware?
I could try and use a timer interrupt, but instead of being dependant on Clock frequency I may be able to make it independent.
That way you could have Hz to 100KHz and will work for 8MHz or 32MHz clock.
I could have a bash at it this weekend, no promises.
Martin

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by Steve001 »

medelec35 wrote:
Steve001 wrote:would a johnson counter work on the output ?
That was just an idea as i have several of those kicking about
I could have a bash at it this weekend, no promises.[/quote]

Thank you but please dont spoil your weekend, its only an idea i have at the moment

Steve
Success always occurs in private and failure in full view.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by medelec35 »

Hi Steve,
Would not spoil the weekend as it keeps me out of mischief. :lol:
When you do want help just let me know.
I had an idea.
If a keypad is used, then there could be various codes used.
e.g 1 for sweep down.
2 For sweep up.
3 To enter sweep lowest value.
4 To enter sweep lowest value.
5 Enter Step value.
6 Decrement.
7 Increment.
Short press # to Decrement by the step value.
Short press * to Increment by the step value.
Long press # to for Enter.
Long press * to delete last digit.
Directly enter a frequency then Long press # to for Enter.

Etc, Etc.
Martin

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: Frequency Generator 500 to 3000Hz (Expandable) in 25Hz Steps

Post by Steve001 »

Hi Martin
medelec35 wrote: Would not spoil the weekend as it keeps me out of mischief. :lol:
Your as bad as me :D

Like the ideas, not sure when i will get round to having a play.
Thank you for the offer of help.Very much appreciated as always

Steve
Success always occurs in private and failure in full view.

Post Reply