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.

Looking for correction of Code for a Robot using PIC16f877a

Status
Not open for further replies.

ather_88

Newbie level 5
Newbie level 5
Joined
May 22, 2014
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
90
While the robot is moving in backward direction it senses any obstacle while moving, I have used a limit switch when it makes the contact after touching any obstacle the robot should stop. Meanwhile if it gets any command to move forward, right or left it moves. I 've tried various codes to get the desired result but unable to achieve the result. The main code is as follows:
Code:
#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();
 }
}

}

But I would like to focus on the reverse case:
Code:
 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))     //if PIND0 or D1 gets zero it jumps into the routine
   {
     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;

I just want someone to highlight how I can achieve my task that it gets scanning the ports.D0 as well as D1 also to check the serial data receive at the same time, if it receives data serially it should that task otherwise as soon as it gets zero at D0 or D1 the robot should stop.

Kindly help me out.
 

Dear ather_88,

Please visit
PHP:
http://www.robotc.net/wiki/Tutorials/Getting_Started/Getting_Started_with_the_VEX_PIC

and
PHP:
http://first.wpi.edu/Images/CMS/First/C_CPP_Programming_Guide.pdf
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top