View previous topic :: View next topic |
Author |
Message |
qoou
Joined: 28 Sep 2016 Posts: 14
|
PWM on ccp1 - no output [SOLVED] |
Posted: Sun Oct 16, 2016 3:01 pm |
|
|
PIC16F1784
http://ww1.microchip.com/downloads/en/DeviceDoc/40001637C.pdf
Compiler version 5.040
Code: | #include <16F1784.h>
#device ADC=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES PUT //Power Up Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming
#use delay(internal=16000000)
void main()
{
output_low(PIN_B0);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16,255,1); // 976 Hz
set_pwm1_duty(64); // 25% duty cycle
while(TRUE)
{
output_high(PIN_D2); // This is just here to verify
delay_ms(10); // that it is running.
output_low(PIN_D2);
delay_ms(10);
}
} |
Scope on D2 shows a 50 Hz square wave as expected.
Scope on B0 shows nothing, no output.
As far as I can tell, the other pwm functions (#use pwm, pwm_set_duty_percent, pwm_set_frequency) are for the software pwm functions, and are not needed here.
Am I doing something wrong?
Last edited by qoou on Sun Oct 16, 2016 3:56 pm; edited 3 times in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Sun Oct 16, 2016 3:39 pm |
|
|
first line of code HAS to be the #include PICtype header
2nd line would be 'fuses'...
3rd thing is to disable ALL other peripherals that use the pin
4th does that PIC have 'pin select' ( I don't use it....)
5th does it work in a 'toggle' program(verify no solder shorts...)
others may have ideas as well...
Jay |
|
|
qoou
Joined: 28 Sep 2016 Posts: 14
|
|
Posted: Sun Oct 16, 2016 3:54 pm |
|
|
temtronic wrote: | first line of code HAS to be the #include PICtype header
2nd line would be 'fuses'...
3rd thing is to disable ALL other peripherals that use the pin
4th does that PIC have 'pin select' ( I don't use it....)
5th does it work in a 'toggle' program(verify no solder shorts...)
others may have ideas as well...
Jay |
Well that was quick.
I had left off the basic header info, edited it in to avoid confusion.
Yes, this PIC has pin select. Didn't realize that. Just checked C2 and the pwm was there... I'm an idiot.
Thanks...! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 16, 2016 4:03 pm |
|
|
The 16F1784 does not have Pin Select. It has Alternate Pin Functions.
To choose PIN_B0 for PWM in this PIC, you specify it as follows:
Code: |
#include <16F1784.H>
#fuses INTRC_IO, NOWDT
#use delay(internal=16M)
//================================
void main()
{
setup_ccp1(CCP_PWM | CCP1_B0); // Select Pin B0 for PWM
output_low(PIN_B0); // Make Pin B0 be an output
setup_timer_2(T2_DIV_BY_16,255,1); // 976 Hz
set_pwm1_duty(64); // 25% duty cycle
while(TRUE)
{
output_high(PIN_D2); // This is just here to verify
delay_ms(10); // that it is running.
output_low(PIN_D2);
delay_ms(10);
} |
|
|
|
qoou
Joined: 28 Sep 2016 Posts: 14
|
|
Posted: Sun Oct 16, 2016 4:13 pm |
|
|
PCM programmer wrote: | The 16F1784 does not have Pin Select. It has Alternate Pin Functions. |
Thanks. I'm not sure I understand the difference between pin select/alternate pin functions. Is this saying ccp1 can be used in 2 different modes on 2 separate pins at the same time? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 16, 2016 6:15 pm |
|
|
With Alternate Pin Functions you typically get a choice of two pre-defined
pins for a peripheral function. You have to choose one pin or the other.
Example, for CCP1 output, you get to pick Pin B0 or Pin C2.
With Pin Select you have many more options. You have more pins to
choose from, and you can do things like mapping an i/o pin to multiple
peripherals.
Your PIC does not have Pin Select. It only has Alternate Pin Functions. |
|
|
qoou
Joined: 28 Sep 2016 Posts: 14
|
|
Posted: Sun Oct 16, 2016 6:41 pm |
|
|
PCM programmer wrote: | With Alternate Pin Functions you typically get a choice of two pre-defined
pins for a peripheral function. You have to choose one pin or the other.
Example, for CCP1 output, you get to pick Pin B0 or Pin C2.
With Pin Select you have many more options. You have more pins to
choose from, and you can do things like mapping an i/o pin to multiple
peripherals.
Your PIC does not have Pin Select. It only has Alternate Pin Functions. |
Got it, thanks. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19498
|
|
Posted: Mon Oct 17, 2016 12:57 am |
|
|
Just to add one more thing here.
There is a third option on some PIC's. These have alternate functions, changed by fuses, rather than the APFCON register. On these there will be fuses like CCP2B3 to change which pin the output is fed to.
I think they run more or less 'historically', so you first got PIC's with the fuse selectable alternate functions, then Microchip started doing some with pins selectable to two locations in code (single bit selecting from two locations), and then realised just how useful this was, especially on the PIC's, with dozens of peripherals, but often limited numbers of pins, and started offering the RP (relocatable peripheral) ability (here each relocatable peripheral pin is given an 'address' for the pin it connects to). |
|
|
|