View previous topic :: View next topic |
Author |
Message |
Ralf2
Joined: 05 Feb 2009 Posts: 10
|
Issue pwm_set_frequency() :( |
Posted: Thu Aug 01, 2013 7:42 am |
|
|
Hi friends
This program not work to any frequency, if use: pwm_set_frequency()???
Code: |
#include <18f252.h>
#use delay(crystal= 20000000)
#fuses hs, nowdt,ccp2c1
#use pwm(ccp1,timer=2, output=pin_c2, frequency=10000)
void config_pwm(void)
{
pwm_on();
pwm_set_frequency(5000); //here problem, not work :(
pwm_set_duty_percent(700UL);
}
void main()
{
config_pwm();
while(true)
{
output_high(pin_b0);
delay_ms(200);
output_low(pin_b0);
delay_ms(100);
}
}
|
Any idea?
Thanks |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Aug 02, 2013 12:26 am |
|
|
I've been playing with it, based on your program, with vs. 5.010.
I have come to the conclusion that this function does not work:
Code: | pwm_set_frequency() |
With a 20MHz crystal I always get 1.22 KHz for the frequency, no matter
what parameter I put into that function. You can set the frequency in the
original #use pwm() statement. That works. But you can't change it later
because pwm_set_frequency() is buggy. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19368
|
|
Posted: Fri Aug 02, 2013 12:42 am |
|
|
and (of course) remember that you can set the frequency using a setup_timer_2 line at any point, but you have to do the calculations...
It is always going to be a 'dubious' function, since the actual frequencies that can be achieved are limited, and changing this will alter the PWM resolution. Seems a case of the compiler trying to be a bit 'too smart'. Except possibly on chips with the 12bit PWM's, where more accurate frequencies are possible.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Ralf2
Joined: 05 Feb 2009 Posts: 10
|
|
Posted: Fri Aug 02, 2013 6:17 am |
|
|
PCM programmer wrote: | I've been playing with it, based on your program, with vs. 5.010.
I have come to the conclusion that this function does not work:
Code: | pwm_set_frequency() |
With a 20MHz crystal I always get 1.22 KHz for the frequency, no matter
what parameter I put into that function. You can set the frequency in the
original #use pwm() statement. That works. But you can't change it later
because pwm_set_frequency() is buggy. |
With a 20MHz crystal I always get 1.22 KHz for the frequency, no matter
what parameter I put into that function. --> YES, so is
You can set the frequency in the
original #use pwm() statement. That works. But you can't change it later
because pwm_set_frequency() is BUGGY. -->YES, so is
Thanks PCM programmer, you're right
and (of course) remember that you can set the frequency using a setup_timer_2 line at any point, but you have to do the calculations...
So is Ttelmah, Thanks
Last edited by Ralf2 on Fri Aug 02, 2013 8:54 am; edited 3 times in total |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Fri Aug 02, 2013 6:25 am |
|
|
if you want arbitrary frequency, or one hz ( even fractional hz) resolution
then the NCO module of the 16f15xx parts
can't be beat.
it is very easy to adapt over a great range of Fosc
and many decades of 1 hz of resolution too
and gives terrific accuracy with a power of two crystal.
see:
http://www.ccsinfo.com/forum/viewtopic.php?t=50121 |
|
![](templates/subSilver/images/spacer.gif) |
|