CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Read two input pins like a binary

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BotBoy



Joined: 06 Nov 2009
Posts: 6

View user's profile Send private message

Read two input pins like a binary
PostPosted: Sat Nov 07, 2009 8:30 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Nov 07, 2009 9:01 am     Reply with quote

Code:
switch (input_a() & 3)
{
  case 1:
    output_high(pin_b5);
    break;
  case 2:
  // ...
}
BotBoy



Joined: 06 Nov 2009
Posts: 6

View user's profile Send private message

PostPosted: Mon Nov 09, 2009 9:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Nov 09, 2009 12:04 pm     Reply with quote

Quote:
(input_a() & 3)


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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group