#include<16f877a.h>
#fuses HS,NOWDT,NOLVP
#use delay(clock=20000000)
//#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
#define M1_1 PIN_B0 //left motor 1 Fwd 19
#define M1_2 PIN_B1 //left motor 1 Rev 20
#define M2_1 PIN_B2 //left motor 2 Fwd 21
#define M2_2 PIN_B3 //left motor 2 Rev 22
#define M3_1 PIN_B4 //Right motor 1 Fwd 23
#define M3_2 PIN_B5 //Right motor 1 Rev 24
#define M4_1 PIN_B6 //Right motor 2 Fwd 27
#define M4_2 PIN_B7 //Right motor 2 Rev 28
#define trig PIN_D6
void stop();
void forward();
void reverse();
void right();
void left();
int going_forward;
int going_reverse;
int going_right;
int going_left;
int lmt;
void forward()
{
output_high(M1_1);
output_high(M2_1);
output_high(M3_1);
output_high(M4_1);
going_forward =1;
}
void reverse()
{
output_high(M1_2);
output_high(M2_2);
output_high(M3_2);
output_high(M4_2);
going_reverse=1;
}
void right()
{
output_high(M1_1);
output_high(M2_1);
output_low(M3_1);
output_low(M4_1);
going_right = 1;
}
void left()
{
output_high(M3_1);
output_high(M4_1);
output_low(M1_1);
output_low(M2_1);
going_left=1;
}
void stop()
{
output_low(M1_1);
output_low(M2_1);
output_low(M3_1);
output_low(M4_1);
output_low(M1_2);
output_low(M2_2);
output_low(M3_2);
output_low(M4_2);
going_forward=0;
going_reverse=0;
going_right=0;
going_left=0;
delay_ms(500);
}
void main()
{
char data;
set_tris_b(0x00);
set_tris_d(0x03);
stop();
while(1)
{
data=getc();
start:
switch(data)
{
case 0x57: // for forward W
if(going_reverse==1||going_right==1||going_left==1)
stop();
forward();
break;
case 0x53: // for Reverse S
if(going_forward==1||going_left==1||going_right==1)
stop();
reverse();
if(going_reverse==1)
{
do
{
while(!input(PIN_D0) || !input(PIN_D1))
{
data = getc();
if(data =='W' || data == 'A' || data =='D')
goto start;
else
while(input(PIN_D0) && input(PIN_D1));
stop();
}
}while(input(PIN_D0) && input(PIN_D1));
}
break;
case 0x41: //for Left A
if(going_forward==1||going_reverse==1||going_right==1)
stop();
left();
break;
case 0x44: //FOR RIGHT D
if(going_forward==1||going_left==1||going_reverse==1)
stop();
right();
break;
case 0x58: //for STOP X
stop();
}
}
}