Вы находитесь на странице: 1из 4

/*

This program is written of technido`s URDP board to make it behave as an Obstacl


e avoider
Connections are as follows:
Left Motor positive terminal @ Port1.0;
Left Motor negative terminal @ Port1.1;
Right Motor positive terminal @ Port1^2;
Right Motor negative terminal @ Port1^3;
Right Sensor @ Port2.0
front Sensor @ Port2.1
Left Sensor @ Port2.2
*/
#include<reg52.h>
#include<stdio.h>
//LCD
sfr LCDdata=0x80; // LCD DATA lines
sbit LCDrs=P2^4; // LCD register Select Pin
sbit LCDrw=P2^5;
sbit LCDen=P2^6; // LCD Enable pin
//motor Connections
sbit L_motor_pos=P1^0;
sbit L_motor_neg=P1^1;
sbit R_motor_pos=P1^2;
sbit R_motor_neg=P1^3;
//Sensor Connections
sbit sen_right=P2^1;
sbit sen_front=P2^2;
sbit sen_left=P2^0;
//Delay Function, Produces 1 mS of delay with 11.0592 Crystal,AT89C51 Controller
And uvision ver1 complier
void MSdelay(unsigned int rtime)
{
unsigned int r,s;
for(r=0;r<rtime;r++)
for(s=0;s<1275;s++);
}
// Sets LCD in command mode and passes commands
void lcdcmd (unsigned char DATA)
{
LCDrs=0;
LCDrw=0;
LCDen=1;
LCDdata=DATA;
LCDrs=0;
LCDen=0;
}
//Delay Function for LCD
void delay (unsigned int a )
{
unsigned int k; unsigned char l;
for (k=0;k<a;k++)
{
for (l=0;l<10;l++)
{
}
}
}
// Initialization of LCD
void initialize (void)
{
lcdcmd (0x30);
delay(30);
lcdcmd (0x38);
delay(30);
lcdcmd (0x0c);
delay(30);
lcdcmd (0x01);
delay(30);
lcdcmd (0x06);
delay(30);
}
// Sets LCD in command data and passes data
void lcddat (unsigned int DATA)
{
LCDrs=1;
LCDrw=0;
LCDen=1;
LCDdata=DATA;
LCDrs=1;
LCDen=0;
}

// Easy function to print data and strings to LCD


void display_lcd (unsigned char location, unsigned char *d)
{
lcdcmd(0x80 | location);
delay(1);
while(*d)
{
lcddat(*d++);
}
}
void forward(unsigned int time) // Makes machine move forward for specified inte
rval of time
{
display_lcd(0x00,"Forward ");
display_lcd(0x40," ");
L_motor_pos=0;
L_motor_neg=1;
R_motor_pos=1;
R_motor_neg=0;
MSdelay(time);
}
void backward(unsigned int time) // Makes machine move Backward for specified in
terval of time
{
display_lcd(0x00,"Backward ");
display_lcd(0x40," ");
L_motor_pos=1;
L_motor_neg=0;
R_motor_pos=0;
R_motor_neg=1;
MSdelay(time);
}
void right(unsigned int time) // Makes machine move right for specified interval
of time
{
display_lcd(0x00,"Right ");
display_lcd(0x40," ");
L_motor_pos=0;
L_motor_neg=1;
R_motor_pos=0;
R_motor_neg=1;
MSdelay(time);
}
void left(unsigned int time) // Makes machine move left for specified interval
of time
{
display_lcd(0x00,"Left ");
display_lcd(0x40," ");
L_motor_pos=1;
L_motor_neg=0;
R_motor_pos=1;
R_motor_neg=0;
MSdelay(time);
}
void main()
{
int flag=0,flag2=0;
P2=0xFF;// Makes P2 as input port
initialize();
display_lcd(0x00," Wall Follower ");
display_lcd(0x40," Waiting... ");
MSdelay(100);
while(1)
{
forward(0);
if(sen_front==0 && sen_left==1 && sen_right== 1 )
{
right(68);
}
if(sen_left==1 && sen_front==1 && sen_right== 1 )
{
left(0);
}
if (sen_left==0 && sen_front==1 && sen_right== 1 )
{
right(0);
}

}
}

Вам также может понравиться