need simple solution

Status
Not open for further replies.

hemnath

Advanced Member level 3
Joined
Jun 24, 2012
Messages
702
Helped
61
Reputation
120
Reaction score
57
Trophy points
1,308
Location
Chennai
Visit site
Activity points
6,589
I have 4 buttons connected to pic 18F2520. which are configured as inputs. I'm using CCS c compiler

below is the routine i'm using now,

HTML:
if((input(PIN_A1) == 0) && (input(PIN_A2) == 0) && (input(PIN_A3) == 0) && (input(PIN_A4) == 0))  // 0000
{
variable = 0;
}

if((input(PIN_A1) == 0) && (input(PIN_A2) == 0) && (input(PIN_A3) == 0) && (input(PIN_A4) == 1))		// 0001
{
variable = 1;
}

like this  ... goes upto


if((input(PIN_A1) == 1) && (input(PIN_A2) == 1) && (input(PIN_A3) == 1) && (input(PIN_A4) == 1))		// 1111
{
variable = 15;
}

The problem is, i have to type so many lines to check the input status and the program becomes very large. Is there any simple solution to read the input?

Please help. Thanks in advance
 

i've found a better solution,

HTML:
unsigned char variable[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

while(1)
{
a = (input_a() & 0b1111);
value = variable[a];
output_b(value);
}

Looks so simple now

Well any one know any other solution, please post. So that i can learn and it may help me.
 

If the pins are all on the same port (I'm not sure of CCS syntax):

if(port & 0x02) result |= 8;
if(port & 0x04) result |= 4;
if(port & 0x08) result |= 2;
if(port & 0x10) result |= 1;

Note that the result in the original code has it's bits reversed !

If they are in MSB -> LSB order the code is simpler:

result = (port & 0x1E) >> 1;

Brian.
 

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