View previous topic :: View next topic |
Author |
Message |
z1rqdym
Joined: 14 Dec 2014 Posts: 24
|
Power PWM on CCS C - How to calculate freq and duty cycle |
Posted: Fri Feb 06, 2015 7:33 am |
|
|
Hello,
don't tell me read the datasheet of the PIC18FXX31.
I read it. but there is no way to use on CCS C.
from the index i found these functions.
Code: |
setup_power_pwm(config)
Sets up the PWM clock, period, dead time etc.
setup_power_pwm_pins(module x)
Configure the pins of the PWM to be in
Complimentary, ON or OFF mode.
set_power_pwmx_duty(duty)
Stores the value of the duty cycle in the PDCXL/H register. This duty cycle value is the time for which the PWM is in active state.
set_power_pwm_override(pwm,override,value)
This function determines whether the OVDCONS or the PDC registers determine the PWM output .
|
in there i will know how to calculate the frequency and duty cycle with, time base, period, compare, postscale etc.
what is the formula for the Power PWM on CCS C ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Fri Feb 06, 2015 10:05 am |
|
|
It's in the data sheet!.....
Actually it is. The point is that the data sheet tells you how the numbers needed are calculated. These are the same numbers the CCS functions use....
So (for instance), if you are using a 40MHz clock, and want 10bit PWM resolution, the 'PTPER' value needs to be 0x1FF (section 17.5 in the data sheet). The 'PTPER' is the PWM Timer Period.
The period value in the setup_power_pwm function, _is_ this value from the data sheet.
etc. etc... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Feb 06, 2015 3:27 pm |
|
|
This thread has an example:
http://www.ccsinfo.com/forum/viewtopic.php?t=37206
The 18F4431 data sheet gives this formula for the period in Equation 18-1:
Code: |
(PTPER + 1) x PTMRPS
TPWM = ----------------------
FOSC/4
|
Invert it to get the frequency formula:
Code: |
FOSC/4
PWM Frequency = ----------------------
(PTPER + 1) x PTMRPS
|
Put in values from my example in the link:
Code: | 8 MHz /4
PWM Frequency = ---------------------- = 1 KHz
(1999 + 1) x 1
|
----
Edit: corrected spelling typos
Last edited by PCM programmer on Fri Feb 06, 2015 4:37 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Feb 06, 2015 4:20 pm |
|
|
Actually you really should read the datasheet. It's the ONLY way you'll understand the power of the PIC and how to use it correctly. Blindly assuming a C function actually works properly is not a good idea. Some programmers aim for speed, some reduced code space, others portability. Others create 'bloatware' that on the surface work but are terribly inefficient.
If you take the time to see how the PIC works, you'll become a far better programmer. CCS does a great job overall but if you dump a listing to see how they code a function, you may see another way to do it, maybe faster, less code space,??
just food for thought
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sat Feb 07, 2015 1:42 am |
|
|
Also, to add to Temtronics comment, the only thing that can explain the 'limitations' of the functions, is the data sheet.
So you get users on some of the basic PIC's asking 'why' CCS doesn't allow a combination line AN0 and AN2 to be used for analog. The reason is simply that these PIC's don't work that way, allowing AN0 on it's own to be selected, or AN0+AN1, or AN0+AN1+AN2 etc.. The abilities of the CCS functions are totally determined by the chip's abilities. |
|
|
|