![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
darkrush
Joined: 29 Sep 2011 Posts: 18 Location: Colombia
|
PIC16F1827 Two PWM problem |
Posted: Thu Sep 29, 2011 10:21 pm |
|
|
Hi everybody, this is my first time posting in this forum, hope you can help me and also I would to help as much as I can.
I've been dealing with problem over a month and can't find a solution.
I'm using a PIC16F1827 with internal oscillator at 8MHz and CCS 4.120
I need to generate two PWM signals (same frequency) in RB3 (CCP1) and RA7 (CCP2) pins, but I only get a PWM signal in RB3 not even in RB6 that is the alternate pin for CCP2 I don't know what is wrong, here is my code:
Code: |
#include <16F1827.h>
#device adc = 10
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal Oscillator
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES PUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES MCLR //Master Clear pin enabled
#use delay(clock=8000000)
#use standard_io(A)
#use standard_io(B)
#byte OSCCON = 0x3A
void main()
{
long duty_PWM_1, duty_PWM_2;
setup_timer_2(T2_DIV_BY_16, 124, 1); //8MHz - 1kHz
setup_ccp1 (CCP_PWM); //PWM CPP module
setup_ccp2 (CCP_PWM);
duty_PWM_1 = 250; // 0.0005seg/(16*(1/8000000))=250, Duty 50%.
duty_PWM_2 = 250;
set_pwm1_duty(duty_PWM_1); //50%
set_pwm2_duty(duty_PWM_2); //50%
while(1){}
}
|
When I simulate it, RA7 is always low.
I don't know if it has something to do with the pin mapping (that seems not to work)
Thanks in advance
Xavier |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19367
|
|
Posted: Fri Sep 30, 2011 3:08 am |
|
|
Not the routing.
If you are using a debugger, look at the AFPCON settings. These default to '0', giving the standard routing. Problem seems to be that because the compiler does not 'know' that CCP2, is on pin B6, it doesn't automatically set the TRIS on this bit to make it an output. If you again use the debugger and look at TRISB, you will see that B6 is set as an input with your code.
So, just add:
Code: |
setup_adc_ports(NO_ANALOGS);
set_tris_b(0xB7);
|
and you will merrily find B6 being modulated. :-)
The compiler setup gets confused because of the number of possible places the CCP can go to (with the routing, and the ECCP options....).
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
darkrush
Joined: 29 Sep 2011 Posts: 18 Location: Colombia
|
PWM on RA7 |
Posted: Fri Sep 30, 2011 10:53 am |
|
|
Hi Ttelmah, thanks a lot, because of CCS I have become lazy setting up the TRIS registers :P. It now works on RB6.
I was going to write that I needed it on RA7 but I figured it out how to.
For anyone interest, it seems that after
Code: |
setup_ccp1 (CCP_PWM)
|
APFCON goes to 0, so you need to configure it after setting up the PWM.
I added this:
Code: |
#byte OSCCON = getenv("SFR:OSCCON")
#byte APFCON0 = getenv("SFR:APFCON0")
|
And with this in the main program:
Code: |
OSCCON = 0x3A;
setup_timer_2(T2_DIV_BY_16, 124, 1); //8MHz - 1kHz
set_tris_b(0xB7);
set_tris_a(0xFE);
setup_ccp1 (CCP_PWM); //PWM CPP module
setup_ccp2 (CCP_PWM);
APFCON0 = 0x08;
|
Set the TRIS register to your needs.
Thanks a lot for the help and I hope this is useful for someone else.
Xavier |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|