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.

Line Detection Robot using PIC16F877

Status
Not open for further replies.

helmyshahryl

Newbie level 1
Newbie level 1
Joined
Aug 23, 2007
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,374
Hi, I am doing my final year project on Line Detection using PIC16F877 and I am writing my codes in C.
Can anyone check my coding?
Thank You.


//LiDTrans PIC Programming

#include <pic.h>

//Hardware:
//Using PIC16F877
//Clock = 4Mhz


//Configuration Fuses
#if defined(_16F877)
#warning PIC16F877 selected
__CONFIG(0x3FFD); //PIC16F877 Configuration Fuses
// - XT crystal oscillator

#else
#error Unsupported PICmicro MCU selected
#endif


//Global Variables
volatile unsigned TRISPC @ (unsigned) &TRISC; //declaration of TRISC as TRISPC
volatile unsigned PC @ (unsigned) &PORTC; //declaration of PC as PORTC - RF input signal
volatile unsigned TRISLNSR @ (unsigned) &TRISD; //declaration of TRISLNSR as TRISD
volatile unsigned LNSR @ (unsigned) &PORTD; //declaration of LNSR as PORTD - Line Sensor

static bit TRISMRF @ (unsigned) &TRISB*8+0; //TRISB0
static bit TRISMLF @ (unsigned) &TRISB*8+1; //TRISB1
static bit TRISMRFH @ (unsigned) &TRISB*8+2; //TRISB2
static bit TRISMLFH @ (unsigned) &TRISB*8+3; //TRISB3
static bit TRISMRR @ (unsigned) &TRISB*8+4; //TRISB4
static bit TRISMLR @ (unsigned) &TRISB*8+5; //TRISB5
static bit TRISMRX @ (unsigned) &TRISB*8+6; //TRISB6
static bit TRISMLX @ (unsigned) &TRISB*8+7; //TRISB7

static bit MRF @ (unsigned) &PORTB*8+0; //RB0 - Motor Right Forward
static bit MLF @ (unsigned) &PORTB*8+1; //RB1 - Motor Left Forward
static bit MRFH @ (unsigned) &PORTB*8+2; //RB2 - Motor Right Forward High
static bit MLFH @ (unsigned) &PORTB*8+3; //RB3 - Motor Left Forward High
static bit MRR @ (unsigned) &PORTB*8+4; //RB4 - Motor Right Reverse
static bit MLR @ (unsigned) &PORTB*8+5; //RB5 - Motor Left Reverse
static bit MRX @ (unsigned) &PORTB*8+6; //RB6 - Motor Right Stop
static bit MLX @ (unsigned) &PORTB*8+7; //RB7 - Motor Left Stop

static bit TRISJSR @ (unsigned) &TRISA*8+0; //TRISA0
static bit TRISJSL @ (unsigned) &TRISA*8+1; //TRISA1

static bit JSR @ (unsigned) &PORTA*8+0; //RA0 - Junction Sensor Right
static bit JSL @ (unsigned) &PORTA*8+1; //RA1 - Junction Sensor Left

static bit TRISUSW @ (unsigned) &TRISA*8+2; //TRISA2
static bit TRISOBS @ (unsigned) &TRISA*8+3; //TRISA3

static bit USW @ (unsigned) &PORTA*8+2; //RA2 - User Switch @Station
static bit OBS @ (unsigned) &PORTA*8+3; //RA3 - Obstacles Limit Switch


//initializing port function
void InitPort()
{
TRISPC = 0b00000111; //Determines RC0,RC1,RC2 as input
TRISLNSR = 0b00000111; //Determines RD0,RD1,RD2 as input

TRISMRF = 0; //Declare RB0 as output
TRISMLF = 0; //Declare RB1 as output
TRISMRFH = 0; //Declare RB2 as output
TRISMLFH = 0; //Declare RB3 as output
TRISMRR = 0; //Declare RB4 as output
TRISMLR = 0; //Declare RB5 as output
TRISMRX = 0; //Declare RB6 as output
TRISMLX = 0; //Declare RB7 as output

TRISJSR = 1; //Declare RA0 as input
TRISJSL = 1; //Declare RA1 as input

TRISUSW = 1; //Declare RA2 as input
TRISOBS = 1; //Declare RA3 as input

ADCON1 = 0b00001111; //Switch PORTA from analog mode to digital mode

PC = 0b00000000; //initialize PORTC to all 0
LNSR = 0b00000000; //initialize PORTD to all 0

MRF = 0; //initialize RB0=0
MLF = 0; //initialize RB1=0
MRFH = 0; //initialize RB2=0
MLFH = 0; //initialize RB3=0
MRR = 0; //initialize RB4=0
MLR = 0; //initialize RB5=0
MRX = 0; //initialize RB6=0
MLX = 0; //initialize RB7=0

JSR = 0; //initialize RA0=0
JSL = 0; //initialize RA1=0

USW = 0; //initialize RA2=0
OBS = 0; //initialize RA3=0
}

//500 Usec Delay function
void Delay500Us()
{
unsigned char cnt500Us = 165; // Delay Cycle to achieve 500Us delay
while(--cnt500Us != 0) // Delay Timing is approximately 3 usec per loop
continue; // Note this routine is for 4MHz crystal frequency
}

//100 msec Delay function
void Delay100ms()
{
unsigned char cnt100ms = 200; // 200 * 500 Usec = 100 msec

do
{
Delay500Us();
}while(--cnt100ms != 0);
}

//1 sec Delay function
void Delay1s()
{
unsigned char cnt1ms = 10; //10 * 100 msec = 1 sec

do
{
Delay100ms();
}while(--cnt1ms != 0);
}

//wait for user input function
void WaitUserInput()
{
while (PC == 0b00000000) // Loop and wait for LiDTrans received signal from RF transmitter
continue;
}

//wait for user press resume button
void UserSwitch()
{
while (USW == 0)
continue;
}

//Input from Line Detection Sensor
void LineSensor()
{
while (LNSR!=0b00000111)
{
switch(LNSR)
{
case 0b00000010: MRF = 1; MLF = 1; break;
case 0b00000011: MRF = 1; MLFH = 1; break;
case 0b00000001: MRX = 1; MLF = 1; break;
case 0b00000110: MRFH = 1; MLF = 1; break;
case 0b00000100: MRF = 1; MLX = 1; break;
}
}
}

//From Station A to Station B function
void A_to_B()
{
LineSensor();

while (JSR != 1 && JSL != 0) //detects junction sensor (01 = Station A)
LineSensor();

MRF = 1; MLR = 1; //from front, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station A

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch(); //wait for user give command to indicates loading job is done

MRX = 0; MLX = 0;

LineSensor();

MRF = 1; MLR = 1; //from Station A, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 1) //detects junction sensor (10 = Station B)
LineSensor();

MRR = 1; MLF = 1; //from front, turn right

Delay1s();

MRR = 0; MLF = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station B

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch(); //wait for user give command to indicates unloading job is done

MRX = 0; MLX = 0;

LineSensor();

MRF = 1; MLR = 1; //from Station B. turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 0) //detects junction sensor (00 = Base)
LineSensor();

MRF = 1; MLR = 1; //U-turn at Base

Delay1s();
Delay1s();

MRF = 0; MLR = 0;

//back to main()
}

//From Station A to Station C function
void A_to_C()
{
LineSensor();

while (JSR != 1 && JSL != 0) //detects junction sensor (01 = Station A)
LineSensor();

MRF = 1; MLR = 1; //from front, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station A

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans Stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRF = 1; MLR = 1; //from Station A, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 1 && JSL != 1) //detects junction sensor (01 = Station C)
LineSensor();

MRF = 1; MLR = 1; //from front, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station C

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRR = 1; MLF = 1; //from Station C, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 0) //detects junction sensor (00 = Base)
LineSensor();

MRF = 1; MLR = 1; //U-turn at Base

Delay1s();
Delay1s();

MRF = 0; MLR = 0;

//back to main()
}

//From Station B to Station A function
void B_to_A()
{
LineSensor();

while (JSR != 0 && JSL != 1) //detects junction sensor (10 = Station B)
LineSensor();

MRR = 1; MLF = 1; //from front, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station B

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRF = 1; MLR = 1; //from Station B, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 1 && JSL != 0) //detects junction sensor (01 = Station A)
LineSensor();

MRR = 1; MLF = 1; //from back, turn right

Delay1s();

MRR = 0; MLF = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station A

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRR = 1; MLF = 1; //from Station A, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 0) //detects junction sensor (00 = Base)
LineSensor();

MRF = 1; MLR = 1; //U-turn at Base

Delay1s();
Delay1s();

MRF = 0; MLR = 0;

//back to main()
}

//From Station B to Station C function
void B_to_C()
{
LineSensor();

while (JSR != 0 && JSL != 1) //detects junction sensor (10 = Station B)
LineSensor();

MRR = 1; MLF = 1; //from front, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station B

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRR = 1; MLF = 1; //from Station B, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 1 && JSL != 1) //detects junction sensor (11 = Station C)
LineSensor();

MRF = 1; MLR = 1; //from front, turn left

Delay1s();

MRR = 0; MLF = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station C

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRR = 1; MLF = 1; //from Station C, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 0) //detects junction sensor (00 = Base)
LineSensor();

MRF = 1; MLR = 1; //U-turn at Base

Delay1s();
Delay1s();

MRF = 0; MLR = 0;

//back to main()
}

//From Station C to Station A function
void C_to_A()
{
LineSensor();

while (JSR != 1 && JSL != 1) //detects junction sensor (11 = Station C)
LineSensor();

MRF = 1; MLR = 1; //from front, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station C

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRR = 1; MLF = 1; //from Station C, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 1 && JSL != 0) //detects junction sensor (01 = Station A)
LineSensor();

MRR = 1; MLF = 1; //from back, turn right

Delay1s();

MRR = 0; MLF = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn a station A

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRR = 1; MLF = 1; //from Station A, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 0) //detects junction sensor (00 = Base)
LineSensor();

MRF = 1; MLR = 1; //U-turn at Base

Delay1s();
Delay1s();

MRF = 0; MLR = 0;

//back to main()
}

//From Station C to Station B function
void C_to_B()
{
LineSensor();

while (JSR != 1 && JSL != 1) //detects junction sensor (11 = Station C)
LineSensor();

MRF = 1; MLR = 1; //from front, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station C

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRR = 1; MLF = 1; //from Station C, turn right

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 1) //detects junction sensor (10 = Station B)
LineSensor();

MRF = 1; MLR = 1; //from back, turn left

Delay1s();

MRR = 0; MLF = 0;

LineSensor();

MRF = 1; MLR = 1; //U-turn at Station B

Delay1s();
Delay1s();

MRF = 0; MLR = 0;
MRX = 1; MLX = 1; //LiDTrans stop

UserSwitch();

MRX = 0; MLX = 0;

LineSensor();

MRF = 1; MLR = 1; //from Station B, turn left

Delay1s();

MRF = 0; MLR = 0;

LineSensor();

while (JSR != 0 && JSL != 0) //detects junction sensor (00 = Base)
LineSensor();

MRF = 1; MLR = 1; //U-turn at Base

Delay1s();
Delay1s();

MRF = 0; MLR = 0;

//back to main()
}


//main function
void main()
{
while (1 == 1)
{
InitPort(); //call initializing port function
WaitUserInput(); //call waiting for user input function
if (PC == 0b00000001)
A_to_B(); //call function station A to station B
else if (PC == 0b00000010)
A_to_C(); //call function station A to C
else if (PC == 0b00000011)
B_to_A(); //call function station B to A
else if (PC == 0b00000100)
B_to_C(); //call function station B to C
else if (PC == 0b00000101)
C_to_A(); //call function station C to A
else if (PC == 0b00000110)
C_to_B(); //call function station C to B
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top