## 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

medelec35
Valued Contributor
Posts: 8929
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

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

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
Flowchart does not use timer interrupts, using built-in PWM component instead.
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
Attachments
PWM Frequency Generator.fcfx
Martin

medelec35
Valued Contributor
Posts: 8929
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

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

Added Error trapping, if PWM frequency is set too high or too low in relation to Clock Frequency:
Error PWM.png
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
Martin

Steve001
Valued Contributor
Posts: 1149
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

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

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
Valued Contributor
Posts: 8929
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

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

Hi Steve,
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

Steve001
Valued Contributor
Posts: 1149
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

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

Thanks martin,

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

ie 4020, 4017

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

medelec35
Valued Contributor
Posts: 8929
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

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

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

Steve001
Valued Contributor
Posts: 1149
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

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

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
Valued Contributor
Posts: 8929
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Contact:

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

Hi Steve,
Would not spoil the weekend as it keeps me out of mischief.
When you do want help just let me know.
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

Steve001
Valued Contributor
Posts: 1149
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

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

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