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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

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

Postby medelec35 » Tue Aug 14, 2018 3:43 pm

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
Clock frequency.png (17.2 KiB) Viewed 2698 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
Frequency Gen.png (211.25 KiB) Viewed 2668 times
Attachments
PWM Frequency Generator.fcfx
(14.13 KiB) Downloaded 85 times
These users thanked the author medelec35 for the post (total 6):
kersing (Tue Aug 14, 2018 4:03 pm) • Jay Dee (Tue Aug 14, 2018 5:41 pm) • LeighM (Wed Aug 15, 2018 8:09 am) • Benj (Wed Aug 15, 2018 4:36 pm) • jgu1 (Thu Aug 16, 2018 2:30 pm) • astro1234 (Sat Aug 18, 2018 8:02 am)
Rating: 31.58%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

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

Postby medelec35 » Thu Aug 16, 2018 9:16 am

Added Error trapping, if PWM frequency is set too high or too low in relation to Clock Frequency:
Error PWM.png
Error PWM.png (47.85 KiB) Viewed 2646 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 93 times
These users thanked the author medelec35 for the post (total 3):
kersing (Thu Aug 16, 2018 1:19 pm) • jgu1 (Thu Aug 16, 2018 2:32 pm) • Steve001 (Thu Aug 16, 2018 8:16 pm)
Rating: 15.79%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1086
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 440 times
Been thanked: 492 times
Contact:

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

Postby Steve001 » Fri Aug 17, 2018 5:06 pm

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.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

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

Postby medelec35 » Fri Aug 17, 2018 5:37 pm

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.
These users thanked the author medelec35 for the post:
Steve001 (Sat Aug 18, 2018 7:44 am)
Rating: 5.26%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1086
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 440 times
Been thanked: 492 times
Contact:

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

Postby Steve001 » Sat Aug 18, 2018 7:51 am

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.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

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

Postby medelec35 » Sat Aug 18, 2018 11:31 am

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.
These users thanked the author medelec35 for the post:
Steve001 (Sat Aug 18, 2018 5:58 pm)
Rating: 5.26%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1086
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 440 times
Been thanked: 492 times
Contact:

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

Postby Steve001 » Sat Aug 18, 2018 6:03 pm

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.

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8440
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2412 times
Been thanked: 3450 times
Contact:

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

Postby medelec35 » Sun Aug 19, 2018 5:04 pm

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.
These users thanked the author medelec35 for the post (total 2):
Steve001 (Sun Aug 19, 2018 7:31 pm) • Benj (Tue Aug 21, 2018 5:44 pm)
Rating: 10.53%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1086
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 440 times
Been thanked: 492 times
Contact:

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

Postby Steve001 » Sun Aug 19, 2018 7:35 pm

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
These users thanked the author Steve001 for the post (total 2):
medelec35 (Sun Aug 19, 2018 7:37 pm) • Benj (Tue Aug 21, 2018 5:44 pm)
Rating: 10.53%
 
Success always occurs in private and failure in full view.