View previous topic :: View next topic |
Author |
Message |
jvd
Joined: 23 May 2015 Posts: 3
|
10KHz-50KHz frequency using CCP in '46K22 |
Posted: Sat May 23, 2015 1:29 am |
|
|
I'm trying to write a program to generate frequencies from 20KHz to 50KHz using CCP. The problem is resolution. I need 0.1KHz steps but the best I get with a prescale of 1 and around 50 KHz is 0.7KHz. How can I solve this?
Thanks |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat May 23, 2015 5:43 am |
|
|
You don't tell us your clock frequency.
At 50kHz (20us) you're wanting resolution of 1 part in 500 (0.2%)
With a 40MHz clock you should have 25ns resolution for the PWM module.
That's 0.125% resolution.
Should be good enough.
Or am I missing something?
Mike |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
|
jvd
Joined: 23 May 2015 Posts: 3
|
|
Posted: Sun May 24, 2015 1:26 am |
|
|
Thank you both.
Mike Walne wrote: | You don't tell us your clock frequency. |
I calculated this in Excel for 16MHz.
By "resolution" I meant the steps of frequency.
As you can see in my table the next frequency available after 50000 Hz(PR2=80) is 49383 Hz(PR2=81).
I want to have 49900 Hz.
asmboy wrote: | use the code in this post but change to an 8mhz clock and you have better resolution than you need. |
I Haven't worked with NCO before.
I took a quick look and seems like it's duty cycle is fixed. Right? I need ECCP's duty cycle control and Half bridge+deadtime features.Looks like CWG can connect to NCO and offers Half bridge+deadtime, duty cycle is still a problem though. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun May 24, 2015 1:56 am |
|
|
He is not going to get the resolution he wants. Look for a chip with the programmable frequency generator(best) or the power PWM (slightly better than the standard PWM).
Problem is that the PWM is not designed as a variable frequency generator. The PWM _period generator_ source clock basically only has a 8bit resolution, with three prescalers (so potentially about 9.5bits). If he runs it off the Fosc/4 (fastest clock), then at 40MHz, the available frequencies, jump in as:
PR2=198 F=50251.256
PR2=199 F=50000
PR2=200 F=49751.24
Better than he is dealing with (presumably a slower master clock), but still jumps of over 250Hz. Then of course with the high master clock, he will only get down to 39062.5Hz, and then changing the prescaler to Fosc/16, gives at 20KHz:
PR2=123 F=20161.29
PR2=124 F=20000
PR2=125 F=19841.26
So the PWM, won't give him 100Hz resolution, but much better than his 0.7KHz...
However the power control PWM module, has a 12bit PTPER register, and this allows you to not have to change the prescaler and at the 20KHz frequency from 40MHz would then give:
PTPER=499 F=20000
PTPER=500 F=19960.07
Better than required.
However the step size at 50KHz is still the same.
Using 20MHz and the standard PWM, Fosc/4 would then get down to just under 20KHz:
PR2=249 F=20000
PR2=248 F=20161.29
So a step size of 161.29 Hz at this frequency, but at 50KHz:
PR2=99 F=50000
PR2=100 F=49504.95
So nearly 500Hz at this frequency. |
|
|
jvd
Joined: 23 May 2015 Posts: 3
|
|
Posted: Sun May 24, 2015 3:36 am |
|
|
Thank you very much Ttelmah.
I'll try to see if power PWM is good enough.
Do you have any suggestions for a chip with the programmable frequency generator? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun May 24, 2015 8:52 am |
|
|
Unfortunately at the moment I think this is only available on a couple of the PIC16's. Chips like the PIC16F1719.
Other comments also apply though - the faster the master clock, the smaller the step. Your PIC can go to 64MHz, and if you ran at (say) 50Mhz, and switched the prescaler as needed, then you could get:
PR2=249 F=50000
PR2=250 F=49800
/4 prescale
PR2=155 F=20032
PR2=156 F=19904
Switch to a PIC24, and several (most) have 16bit PWM's that can run off Fosc/2, with PLL's supporting in some cases Fosc up to 140MHz. Simpler since you don't then need to switch pre-scalers. |
|
|
|