Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM control for robot

Status
Not open for further replies.

daisordan

New Member
Hi, I am using PIC18F4455 chip to control two PWM and let the robot to move around using C Compiler. PWM1 for moving left or right and PWM2 for forward or backward.
If i run my PWM code one by one it is working but now when i put them together the robot will stop after PWM2 changes. PWM1 is working fine
What it is happening now is turn right + move forward, delay 2s, turn left + move forward, delay 2s, turn right but it doesnt go back(PWM2 stop), delay 2s, turn left and PWM2 stop.

Do i got some problems with the PWM2 coding?

Thanks for the helping
Ivan

void main (void){
TRISC=0x00;
OpenTimer2(TIMER_INT_OFF &
T2_PS_1_16 &
T2_POST_1_8);
OpenPWM1(255);
SetOutputPWM1(SINGLE_OUT,PWM_MODE_1);
OpenPWM2(255);

SetDCPWM1(122);
SetDCPWM2(75);
Delay10KTCYx(60);
SetDCPWM1(55);
SetDCPWM2(75);
Delay10KTCYx(60);
SetDCPWM1(122);
SetDCPWM2(98);
Delay10KTCYx(60);
SetDCPWM1(55);
SetDCPWM2(98);
Delay10KTCYx(60);
}
 
Last edited:
Code:
void main (void){ 
TRISC=0x00; 
OpenTimer2(TIMER_INT_OFF & 
T2_PS_1_16 & 
T2_POST_1_8); 
OpenPWM1(255); 
SetOutputPWM1(SINGLE_OUT,PWM_MODE_1); 
OpenPWM2(255); 

SetDCPWM1(122);
SetDCPWM2(75);
Delay10KTCYx(60);
SetDCPWM1(55);
SetDCPWM2(75);
Delay10KTCYx(60);
SetDCPWM1(122);
[B]SetDCPWM2(98);[/B]
Delay10KTCYx(60);
SetDCPWM1(55);
[B]SetDCPWM2(98);[/B]
Delay10KTCYx(60);
} .

You do know that the two lines I bolded are the same, right?
 
Why would you need to write the same duty cycle values two times over?. Well if this is your full code, than its missing the initialization of PWM2 which the 18f4455 doesn't have. You will need to use the ECCP PWM in compatible mode for the other PWM output. They work the same way.
 
Thanks for your reply :)
SetDCPWM1(122); = turning right
SetDCPWM2(98); = backward
SetDCPWM1(55); = turning left
wt i wanna to do is turn the motor moving right backward for 5s, then move to left backward for 5s

Sorry, i dun get "ECCP PWM in compatible mode for the other PWM output" means. Do you mean i should use compatible mode for PWM2? is that a software code or?
 
Hi, have you gone through the table(page 146 datasheet). Since both PWMs use TMR2 for the PWM frequency, both will have the same frequency. Its because CCP1 is not implemented as a separate ECCP mode PWM pin. May be thats the cause of your problem.
 
Last edited:
My motor is working fine when CCP1 (move forward) and CCP2 (turn right/left). They can do it together. After i change it to CCP1(move backward) and CCP2 (turn right/left), the motor will just turn right or left but not backward. And you say it is not implemented as separate ECCP mode so CCP2 cannot goes to backward. So CCP2 got the (forward) signal and now it cannot change it to (backward) because the ECCP mode PWM pin already store the (forward) code in ? Should i change to another pin to fix this problem? I have try to clear the clock before it process the (backward) code. It is still not working

Thanks
 
Have you tried checking the CCP1 output when you want it to go backward? How are you achieving bidirectional motion with only a single ended PWM output?Try swapping the pin functions, use CCP1 for right/left and CCP2 for forward/reverse. I reckon it is a hardware glitch.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top