So I recently dove back into programming, and looking at old code I see something that I believe could be simplified:
Into this, utilizing the PORT register:
Just curious, as reading from a port is far easier than I thought originally.
Code:
LATB=0x07;
TRISB=0x07;
LATC=0x00;
TRISC=0x00;
ADCON1=0x0F;
INTCON2bits.RBPU=0;
while(1)
{
swtch=PORTB&0x07;
leftbumper = (swtch & 0x01); //Port B0
rightbumper = (swtch & 0x02);//Port B1
centerbumper = (swtch & 0x04);//Port B2
MOTOR=FORWARD;
if(!(leftbumper))
{
MOTOR=BACKWARD;
Delay10KTCYx(250);
MOTOR=BACKWARD_B|FORWARD_A;
Delay10KTCYx(100);
MOTOR=FORWARD;
}
else
{
MOTOR=FORWARD;
}
if(!(rightbumper))
{
MOTOR=BACKWARD;
Delay10KTCYx(250);
MOTOR=BACKWARD_A|FORWARD_B;
Delay10KTCYx(100);
MOTOR=FORWARD;
}
else
{
MOTOR=FORWARD;
}
if(!(centerbumper))
{
PERIPH = buzzer;
MOTOR=BACKWARD;
Delay10KTCYx(250);
MOTOR=BACKWARD_A|FORWARD_B;
Delay10KTCYx(200);
MOTOR=FORWARD;
PERIPH = 0x00;
}
else
{
MOTOR=FORWARD;
}
}
}
Into this, utilizing the PORT register:
Code:
LATB=0x07;
TRISB=0x07;
LATC=0x00;
TRISC=0x00;
ADCON1=0x0F;
INTCON2bits.RBPU=0;
while(1)
{
MOTOR=FORWARD;
if(!(PORTBbits.RB0))
{
MOTOR=BACKWARD;
Delay10KTCYx(250);
MOTOR=BACKWARD_B|FORWARD_A;
Delay10KTCYx(100);
MOTOR=FORWARD;
}
else
{
MOTOR=FORWARD;
}
if(!(PORTBbits.RB1))
{
MOTOR=BACKWARD;
Delay10KTCYx(250);
MOTOR=BACKWARD_A|FORWARD_B;
Delay10KTCYx(100);
MOTOR=FORWARD;
}
else
{
MOTOR=FORWARD;
}
if(!(PORTBbits.RB2))
{
PERIPH = buzzer;
MOTOR=BACKWARD;
Delay10KTCYx(250);
MOTOR=BACKWARD_A|FORWARD_B;
Delay10KTCYx(200);
MOTOR=FORWARD;
PERIPH = 0x00;
}
else
{
MOTOR=FORWARD;
}
}
}
Just curious, as reading from a port is far easier than I thought originally.