View previous topic :: View next topic |
Author |
Message |
starfire151
Joined: 01 Apr 2007 Posts: 203
|
Anyone have PWM working with PIC16LF15325? |
Posted: Mon Apr 07, 2025 6:40 pm |
|
|
Has anyone gotten the PWM to work with the PIC16LF15325?
If so, would you post a very simple code example of the setup? Any frequency, duty cycle, or output pin.
I have tried all traditional methods of getting this to work and they all fail with an output of 0.
Thanks. |
|
 |
randy.shaffer
Joined: 21 Mar 2018 Posts: 63
|
|
Posted: Thu Apr 17, 2025 2:24 pm |
|
|
I don't have that chip to try this code on, but this is the approach that I would take:
Code: |
#include <16LF15325.h>
#fuses NOWDT
#use delay(internal = 32MHZ)
#use PWM(FREQUENCY=31.25kHz, OUTPUT=PIN_A5, stream = PWM_OUT)
void main()
{
set_tris_a(0b00001011);
pwm_set_duty_percent(PWM_OUT, 500);
pwm_on(PWM_OUT);
while(TRUE)
{
}
} |
|
|
 |
starfire151
Joined: 01 Apr 2007 Posts: 203
|
|
Posted: Thu Apr 17, 2025 2:57 pm |
|
|
THANK YOU!
That worked!
Apparently my original code specified the output pin through a pin select. This did not work. When I specify the output pin through the #use PWM() function, as your code did, it goes to the right pin and works.
...and I didn't even need to specify setup_timer_2().
Interesting...
Thanks, again! |
|
 |
randy.shaffer
Joined: 21 Mar 2018 Posts: 63
|
|
Posted: Thu Apr 17, 2025 3:02 pm |
|
|
I'm glad to hear that it worked! The timer can be specified in the #use pwm statement, but timer 2 is the default. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19779
|
|
Posted: Fri Apr 18, 2025 12:14 am |
|
|
It is almost certainly using CCP1, not PWM3.
The #use pwm function, seems to have some major problems with handling
ECCP or PWM peripherals, rather than the standard CCP. It works on some
chips, but has problems on others. On the ones with problems, you have
to go 'manual', and use the setup functions, rather than the #use.
I must admit I'd always use the manual setups, not #use. I want to know
from the data sheet what the limitations are on resolution and speed, and
choose the clock rates and setups to optimise my final result. Now (conversely), I'd use #USE RS232 all the time. Where I don't like the
#USE functions, is situations like this where the chip has two distinct
peripherals that can both do the job. I want to know which one is being
used, and the #USE PWM, does not allow this to be properly controlled.
It also has limitations when dealing with different modes for the more
sophisticated peripheral. For example, gating. These extra features require
the setup functions to be used, and the exact behaviour of 'USE is often
poorly documented, so better to actually know what you are doing, than
rely on shortcuts like this. |
|
 |
|