# How to check 4 inputs and set flag in Microcontroller

I have a PIC MCU and have 4 input pins. I need to check the status of inputs and set the value of flag. Like if input 1 is active, then value of flag should be 1, if input 1 and input 2 are activated then flag is 2. FOr input 1,2,3 flag=3. For this kind of logic, I though of using multiple if else condition:

if(input1==high)
{
flag = 1;
if(input2==high)
{
flag=2;
if(input3==high)
{
flag=3;
if(input4==high)
{
flag=4;
}
}
}
}


Is there any way I can skip this if else and can use some other c logic. Please help.

• It depends, can you read these 4 inputs at the same time (I mean one mcu operation)? Do they belong to the same port? May 17 '16 at 7:39
• Yes they belong to same port and can be read at same time. These input can occur at the same time. Like 4inputs can be high at same time May 17 '16 at 7:48
• Why do you want to skip the if logic part and use something else???? May 17 '16 at 8:18
• Because I thought there might a better option than using if May 17 '16 at 8:25
• Your question is unclear to me. Are you wanting the value of Flag to represent the NUMBER of bits that are high? In other words, what is the value of Flag when only input 3 is high?\ May 17 '16 at 13:59

I would consider using a pre-initialised table, like this:

static uint8 table[] = { 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4 };

flag = table[port_val];


I'm more of an AVR hacker myself, and don't know what the PIC equivalent would be of using PROGMEM to put the table in ROM.

And for 8 bits instead of 4:

uint8 port_val = Read_Port() & 0xff;
flag = table[port_val & 0xf];
if (flag == 4) flag += table[port_val >> 4];


I am assuming that these 4 inputs are lower 4 bits of same port - Pin 0 to 3. You can change values if pins are different.

I would go with something like

port_val = Read_Port() & 0x0F;
switch(port_val)
{
case 0x01: // Only input 1 is set
flag = 1;
break;

case 0x03: // input 1 and 2 set
flag = 2;
break;

case 0x07: // input 1,2 and 3 are set
flag = 3;
break;

case 0x0F: //All 4 inputs are set
flag = 4;
break;
default:
//Optional
flag = 0;
break;
}

• With input 5, the original program has input1 = 1 and input2 = 0 (and input3 = 1), so the result is flag = 1. Swanand's program gives flag = 0 in that case. I'm not 100% sure this matters in the OP's situation, but the two programs do behave differently. May 17 '16 at 16:10
flag =0 ;
for(i=0;i<4;i++)
{
if(port_val & (0x01<<i))
{
flag++;
}
else
{
break;
}
}


If you really want to do it as a loop, try this:

uint8 port_val = Read_Port() & 0xf;
flag = 0;
while (port_val & 1) {
flag++; port_val = port_val >> 1;
}


But I would use the lookup table myself: it's faster and very probably smaller too.

• I would vote against my own answer if I could. Sadly, SE doesn't seem to appreciate the irony. May 17 '16 at 14:29

You can do this with one concise line of C code as follows:

flag=x&1?x&2?x&4?x&8?4:3:2:1:0;   // set flag appropriately