yyyyaser
Newbie level 5
Help , pic16f877a , pwm for a motor
why is this code not working?
when set_point < feed_back , the if statement is executed.
but when set_point > feed_back , the it doesn't.
and it is working on the sw debugger but not on the breadboard.
//---------------------------------------------------------
unsigned int set_point , feed_back;
signed int current_duty;
void InitMain() {
PORTA = 0;
TRISA = 0xff; //PORT A FOR A/D CONVERSION
/*ADCON0 = 0x00000001; //PORT A WORKS AS AN ANALOG INPUT NOT DIGITAL */
PORTE = 0; // set PORTE to 0
TRISE = 0; // designate PORTE pins as output
PWM1_Init(20000); // Initialize PWM1 module at 20KHz
}
void main() {
InitMain();
ADC_Init();
PWM1_Start(); // start PWM1
while (1) {
set_point = ADC_Read(0);
feed_back = ADC_Read(1);
if(set_point<feed_back)
{
PORTE.B1=0;
PORTE.B2=1;
}
else if(set_point>=feed_back)
{
PORTE.B1=1;
PORTE.B2=0;
}
current_duty = set_point - feed_back;
current_duty = current_duty * (255.0/1023.0) ;
current_duty = abs( current_duty );
PWM1_Set_Duty(current_duty); // Set current duty for PWM1
}
}
why is this code not working?
when set_point < feed_back , the if statement is executed.
but when set_point > feed_back , the it doesn't.
and it is working on the sw debugger but not on the breadboard.
//---------------------------------------------------------
unsigned int set_point , feed_back;
signed int current_duty;
void InitMain() {
PORTA = 0;
TRISA = 0xff; //PORT A FOR A/D CONVERSION
/*ADCON0 = 0x00000001; //PORT A WORKS AS AN ANALOG INPUT NOT DIGITAL */
PORTE = 0; // set PORTE to 0
TRISE = 0; // designate PORTE pins as output
PWM1_Init(20000); // Initialize PWM1 module at 20KHz
}
void main() {
InitMain();
ADC_Init();
PWM1_Start(); // start PWM1
while (1) {
set_point = ADC_Read(0);
feed_back = ADC_Read(1);
if(set_point<feed_back)
{
PORTE.B1=0;
PORTE.B2=1;
}
else if(set_point>=feed_back)
{
PORTE.B1=1;
PORTE.B2=0;
}
current_duty = set_point - feed_back;
current_duty = current_duty * (255.0/1023.0) ;
current_duty = abs( current_duty );
PWM1_Set_Duty(current_duty); // Set current duty for PWM1
}
}