Equivalent function?

Status
Not open for further replies.

Lake9398

New Member
So I recently dove back into programming, and looking at old code I see something that I believe could be simplified:

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.
 
Use more #defines and get the best of both worlds

Code:
#define LEFT_BUMPER !PORTBbits.RB0

then you can say this and get rid of the negated logic too

if(LEFT_BUMPER)
{
  MOTOR=BACKWARD;
  Delay10KTCYx(250);
  MOTOR=BACKWARD_B|FORWARD_A;
  Delay10KTCYx(100);
  MOTOR=FORWARD;
}
With so many #defines the code can bet a bit hard to debug.
For the constants you may want to use actual constants

const unsigned char Backwards = 0bXXXXXXXX;

sort of thing. Last I checked MPLAB's debugger did not understand
#defined values but it will show the real const's

If you want to see the real magic behind the PORTBbits.RB0 checkout the processor header file (ie p18f1320.h) and search or PORTBbits. You will see a union of structs that is rather interesting and useful.
 
Last edited:
There is also a difference in the timing (depending on how the bits on the input side of the port can be set). In the original code, all bits were sampled at the same time (a single read of the port) whereas your alternative version allows for the input bits to alter between successive reads of the port. If one of the "if" statements is true, then the code that is executed contains a couple of calls to "delay" functions: this means that the bits could alter between successive port reads and the actual code paths will vary.

I'm not saying that this is a good or a bad thing - that will depend on what you are really trying to do and when the bits being read by the port can actually change. However it is a difference in the way you program will execute, all else being the same.

Susan
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…