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.

help coding of PIC16F877A

Status
Not open for further replies.

sfchew7

Junior Member level 2
Joined
Sep 29, 2011
Messages
20
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,423
CAN ANYONE teach ME HOW TO SIMPLIFY THIS CODE? it is very messy. How if I have 100 parking lots.
I have no idea at all.
1 = available
0 = unavailable
I know how to declare the array
but I dont know how to insert it.
int8 x;
int8 y;
int8 plav[4];


Code:
		 if((input(pin_a0)==0) && (input(pin_a1)==1) && (input(pin_a2)==0)) 
		{
				
				lcd_putc("\f\nPL available:2/3");
				
			
					}	
			
				else if((input(pin_a0)==0) && (input(pin_a1)==1) && (input(pin_a2)==1)) 
		{
				
				lcd_putc("\f\nPL available:1/3");
				
			
					}	
			
				else if((input(pin_a0)==1) && (input(pin_a1)==0) && (input(pin_a2)==0)) 
		{
				
				lcd_putc("\f\nPL available:2/3");
				
			
					}	


				else if((input(pin_a0)==1) && (input(pin_a1)==0) && (input(pin_a2)==1)) 
		{
			
				lcd_putc("\f\nPL available:1/3");
				
			
					}	


				else if((input(pin_a0)==1) && (input(pin_a1)==1) && (input(pin_a2)==0)) 
		{
				
				lcd_putc("\f\nPL available:1/3");
				
			
					}

thanks
 

If you have 100 inputs and the input is either 1 or 0, you can adding all. Then you will know how many 1's..
 

If "pin_a0", "pin_a1" and "pin a2" are bits 0, 1 and 2 of a port, treat them as a 3-bit banary value, lets call it "pins_a",

if((pins_a > 0) && (pins_a < 3) lcd_putc("\f\nPL available:1/3");
else lcd_putc("\f\nPL available:2/3");

Does the same as the code you show but I'm not sure it's what you really want.

Brian.
 
Code:
a0	a1	a2	Output
------------------------
0	0	0	?
0	0	1	?
0	1	0	2/3
0	1	1	1/3
1	0	0	2/3
1	0	1	1/3
1	1	0	1/3
1	1	1	?
I think this is what your code is trying to do.

One way to implement this cleanly would be to use a switch/case statement. Take a0, a1, a2 and combine them into one number. I think your naming convention is backwards of normal (a0 is typically the lowest precision bit, thus representing the 1's position... a1 would represent 2', a2 would represent 4's, etc), so this will look a little different than I've typically seen it.

number = 4*a0 + 2*a1 + a0. This will convert your three bits into a number ranging from 0 to 7 (2^3 = 8 binary combinations). Then use code like the following (syntax specific to your device... look in the compiler's help pages for examples).

Code:
my_number = 4*a0 + 2*a1 + a0;  //converts three bits to a single unsigned integer value from 0-7

switch(my_number)
{
   case 2, 4:  lcd_print(".....2/3");
   case 3, 5, 6:  lcd_print(".....1/3");
   default;  //default handles all other cases left undefined (e.g. 0, 1 and 7)
}
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top