Page 1 of 1

How to Create a variable frequency output

Posted: Mon Aug 13, 2018 7:56 pm
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.

Re: How to Create a variable frequency output

Posted: Mon Aug 13, 2018 10:30 pm
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?

Re: How to Create a variable frequency output

Posted: Tue Aug 14, 2018 2:54 pm
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 345 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.

Re: How to Create a variable frequency output

Posted: Tue Aug 14, 2018 3:45 pm
by medelec35
Hi Jay,
This may be of use for you?

Re: How to Create a variable frequency output

Posted: Tue Aug 14, 2018 7:41 pm
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.