|
|
View previous topic :: View next topic |
Author |
Message |
CheAhMeD
Joined: 10 Apr 2011 Posts: 4 Location: Tunisie
|
Help with PWM on PIC16F876 or 16F877 |
Posted: Sun Apr 10, 2011 7:59 pm |
|
|
Hello,
I doing a project to control the speed of a powrful DC Motor
I'm using 2 push buttons and PWM from a 16F877 to adjust the speed
I wrot the following program and compile it but I didn't get the PWM out of the CCP1 pin in fact there is no signal even for the LCD
I'm really confused since I'm a beginner
I really need your help
Code: |
#include <16F876.h>
#device adc=8
#use delay(clock=4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, BROWNOUT, LVP, NOCPD, NOWRT, NODEBUG
#include <LCD.C>
#define Bouton1 PIN_C3
#define BOUTON2 PIN_C4
void convert_to_pourcent( long int data, char volts[6]) {
BYTE i;
long int temp,div;
div=1000;
for(i=0;i<=2;i++) {
temp=data/div;
volts[i]=(BYTE)temp+'0';
temp=div*(BYTE)temp;
data=data-temp;
div=div/10;
}
}
void display_data( long int data ) {
char volt_string[6];
convert_to_pourcent( data, volt_string );
printf(lcd_putc,volt_string);
lcd_putc(" % ");
}
void main()
{
long int r;
r=500;
output_c(0xF8);
set_tris_c(0xF8);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_2(T2_DIV_BY_16, 127, 1);
lcd_init();
do{
setup_ccp1(CCP_PWM);
set_pwm1_duty(r);
lcd_putc("\f La vitesse actuelle du moteur est :");
lcd_gotoxy(1,2);
display_data(r);
if (input(bouton1) ==0) { r=r+10;set_pwm1_duty(r);lcd_gotoxy(1,2);display_data(r);delay_ms(50);}
if (input(bouton2) ==0) { r=r-10;set_pwm1_duty(r);lcd_gotoxy(1,2);display_data(r);delay_ms(50);}
if (r> 1000 ) {r=0;}
}
while (true);
}
|
Thank you so much |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 10, 2011 10:10 pm |
|
|
If something doesn't work, make a very simple test program that tests
only one thing. See if it works. For example, here is a program to
test PWM on the CCP1 pin. Test it, exactly as shown, with no changes.
Code: |
#include <16F877.h>
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
//==========================
void main()
{
setup_timer_2(T2_DIV_BY_16, 255, 1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(128); // 50% duty cycle
while(1);
} |
|
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Mon Apr 11, 2011 6:29 am |
|
|
Have you got pullup resistors on your pushbuttons? And why not use Port B, which has internal ones? |
|
|
CheAhMeD
Joined: 10 Apr 2011 Posts: 4 Location: Tunisie
|
|
Posted: Mon Apr 11, 2011 6:32 am |
|
|
PCM programmer wrote: | If something doesn't work, make a very simple test program that tests
only one thing. See if it works. For example, here is a program to
test PWM on the CCP1 pin. Test it, exactly as shown, with no changes.
Code: |
#include <16F877.h>
#fuses XT, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
//==========================
void main()
{
setup_timer_2(T2_DIV_BY_16, 255, 1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(128); // 50% duty cycle
while(1);
} |
|
Hello PCM programmer and thank you for responding
I did exactly what you told me to do and still nothing on the CCP1 (PIN_C2) there is no signal :( |
|
|
CheAhMeD
Joined: 10 Apr 2011 Posts: 4 Location: Tunisie
|
|
Posted: Mon Apr 11, 2011 6:33 am |
|
|
John P wrote: | Have you got pullup resistors on your pushbuttons? And why not use Port B, which has internal ones? |
Yes I did put resistors on both push buttons and the port B is for the LCD |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 11, 2011 11:48 pm |
|
|
Does the PIC do anything ? Have you ever made it blink an LED ?
If you have no PWM signal, it could be because:
1. You have the LVP fuse enabled while not needing it, and pin B3 is
not connected to ground with a pull-down resistor, and is floating.
This causes the PIC to lock-up when pin B3 floats to a high level.
Solution: Change it to NOLVP.
2. The PIC is not running, because the oscillator is not working, or the
PIC is held in reset because the MCLR pin doesn't have a pull-up resistor
on it, or the PIC doesn't have power or ground connected to it.
3. The CCP1 pin may be connected to a very low resistance load
which the output driver on the PIC's CCP1 pin can't handle, and
therefore you see no PWM signal. The pin is loaded down too much.
Solution: Disconnect the load (the motor, or whatever it is). See if
you now get a signal. If so, you need an external driver for the motor. |
|
|
CheAhMeD
Joined: 10 Apr 2011 Posts: 4 Location: Tunisie
|
|
Posted: Tue Apr 12, 2011 9:03 am |
|
|
PCM programmer wrote: | Does the PIC do anything ? Have you ever made it blink an LED ?
If you have no PWM signal, it could be because:
1. You have the LVP fuse enabled while not needing it, and pin B3 is
not connected to ground with a pull-down resistor, and is floating.
This causes the PIC to lock-up when pin B3 floats to a high level.
Solution: Change it to NOLVP.
2. The PIC is not running, because the oscillator is not working, or the
PIC is held in reset because the MCLR pin doesn't have a pull-up resistor
on it, or the PIC doesn't have power or ground connected to it.
3. The CCP1 pin may be connected to a very low resistance load
which the output driver on the PIC's CCP1 pin can't handle, and
therefore you see no PWM signal. The pin is loaded down too much.
Solution: Disconnect the load (the motor, or whatever it is). See if
you now get a signal. If so, you need an external driver for the motor. |
Hello PCM programmer,
I tried the program in ISIS in this circuit
It's a simple schematic but it doesn't work the pin CCP1 (PIN C2) is always blue (LOW 0v)
I'm really confused |
|
|
|
|
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
|