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 CCS Technical Support

PWM on the 16f877A

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



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PWM on the 16f877A
PostPosted: Sat Jun 04, 2005 8:04 pm     Reply with quote

I'm trying to use the pwm on both cp1 and ccp2 at teh same time but only one seems to be working. I stripped my code down to the bare essentials but still only one pin pwm's, the other stays low. Does anyone see anything wrong here, or should I supect a bad pic?

#include "16f877A.h"
#fuses hs,wdt,noprotect,put,lvp
#device *=16
#use delay(clock=20000000, RESTART_WDT)
signed int32 //long
PWM_L,PWM_R,PWM_MAX;

void main()
{
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,128,16);// 18.6 khz 0-512 int every 8ms //
PWM_MAX=512;

PWM_L=128;
PWM_R=128;
set_pwm1_duty (PWM_L);//0-512
set_pwm2_duty (PWM_R);//0-512

}//end main

Thanks
Ringo
Question
_________________
Ringo Davis
Ttelmah
Guest







PostPosted: Sun Jun 05, 2005 5:14 am     Reply with quote

Though it should not cause the problem, change the variables for the pwm, to unsigned int16. You do not want a sign bit being involved, and there is no point in handling a 32bit variable for values that cannot get this large.
Now on the main problem, I'd try configuring timer1 with T1_DISABLED (or internal), since the CCP2 pin is multiplexed with the timer1 input bit, and this might be causing problems.

Best Wishes
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jun 05, 2005 9:25 am     Reply with quote

CCP1 is the one that works, CCP2 does not, so it sounds like T1 could be a problem but I tried your suggestions and still the same thing. The new code is below. any other thoughts?
Thanks
Ringo
#include "16f877A.h"
#fuses hs,wdt,noprotect,put,lvp
#device *=16
#use delay(clock=20000000, RESTART_WDT)
unsigned int16 PWM_L,PWM_R;

void main()
{
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_2(T2_DIV_BY_1,128,16);// 18.6 khz 0-512 int every 8ms //
setup_timer_1(T1_disabled);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
PWM_L=128;
PWM_R=128;
set_pwm1_duty (PWM_L);//0-512
set_pwm2_duty (PWM_R);//0-512

}//end main
_________________
Ringo Davis
Ttelmah
Guest







PostPosted: Sun Jun 05, 2005 10:35 am     Reply with quote

What compiler version?.
At this point the next step involves compiling the code, and seeing what is actually being put into the registers.

Best Wishes
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Jun 05, 2005 1:52 pm     Reply with quote

You probably shouldn't be using LVP in the fuses, instead use NOLVP. This is a common mistake that causes many people grief. However, this is probably not the problem with the pwm but you should change it anyway.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jun 05, 2005 3:05 pm     Reply with quote

I don't remeber the version but I bought it about 2 weeks ago. I have LVP in the fuses because that is how I program the chip. The chip is soldered down and I program it In-Circuit. I have the exact same code running on an 18f452 and it works fine. I'm going to try another chip at this point, I'm thinking it me be a bad pn or something.
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 05, 2005 3:06 pm     Reply with quote

I took your program, cleaned up everything that was wrong with it,
got rid of everything that wasn't necessary for testing pwm, and
now it works. On pins 16 and 17 of my 40-pin DIP 16F877A,
I get a 38.7 KHz signals, with a 25% duty cycle. I'm running the
PIC at +5v.

Code:
#include <16f877A.h>
#fuses HS, NOWDT, NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock=20000000)

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.
}
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jun 05, 2005 3:22 pm     Reply with quote

Thanks, Now I know it's a bad pin. I wrote a little program to just toggle all the pins and PIN_C1 never moves. Very stange.
Thanks for all the replies.
Ringo
_________________
Ringo Davis
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Sun Jun 05, 2005 4:52 pm     Reply with quote

Ringo42 wrote:
I don't remeber the version but I bought it about 2 weeks ago. I have LVP in the fuses because that is how I program the chip. The chip is soldered down and I program it In-Circuit. I have the exact same code running on an 18f452 and it works fine. I'm going to try another chip at this point, I'm thinking it me be a bad pn or something.
Ringo


In-circuit doesn't mean LVP. Most in-circuit programmers still apply Vpp to program the chips. This includes the ICD's. What did you do with the LVP pin? Either RB3 or RB5 depending on whether we are talking about the 16F877 or 18F452.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jun 05, 2005 6:12 pm     Reply with quote

I pull the LVP pin down with a 10K resistor. I program the chip using a simple programmer that connects to the printer port. The programmer is powered by teh board being programmed so I only need 5V, not the 12 or 15 that a typical programmer uses. It costs me 1 I/O, but it is worth it because of the simpleness of teh programmer.
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jun 05, 2005 6:47 pm     Reply with quote

So you're probably using the TLVP or something similar to it.
http://www.finitesite.com/d3jsys/
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jun 05, 2005 7:37 pm     Reply with quote

Same sort of thing. Mine uses a 74ls14 and 5 or 6 transistors. It works with several different software programs. I found the schematic online a couple years ago, but don't remeber where. Works great. If anyone needs a schematic let me know and I'll email it.
Ringo
_________________
Ringo Davis
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