View previous topic :: View next topic |
Author |
Message |
BotBoy
Joined: 06 Nov 2009 Posts: 6
|
Read two input pins like a binary |
Posted: Sat Nov 07, 2009 8:30 am |
|
|
Hi all,
I´m trying read two inputs and after it enable one specifical output high. I did a code but i think maybe not can be the better way to do it.
Any suggestions?
Code: | #include <16F628A.h>
//------------------------------------------------------------------------------
#use delay(clock=4000000)
#fuses NOWDT,NOPUT,NOPROTECT,NOBROWNOUT,MCLR,NOLVP,INTRC_IO
short input1, input2;
void main(void){
input1 = input(pin_a0);
input2 = input(pin_a1);
while(1){
//Input Binary 1
if((input1==1) && (input2==0)){
output_high(pin_b5);
continue;
}
//input Binary 2
if((input1==0) && (input2==1)){
output_high(pin_b6);
continue;
}
//input Binary 3
if((input1==1) && (input2==1)){
output_high(pin_b7);
continue;
}
}
} |
Last edited by BotBoy on Sat Nov 07, 2009 9:08 am; edited 1 time in total |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Nov 07, 2009 9:01 am |
|
|
Code: | switch (input_a() & 3)
{
case 1:
output_high(pin_b5);
break;
case 2:
// ...
} |
|
|
|
BotBoy
Joined: 06 Nov 2009 Posts: 6
|
|
Posted: Mon Nov 09, 2009 9:49 am |
|
|
Great!... Worked fine thank you!!!
But why just add 3 to the input_a? What is the reason for it?
If I need look for 4 inputs instead 2...
I tried some tests just adding 2 more inputs in input_a using this approach and failed.
Please...Could you explain? |
|
|
anandpv2009
Joined: 26 Jul 2009 Posts: 31
|
|
Posted: Mon Nov 09, 2009 12:04 pm |
|
|
Here '&' is used as bitwise operator. Which means the above statement will AND the input from PORTA with 3.
AND example:
0 0 0 0 1
1 0 1 0 1
0 0 0 0 1 Result |
|
|
|