| View previous topic :: View next topic |
| Author |
Message |
JerryR
Joined: 07 Feb 2008 Posts: 184
|
| Use of pwm_set_duty_percent |
Posted: Tue Nov 11, 2025 12:52 pm |
|
|
Using a PIC18F86J60 target with CCS PCWH v5.121. I cant get this PWM method to work. Output always 0. Any help is appreciated
| Code: |
#include <18F86J60.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#use delay(crystal=25000000)
#use PWM(FREQUENCY=31.25kHz, OUTPUT=PIN_C2, stream = PWM1)
#use PWM(FREQUENCY=31.25kHz, OUTPUT=PIN_C1, stream = PWM2)
void main()
{
pwm_set_duty_percent(PWM1, 500);
pwm_on(PWM1);
pwm_set_duty_percent(PWM2, 500);
pwm_on(PWM2);
while(1)
{
}
}
|
|
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 351
|
|
Posted: Tue Nov 11, 2025 2:18 pm |
|
|
| Add some code in your while(1) loop to toggle another pin manually at 1Hz to make sure your processor is running. |
|
 |
JerryR
Joined: 07 Feb 2008 Posts: 184
|
|
Posted: Tue Nov 11, 2025 3:54 pm |
|
|
Processor is running fine. Thanks. C1 and C2 toggle.
Thanks |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19987
|
|
Posted: Tue Nov 11, 2025 11:01 pm |
|
|
These are PPS outputs.
Now how good the 'smart' functions are at using PPS, varies with compiler
versions. I would be explicit, manually setup the PPS pins for the PWM
modules you want to use, and then use the module name, and not the
pin numbers in the #USE PWM statements.
This is more reliable even on the latest compilers, but on yours I'd say
you have to do this. |
|
 |
JerryR
Joined: 07 Feb 2008 Posts: 184
|
|
Posted: Wed Nov 12, 2025 9:08 am |
|
|
Ttelmah- Thanks as usual for your kind consideration. I might consider another method to generate reliable PWM from this processor.
I'll keep the group informed |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19987
|
|
Posted: Thu Nov 13, 2025 3:37 am |
|
|
Try one simple thing.
Specify the PWM module to use for each.
You are not specifying a module to use. The compiler therefore will by
default use a software PWM. This has very limited abilities compared to
the hardware. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19987
|
|
Posted: Mon Nov 17, 2025 3:20 am |
|
|
First thing, before we look deeper. Have you checked the CPU is actually
running?. You say the outputs are '0'. Start by adding a simple 'flash a pin'
code and make sure the CPU is running and at the right speed. 25MHz, is
the maximum for a crystal on these chips, and it would not take much
for this to not actually be working right. Let's rule it out.
Now, your compiler does correctly generate the setups for he ECCP
on the port pins as specified, and sets up the TRIS correctly.
By default the PWM's are 'on', so your should not need the lines for this.
However again take a step back, and set the duty up in the #use pwm lines
and see if this has any effect (REM out the set_duty_percent lines). If
it then works, you have a pointer to what is wrong. It should be defaulting
to running at 50%.
A walk through the assembler generated, looks as if it should work.
What package is the actual chip?. This affects some features of the ECCP's
so worth knowing. |
|
 |
|