Hello guys!
I would like to seek your expertise with regards to ADC and PWM control of a dc motor. I am using a pic16f877 and mikroC as my compiler. The code compiles successfully but does not work on my circuit. Hope you guys could give me a hand. Here are the following parameters:
PIC16F877 -runs at 4Mhz
RA3(AN3) -pot command
RC2 - Motor Forwards High
RC1 - Motor Reverse PWM (on Low)
RE2 - Motor Reverse High
RE1 - Motor Forwads PWM (on Low)
Attached is my code and the schematic.. Thank you.. Godbless.. ^^
I would like to seek your expertise with regards to ADC and PWM control of a dc motor. I am using a pic16f877 and mikroC as my compiler. The code compiles successfully but does not work on my circuit. Hope you guys could give me a hand. Here are the following parameters:
PIC16F877 -runs at 4Mhz
RA3(AN3) -pot command
RC2 - Motor Forwards High
RC1 - Motor Reverse PWM (on Low)
RE2 - Motor Reverse High
RE1 - Motor Forwads PWM (on Low)
Attached is my code and the schematic.. Thank you.. Godbless.. ^^
Rich (BB code):
unsigned short current_duty, old_duty, current_duty1, old_duty1;
int Dlay;
char ADCValue;
main()
{
PORTC=0;
PORTE=0;
PORTA= 255;
TRISA= 255;
PWM1_Init(4000);
PWM2_Init(4000);
current_duty = 16;
current_duty1 = 16;
PWM1_Start();
PWM2_Start();
PWM1_Set_Duty(current_duty);
PWM2_Set_Duty(current_duty1);
ADCON0=0b01011001;
TMR2=0;
PR2=64;
T2CON=0b00000100;
CCPR1L=0;
while(1 == 1)
{
delay_ms(100);
ADCON0.B2 = 1;
while (ADCON0.B2);
ADCValue=ADRESH;
if (ADCValue > 0x80)
{
CCPR1L = (ADCValue - 80) >> 1;
CCP1CON = 0b00101110;
current_duty++;
PWM1_Set_Duty(current_duty);
current_duty1--;
PWM2_Set_Duty(current_duty1);
TRISC = 0b00000110;
TRISE = 0b00000110;
}
else
{
CCPR1L = (ADCValue ^ 0x7F) >> 1;
CCP1CON = 0b00111110;
current_duty++;
PWM1_Set_Duty(current_duty);
current_duty1--;
PWM2_Set_Duty(current_duty1);
TRISC = 0b00000110;
TRISE = 0b00000110;
}
}
}
Attachments
-
58.5 KB Views: 67