Code for Robot
Based on the code below, we decided to use 5 IR sensor as input and 2 motor as output. The code for "PWM_test.c" has been shown in previous post. the function of PWM_test.c is to control the speed of the motor in this assignment. PWM_test.c produced rectangle signal to Port 1.0. The code of main body has been shown below.
-------------------------------------------- CODE FOR MAIN BODY --------------------------------------------
#include <reg52.h> /* Use reg52.h for header file */
#include "PWM_test.c" /* Call the PWM_test.c in same folder*/
sbit s1=P2^1; /* Declare sensor 1,2,3,4 & 5 to port 2*/
sbit s2=P2^2;
sbit s3=P2^3;
sbit s4=P2^4;
sbit s5=P2^5;
sbit R_motor1 = P3^3; /* Declare motor driver 1 & 2 for forward and reverse function to port 3*/
sbit F_motor1 = P3^4;
sbit R_motor2 = P3^0;
sbit F_motor2 = P3^1;
void main () /* main body*/
{
P3=0x00; /* set no input for port 3*/
pwm_setup(0); /* set the duty cycle to 100%*/
while(1) /* when system is running*/
{
if((s1==0)&&(s2==1)&&(s3==0)&&(s4==0)&&(s5==0)) /* When sensor 2 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==1)&&(s2==0)&&(s3==0)&&(s4==0)&&(s5==0)) /* When sensor 1 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==0)&&(s2==0)&&(s3==1)&&(s4==0)&&(s5==0)) /* When sensor 3 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move forward*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==1)&&(s5==0)) /* When sensor 4 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==0)&&(s5==1)) /* When sensor 5 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==1)&&(s2==1)&&(s3==1)&&(s4==0)&&(s5==0)) /* When sensor 1, 2, 3 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==1)&&(s2==1)&&(s3==0)&&(s4==0)&&(s5==0)) /* When sensor 1, 2 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 100%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==0)&&(s2==0)&&(s3==1)&&(s4==1)&&(s5==1)) /* When sensor 3, 4, 5 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==1)&&(s5==1)) /* When sensor 4, 5 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 100%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==1)&&(s3==1)&&(s4==1)&&(s5==1)) /* When sensor 2, 3, 4, 5 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==1)&&(s2==1)&&(s3==1)&&(s4==1)&&(s5==0)) /* When sensor 1, 2, 3, 4 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==0)&&(s5==0)) /* When no sensor is not triggered*/
{
pwm_setup(128); /* set the duty cycle to 100%*/
R_motor1 = 0; /* move forward*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==1)&&(s2==1)&&(s3==1)&&(s4==1)&&(s5==1)) /* When all sensor is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else /* If sensor is triggered but there have no instructure above */
{
pwm_setup (128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move forward*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 1;
}
}
}
--------------------------------------------------------------------------------------------------------------------------
-------------------------------------------- CODE FOR MAIN BODY --------------------------------------------
#include <reg52.h> /* Use reg52.h for header file */
#include "PWM_test.c" /* Call the PWM_test.c in same folder*/
sbit s1=P2^1; /* Declare sensor 1,2,3,4 & 5 to port 2*/
sbit s2=P2^2;
sbit s3=P2^3;
sbit s4=P2^4;
sbit s5=P2^5;
sbit R_motor1 = P3^3; /* Declare motor driver 1 & 2 for forward and reverse function to port 3*/
sbit F_motor1 = P3^4;
sbit R_motor2 = P3^0;
sbit F_motor2 = P3^1;
void main () /* main body*/
{
P3=0x00; /* set no input for port 3*/
pwm_setup(0); /* set the duty cycle to 100%*/
while(1) /* when system is running*/
{
if((s1==0)&&(s2==1)&&(s3==0)&&(s4==0)&&(s5==0)) /* When sensor 2 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==1)&&(s2==0)&&(s3==0)&&(s4==0)&&(s5==0)) /* When sensor 1 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==0)&&(s2==0)&&(s3==1)&&(s4==0)&&(s5==0)) /* When sensor 3 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move forward*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==1)&&(s5==0)) /* When sensor 4 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==0)&&(s5==1)) /* When sensor 5 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==1)&&(s2==1)&&(s3==1)&&(s4==0)&&(s5==0)) /* When sensor 1, 2, 3 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==1)&&(s2==1)&&(s3==0)&&(s4==0)&&(s5==0)) /* When sensor 1, 2 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 100%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==0)&&(s2==0)&&(s3==1)&&(s4==1)&&(s5==1)) /* When sensor 3, 4, 5 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==1)&&(s5==1)) /* When sensor 4, 5 is triggered*/
{
pwm_setup(128); /* set the duty cycle to 100%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==0)&&(s2==1)&&(s3==1)&&(s4==1)&&(s5==1)) /* When sensor 2, 3, 4, 5 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the left*/
F_motor1 = 0;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==1)&&(s2==1)&&(s3==1)&&(s4==1)&&(s5==0)) /* When sensor 1, 2, 3, 4 is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else if((s1==0)&&(s2==0)&&(s3==0)&&(s4==0)&&(s5==0)) /* When no sensor is not triggered*/
{
pwm_setup(128); /* set the duty cycle to 100%*/
R_motor1 = 0; /* move forward*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 1;
}
else if((s1==1)&&(s2==1)&&(s3==1)&&(s4==1)&&(s5==1)) /* When all sensor is triggered*/
{
pwm_setup(155); /* set the duty cycle to 40%*/
R_motor1 = 0; /* move to the right*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 0;
}
else /* If sensor is triggered but there have no instructure above */
{
pwm_setup (128); /* set the duty cycle to 50%*/
R_motor1 = 0; /* move forward*/
F_motor1 = 1;
R_motor2 = 0;
F_motor2 = 1;
}
}
}
--------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment