I am using C Programming to program a PIC18F252 microcontroller chip that allows a robot to follow a line then change directions in different direction on each end and also senses blockage and goes back. This is my code wich at the moment is giving me problems allowing the robot to change directions in different directions and sense blockage...can anyone see where i am going wrong and help out?????????
#include <p18F252.h>
void move (void);
void turn (void);
int temp;
int flag = 0;
void delay (void)
{
//set the timer 2 to produce certain delay
// by loading T2CON and PR2 registers
if(temp == 0x00)
{
T2CON = 0x49;
PR2 = 0x7C;
}
if (temp == 0x01)
{
T2CON = 0x21;
PR2 = 0x7C;
}
T2CONbits.TMR2ON = 1; // start the timer 2
while (!PIR1bits.TMR2IF); // wait for timer flag
T2CONbits.TMR2ON = 0; // stop the Timer 2
PIR1bits.TMR2IF = 0; // clear timer flag
}
void main (void)
{
//ADCON0bits.CHS2 = 0;
//ADCON0bits.CHS1 = 0;
//ADCON0bilts.CHS0 = 0;
//ADCON0bits.ADON = 1;
ADCON1 = 0x07; // PORTA is digital
TRISB = 0x00; // configure PORTB as output
TRISA = 0xFF; // cofigure PORTA as input
PORTB = 0xFF;
while(1){ // run forever
move ();
turn ();
//ADCON0bits.GO_DONE = 1; //start conversion
//while(ADCON0bits.GO_DONE); //wait for conversion end
// PORTB = ADRESH; //send to DAC
//temp = PORTA;
//temp = temp & 0x01;
// PORTBbits.RB0 = 1;
// delay();
//PORTBbits.RB0 = 0;
//delay();
}
}
void turn (void){
if(flag == 1)
{
PORTBbits.RB0 = 0; // enable LHS
PORTBbits.RB1 = 1; // enable direction LHS
PORTBbits.RB2 = 0; // enable RHS
PORTBbits.RB3 = 0; // disable direction RHS
delay();
}
else if(flag = 0)
{
PORTBbits.RB0 = 0; // enable LHS
PORTBbits.RB1 = 0; // disable direction LHS
PORTBbits.RB2 = 0; // enable RHS
PORTBbits.RB3 = 1; // enable direction RHS
delay();
}
}
void move (void){
if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 1 && PORTAbits.RA2 == 0)
{
PORTBbits.RB0 = 0; // enable LHS
PORTBbits.RB1 = 1; // enable direction LHS
PORTBbits.RB2 = 0; // enable RHS
PORTBbits.RB3 = 1; // enable direction RHS
delay();
}
else if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 0 && PORTAbits.RA2 == 1)
{
PORTBbits.RB0 = 0; // enable LHS
PORTBbits.RB1 = 1; // enable direction LHS
PORTBbits.RB2 = 1; // disable RHS
PORTBbits.RB3 = 0; // disable direction RHS
delay();
}
else if(PORTAbits.RA0 == 1 && PORTAbits.RA1 == 0 && PORTAbits.RA2 == 0)
{
PORTBbits.RB0 = 1; // disable LHS
PORTBbits.RB1 = 0; // disable direction LHS
PORTBbits.RB2 = 0; // enable RHS
PORTBbits.RB3 = 1; // enable direction RHS
delay();
}
else if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 1 && PORTAbits.RA2 == 1)
{
PORTBbits.RB0 = 0; // enable LHS
PORTBbits.RB1 = 1; // enable direction LHS
PORTBbits.RB2 = 1; // enable RHS
PORTBbits.RB3 = 1; // enable direction RHS
delay();
}
else if(PORTAbits.RA0 == 1 && PORTAbits.RA1 == 1 && PORTAbits.RA2 == 0)
{
PORTBbits.RB0 = 1; // enable LHS
PORTBbits.RB1 = 1; // enable direction LHS
PORTBbits.RB2 = 0; // enable RHS
PORTBbits.RB3 = 1; // enable direction RHS
delay();
}
else if(PORTAbits.RA0 == 0 && PORTAbits.RA1 == 0 && PORTAbits.RA2 == 0)
{
turn();
delay();
}
}