Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f877a pwm problem

Status
Not open for further replies.

masab_ahmad

Member level 1
Joined
Jul 20, 2010
Messages
32
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Pakistan
Activity points
1,564
Hi all, i made a code to drive 2 DC motors. the PWM pins are connected to a 1k Resistor, which is connected to the gate of a irf510 mosfet with the motor connectd to it(is this ok?). ive attached the code here, it should vary the PWM with variations in the ADC value stored in the 'temp' variables. But it doesnt seem to vary the PWM, (the adc voltages vary just fine), anything wrong with the code? its in mikroC.



/************
* MAIN LOOP
************/
unsigned int temp0;
unsigned int temp1;

void main()
{
ADCON1 = 0 ; // enables ADC

TRISA = 0xff ; // PORTA as inputs
PORTA = 0 ;

TRISC = 0 ; // PORTC as outputs
PORTC = 0 ;

// init PWM Channel : 20 Khz, 90% duty cycle
PWM1_Init(20000) ;
PWM1_Set_Duty(200) ;
PWM2_Init(20000) ;
PWM2_Set_Duty(200) ;
PWM1_Start(); // enable PWM output
PWM2_Start();

// forever loop
for(;;)
{
while((Adc_Read(0) && Adc_Read(1)) > 1) //pulses detected always
{
temp0 = Adc_Read(0); //reading adc ports for ultrasonic pulses
temp1 = Adc_Read(1);
if(temp0 < 655) //car too far from obstacle
{
PWM1_Set_Duty(200);
}
else if((temp0 > 655) &&(temp0 < 700)) //car far from obstacle
{
PWM1_Set_Duty(150);
}
else if((temp0 < 750) && (temp0 > 700)) //car far from obstacle
{
PWM1_Set_Duty(120);
}
else if((temp0 < 800) && (temp0 > 750)) //car medium from obstacle
{
PWM1_Set_Duty(100);
}
else if((temp0 < 900) && (temp0 > 800)) //car near to obstacle
{
PWM1_Set_Duty(0);
PWM2_Set_Duty(200);
}


if(temp1 < 460) //car too far from obstacle
{
PWM1_Set_Duty(200);
}
else if((temp1 < 500) && (temp1 > 460)) //car too far from obstacle
{
PWM2_Set_Duty(150);
}
else if((temp1 < 550) && (temp1 > 500)) //car far from obstacle
{
PWM2_Set_Duty(120);
}
else if((temp1 < 600) && (temp1 > 550)) //car medium to obstacle
{
PWM2_Set_Duty(100);
}
else if((temp1 < 780) && (temp1 > 600)) //car near to obstacle
{
PWM2_Set_Duty(0);
PWM1_Set_Duty(200);
}
Delay_ms(10); // 1 millisecond delay before next sample
}
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top