How to Create a variable frequency output

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

How to Create a variable frequency output

Post by Jay Dee »

Hi Guys,
Anyone got any good tricks for creating a variable frequency output from a 18F Pic.

I would like to have a range of frequencies between 500Hz to 3kHz, I dont need any specific values, just a range of steps.
I also after about 50% duty but anything close will do.

The PWM module only allow you to change Prescaler to three settings.
Is there a known method of changing the Period overflow and Duty values, to vary the overall freq and keep a rough 50% pwm?

I would like to do it via the PWM component, rather than discreate lines of FC creating an oscillating output pin, since there are lot of other process going on and keeping the the timing tight for a software PWM could be tricky.
Thanks, J.

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

Re: How to Create a variable frequency output

Post by medelec35 »

Jay Dee wrote:I would like to do it via the PWM component, rather than discreate lines of FC creating an oscillating output pin, since there are lot of other process going on and keeping the the timing tight for a software PWM could be tricky.
You can use a timer interrupt to keep timing correct.
For example I created a Variable Frequency Generator 500Hz to 5KHz (100 Hz Steps)
If you are interested in it then if you post a flowchart with your target and clock settings, I can then convert it to FC7.

I could even give PWM component a bash?
Martin

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

Re: How to Create a variable frequency output

Post by Jay Dee »

Hi Martin,
Thanks for that, I did not follow all of it ! :D but it certainly triggered an idea for the use of interrupts, which was a great shout.
For anyone else playing along, attached is a very simple Interrupt based fixed-frequency output signal.

An interrupt macro counts up to a rollover value, on rollover, an output pin has its state flipped.... this repeats creating a 50% frequency output signal.
Change the value of TmrRollOver and you can change the Frequency.
18F2580_Timer_Freq_V2.fcfx
(15.26 KiB) Downloaded 374 times
//EDIT// This is correct file now, with a tweak to increase stability!
Thanks, it was just the nudge I needed to find a solution.
J.
Last edited by Jay Dee on Tue Aug 14, 2018 7:38 pm, edited 1 time in total.

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

Re: How to Create a variable frequency output

Post by medelec35 »

Hi Jay,
This may be of use for you?
Martin

User avatar
Jay Dee
Posts: 398
Joined: Tue Aug 18, 2009 6:42 pm
Has thanked: 121 times
Been thanked: 154 times

Re: How to Create a variable frequency output

Post by Jay Dee »

Thanks for the alternate PWM method Martin,
FYI, I previously posted the wrong file, swapped for correct one, which has also been tweaked to improve stability.
Its based on a PIC 18F2580 with 20Mhz Crystall so would need relevant tweaks if anyone wanted to use it on a alternate chip or clock freq. J.

Post Reply