CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC16F887 - Dual PWM out. CCP1 not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
drot_bigear



Joined: 18 Apr 2008
Posts: 3
Location: Denmark

View user's profile Send private message Visit poster's website

PIC16F887 - Dual PWM out. CCP1 not working
PostPosted: Fri Apr 18, 2008 1:27 pm     Reply with quote

Hi all.

I hope someone can help me on this one.

I'm doing a minor project with a PIC16F887 - 40pin DIP
Need 2 PWM out with same frequency but different duty cycles.

I've set both ccp modules up the same way, but I can't measure anything on the CCP1 output, pin17.
CCP2 works flawless.

I've read through most of this forum searching for CCP1 subjects.
Tried some of the codes that were suggested for others, including this code
Code:
int16 pwm_l, pwm_r;

void main()
{
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);

setup_timer_2(T2_DIV_BY_1, 128, 1);

pwm_l = 128;
pwm_r = 128;

set_pwm1_duty(pwm_l);
set_pwm2_duty(pwm_r);

while(1);  // Prevent PIC from going to sleep.
}


It didn't work.

This is how my own code looks at the moment.
Code:

unsigned int Duty = 0;


void Update_PWM()
{
   set_pwm1_duty(Duty);
   set_pwm2_duty(Duty);
   Duty +=4;
   if (Duty > 200) Duty = 0;
}


void main()
{
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_1,200,1);   //8MHz/4/200 = 10kHz

   setup_ccp1(CCP_OFF);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_OFF);
   set_pwm2_duty(0);            //to avoid high start output
   setup_ccp2(CCP_PWM);

   setup_oscillator(OSC_8MHZ);

   do {
      delay_ms(1000);
      Update_PWM();
   } while (TRUE);


surely hope someone can help me.

Best regards

Jacob
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 18, 2008 2:00 pm     Reply with quote

I don't have a 16F887, but I do have 16F886 and it's a very similar PIC
except it's in a 28-pin package. I tested the program shown below
with compiler vs. 4.071 and it worked. I got a PWM signal on pins 12
and 13 (CCP2 and CCP1) with a frequency of 7.75 KHz, and a 25% duty
cycle.

Code:
#include <16F886.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

//===================================
void main()
{
int16 pwm_l, pwm_r;

setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);

setup_timer_2(T2_DIV_BY_1, 128, 1);

pwm_l = 128;
pwm_r = 128;

set_pwm1_duty(pwm_l);
set_pwm2_duty(pwm_r);

while(1);  // Prevent PIC from going to sleep.
}


If it doesn't work for you, then:
1. Check the external circuit on the CCP pin. Is it loading down the
signal too much, so that you can't see the signal ?

2. Post your compiler version.

3. Post a complete test program (but short) as I have shown above,
showing the #fuses and #use delay() statements.
drot_bigear



Joined: 18 Apr 2008
Posts: 3
Location: Denmark

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 18, 2008 5:08 pm     Reply with quote

Compiler version 4.016

I have no external circuit on the CCP1 pin yet.
CCP2 has a 1k res and a LED to gnd.

my fuses from my .h file:
Code:

#include <16F887.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPUT                    //No Power Up Timer
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOCPD                    //No EE protection
#FUSES NOBROWNOUT               //No Reset when brownout detected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOLVP                    //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 18, 2008 5:42 pm     Reply with quote

Quote:
Compiler version 4.016

That's an early version, and the setup_ccpx() routines are buggy in that
version. You can make it work if you substitute the "my_setup_ccpx()"
routines that are shown in the program below. Use them instead of
the normal routines. In these two new routines, I have just copied what
CCS does in vs. 4.071.
Code:
#include <16F886.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)

#byte CCP1CON = 0x17
#byte CCP2CON = 0x1D
#byte PWM1CON = 0x9B

void my_setup_ccp1(int8 value)
{
output_low(PIN_C2);
CCP1CON = value;
PWM1CON = 0;
}

void my_setup_ccp2(int8 value)
{
output_low(PIN_C1);
CCP2CON = value;
}

//===================================
void main()
{
int16 pwm_l, pwm_r;

my_setup_ccp1(CCP_PWM);
my_setup_ccp2(CCP_PWM);

setup_timer_2(T2_DIV_BY_1, 128, 1);

pwm_l = 128;
pwm_r = 128;

set_pwm1_duty(pwm_l);
set_pwm2_duty(pwm_r);

while(1);  // Prevent PIC from going to sleep.
}
drot_bigear



Joined: 18 Apr 2008
Posts: 3
Location: Denmark

View user's profile Send private message Visit poster's website

PostPosted: Sat Apr 19, 2008 4:44 am     Reply with quote

Well that works much better.

Thank you very much for a quick reply.
I'll make sure to save that info for further usage Smile

Best regards

Jacob
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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