View previous topic :: View next topic |
Author |
Message |
randy.shaffer
Joined: 21 Mar 2018 Posts: 51
|
Need help with 16F1455 PWM |
Posted: Tue Sep 24, 2019 5:24 pm |
|
|
Trying to get a 20 kHz, 50% duty ratio out of Pin 2 (A5). Nothing so far. Compiler is v5.080.
Code: | #include <C:\Program Files (x86)\PICC\Devices\16F1455.h>
#fuses NOPUT, NOWDT, LVP // LVP needed to use PGD & PDC at RA0 & RA1
#use delay(internal = 16MHz)
#use pwm(PWM2, TIMER = 2, FREQUENCY = 20kHz, PWM_OFF, STREAM = PWM_OUT, output=PIN_A5)
void main()
{
setup_oscillator(OSC_16MHZ); // run @16MHz
PWM_set_duty_percent(PWM_OUT, 500); // set duty ratio
PWM_ON(PWM_OUT);
while(TRUE)
{
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Sep 24, 2019 5:28 pm |
|
|
I don't use that PIC but...
1) have you got it to run a 1Hz LED program ? This would confirm PIC is good, wired correctly, etc.
2) does your programmer actually use 'LVP' ? Most do not, I've never seen one in 2 decades of 'playing with PICs'.
3) does that PIC have programmable pins ? If so they need to be programmed first.... there's a sticky' above... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Wed Sep 25, 2019 12:52 am |
|
|
As Jay says you almost certainly do not need/want LVP. Unless you are
programming the chip by some form of direct link using another processor.
Your comment on this is not true. LVP, is not needed to use PGC & PDC
at RA0 and RA1.....
However the actual problem:
There are four basic types of pin behaviour for peripherals on the PIC.
1) Fixed pins. The peripheral uses just one set of pins and these cannot
change.
2) Fuse selectable pins. The peripheral can be moved to two different sets
of pins based on a fuse setting.
3) APFCON control. The peripheral can be moved to two sets of pins based
on the value in the APFCON register.
4) PPS control. Here the peripheral can be moved to any 'RP' pin.
Now the standard way of working is '1'. Then '2' requires you to set the
fuse yourself, and if you move the peripheral to the 'alternate' pins, you
will probably also need to set TRIS. '4' is controlled by #PIN_SELECT. This
leaves '3'. This is what is involved on this PIC. Here you have to set
APFCON yourself if you want to use the alternate pins. Since this is what
you are doing, you need to have:
Code: |
#include <16F1455.h>
#fuses NOPUT, NOWDT, LVP // LVP needed to use PGD & PDC at RA0 & RA1
#use delay(internal = 16MHz)
#use pwm(PWM2, TIMER = 2, FREQUENCY = 20kHz, PWM_OFF, STREAM = PWM_OUT, output=PIN_A5)
#BIT P2SEL=getenv("BIT:P2SEL") //bit to select alternate pin for PWM2
void main()
{
//setup_oscillator(OSC_16MHZ); // run @16MHz
//Not needed this is already set
PWM_set_duty_percent(PWM_OUT, 500); // set duty ratio
P2SEL=TRUE; //select alternate pin
PWM_ON(PWM_OUT);
while(TRUE)
{
}
}
|
|
|
|
randy.shaffer
Joined: 21 Mar 2018 Posts: 51
|
|
Posted: Wed Sep 25, 2019 8:49 am |
|
|
First, thanks so much for the replies and for the help, the PWM is now working!
Also, I am very grateful for the information about pin selection. I was aware that the pin needed to be re-directed and was trying to use pin_select but was unsuccessful.
The LVP fuse is a puzzle. At first I couldn't program the micro. The datasheet showed that PGD/C are available at pin pairs RC0/1 and RA0/1. I needed the A/D inputs at RC0/1 and so wanted to use RA0/1 for programming. The pin allocation table shows a footnote for RA0/1 that reads "LVP support for PIC18(L)F1XK50 legacy designs". I thought that meant to use the LVP fuse. After I used the LVP fuse, I was able to program the chip.
Should I be using re-directed pin commands for PGD/C as was needed for the PWM output? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Wed Sep 25, 2019 9:56 am |
|
|
What programmer are you using?.
Are you using any form of ICD?.
Using the pins for PWM, does not stop them from being used for
programming. They only switch to being programming pins when the
right signal is generated on the MCLR pin. If you are programming
'in circuit' the loads on the pins need to be kept low though. |
|
|
randy.shaffer
Joined: 21 Mar 2018 Posts: 51
|
|
Posted: Wed Sep 25, 2019 10:03 am |
|
|
For programming, I'm using the ICD-U64 and CCSLOAD.
I'm stuck with A0/A1 for programming at this time (new PCB).
Did using the LVP fuse fix the programming problem? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Wed Sep 25, 2019 10:46 am |
|
|
The A0/A1 pins only support LVP. Hence why the fuse made it work.
However the ICD U64 doesn't support LVP, so what is actually happening
is you are programming the chip using standard (not LVP) programming
on the pins only designed to support LVP. Long term this may cause
issues.
The PGC pin should have a pull down resistor (perhaps 50KR) to
prevent it floating when the programmer is not attached. |
|
|
randy.shaffer
Joined: 21 Mar 2018 Posts: 51
|
|
Posted: Wed Sep 25, 2019 10:53 am |
|
|
OK, I see. Thanks so much for the help. |
|
|
|