View previous topic :: View next topic |
Author |
Message |
doke
Joined: 09 Feb 2009 Posts: 6
|
Problems with two PWMs @ PIC16F877 |
Posted: Mon Feb 09, 2009 9:43 am |
|
|
Hello everybody!
I'm new here and this is my first problem:
I want to generate two PWM signals with my PIC16F877. Therefore I use the following code. To check the PWM signals (CCP1 and CCP2) I have connected them with two LEDs. CCP1 works fine, CCP2 however stays low (LED does not shine).
Is there a problem with my code or maybe with the PIC??
Thanks
doke
Code: |
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
void main()
{
unsigned char dc ;
setup_ccp1(CCP_PWM); // set mode to PWM
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 249, 1); // set prescale, period and postscale
// cycle time = (1/clock)*4*t2div*(period+1)
// period 249 => 250 Hz
for(;;)
{
for(dc = 10 ; dc < 249 ; dc++)
{
set_pwm1_duty(dc); // set the duty cycle (0..period)
set_pwm2_duty(dc);
delay_ms (3);
}
for(dc = 249 ; dc > 10 ; dc--)
{
set_pwm1_duty(dc);
set_pwm2_duty(dc);
delay_ms (3);
}
}
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Mon Feb 09, 2009 10:13 am |
|
|
Start by proving if it is the PIC.
Change your oscillator fuse to XT (HS, turns up the oscillator gain, and is really only meant for crystals > 4MHz), then run something really simple like:
Code: |
#include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
void main() {
while (TRUE) {
output_high(PIN_C1);
delay_us(50);
output_low(PIN_C1);
delay_us(50);
}
}
|
Triple check the actual connection. If the PWM2 pin (C1), doesn't light the LED now, you have proved you have a PIC problem.
Presumably you have got current limiting resistors to the PIC pins?. Otherwise you are overloading them....
Best Wishes |
|
|
doke
Joined: 09 Feb 2009 Posts: 6
|
|
Posted: Tue Feb 10, 2009 11:04 am |
|
|
Thank you for your answer Ttelmah.
It tried the following code and only the LED at the RC2/CCP1 pin was blinking, no signal at RC1/T1OSI/CCP2
Code: | #include <16F877.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
void main() {
while (TRUE) {
output_high(PIN_C1);
output_low(PIN_C2);
delay_ms(50);
output_low(PIN_C1);
output_high(PIN_C2);
delay_ms(50);
}
} |
|
|
|
Ttelmah Guest
|
|
Posted: Tue Feb 10, 2009 11:27 am |
|
|
I'm afraid you have your answer... :(
Try unplugging your PIC, and checking with an ohmeter, the resistance of the RC1 pin to ground. It could be as simple as an almost invisible solder 'whisker' shorting the pin to ground. PICs are at times remarkably rugged, and unless you have been doing things with high voltages etc., round it, it is pretty uncommon to kill a pin.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 10, 2009 1:43 pm |
|
|
What if the LED is installed backwards ? Or the series resistor is too
low or too high in value ? (It should be about 220 to 470 ohms).
Check those things just to be sure. |
|
|
doke
Joined: 09 Feb 2009 Posts: 6
|
|
Posted: Sun Feb 15, 2009 12:18 pm |
|
|
I'm afraid, the PIC is dead. I try to get another one, maybe then it works... |
|
|
|