View previous topic :: View next topic |
Author |
Message |
Ringo42
Joined: 07 May 2004 Posts: 263
|
PWM on the 16f877A |
Posted: Sat Jun 04, 2005 8:04 pm |
|
|
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
_________________ Ringo Davis |
|
|
Ttelmah Guest
|
|
Posted: Sun Jun 05, 2005 5:14 am |
|
|
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
|
|
Posted: Sun Jun 05, 2005 9:25 am |
|
|
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
|
|
Posted: Sun Jun 05, 2005 10:35 am |
|
|
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
|
|
Posted: Sun Jun 05, 2005 1:52 pm |
|
|
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
|
|
Posted: Sun Jun 05, 2005 3:05 pm |
|
|
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
|
|
Posted: Sun Jun 05, 2005 3:06 pm |
|
|
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
|
|
Posted: Sun Jun 05, 2005 3:22 pm |
|
|
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
|
|
Posted: Sun Jun 05, 2005 4:52 pm |
|
|
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
|
|
Posted: Sun Jun 05, 2005 6:12 pm |
|
|
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
|
|
|
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Sun Jun 05, 2005 7:37 pm |
|
|
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 |
|
|
|