View previous topic :: View next topic |
Author |
Message |
JackB
Joined: 04 Mar 2016 Posts: 32 Location: Netherlands
|
SOLVED: pwm frequency too high (1.22Khz) on PIC18F at 20MHz |
Posted: Mon Mar 21, 2016 3:44 pm |
|
|
Hi,
I use ECCP1 to generate a PWM signal on a PIC18F67K90 at 20MHz.
Ideal, I would like a pwm frequency of about 100Hz.
However I get 1.22kHz at lowest.
However, the pwm_set_frequency function doesn't work, and the setting in #use pwm also doesn't work.
Is there a trick to get a lower than 1.22kHz pwm frequency?
Code: |
#use pwm(ccp1, OUTPUT=PIN_C2, FREQUENCY=100Hz)
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
pwm_set_frequency(100);
pwm_on();
set_pwm1_duty(200);
|
Last edited by JackB on Thu Mar 24, 2016 6:03 am; edited 4 times in total |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Mar 21, 2016 3:56 pm |
|
|
are you unable to use the HARDWARE CCP/ PWM based functions?
what you are trying to do is use a hidden ( to you ) system timer an a bunch of software to generate PWM - ugly IMHO
without the oscillator setup and fuses
no way to tell your problem really
i suggest you read the section entitled
SETUP_CCP1() on page 297 of the currrent CCS manual
it is KNOWN to work very well
i personally would NEVER use the software PWM because there is too much drain on system resources that is invisible to you at the source code level.
jus my opinion
Last edited by asmboy on Mon Mar 21, 2016 8:23 pm; edited 1 time in total |
|
|
JackB
Joined: 04 Mar 2016 Posts: 32 Location: Netherlands
|
|
Posted: Mon Mar 21, 2016 4:19 pm |
|
|
It works, but I get 1.22kHz at lowest.
I would like to get a pwm frequency of around 100-200Hz. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Mar 21, 2016 5:35 pm |
|
|
That's the lowest PWM frequency that you can get with that PIC running
at 20 MHz, using the hardware PWM module.
If you want lower, you will have to use software PWM. There are many
threads on this in the forum archives.
Use the forum's search engine to search for:
Set the search engine to
Quote: | Search for all terms |
http://www.ccsinfo.com/forum/search.php |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Mar 21, 2016 6:47 pm |
|
|
Quote: |
pwm frequency of around 100-200Hz.
|
but why so low a freq ???
what do you intend to do with the pwm generator?
does duty cycle of the pwm matter in your application? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19568
|
|
Posted: Tue Mar 22, 2016 1:37 am |
|
|
and, there is another way of going slower, if you only want one PWM.
Use a CCP, rather than CCP_PWM.
Have a look at the code I posted here:
<http://www.ccsinfo.com/forum/viewtopic.php?t=54739&highlight=1msec>
This generates 50Hz pulses adjustable from 1 to 2mSec.
the same approach can be done using the CCP triggering INT_CCP, rather than the polling approach shown. The reason for polling here was to allow the very long delay between pulses, but also good accuracy on the pulses themselves.
This is a 'half hardware' PWM.
The other similar approach, without the ability to do the very long delay needed here between pulses, is to use the CCP's ability to toggle a pin.
Generally PWM's, want to be fast. This is what the hardware is for. For slower pulses other approaches like programming a counter/interrupt are OK. The reason for the PWM hardware is to handle the cases where the pulse train is too fast to comfortably be handled by other approaches, without badly degrading the performance of the chip.
The CCP itself (not used as a PWM), can generate programmable delays from 200nSec to 0.209 seconds (from your master clock). With a interrupt routine to program the next delay needed each time, this can give PWM's down to a fraction of a Hz. |
|
|
JackB
Joined: 04 Mar 2016 Posts: 32 Location: Netherlands
|
|
Posted: Thu Mar 24, 2016 6:03 am |
|
|
Hi asmboy,
I understand higher PWM frequencies is often what you want or need.
However this is connected to a LT3791 Power LED driver.
This chip has it's own PWM (at a high frequency) to regulate the current through a Power LED.
To dim that LED, the chip uses a PWM (at a low frequency) on an input.
The datasheet specifies 100Hz PWM.
Hope this helps explaining why I was looking for a solution.
I now understand that 1.22kHz id really the lowest this PIC18F67K90 can do by hardware.
In tests, the chip behaves very well with input PWM frequencies if 1.25kHz and even 5kHz.
I can smoothly control the Power LED (100W) with a DMX signal.
So, I decided to go with 1.25kHz.
Thank you all for helping me again!
Regards,
Jack. |
|
|
|