Welcome!
cool Welcome PIC fans!
(to Pascal language fans too!)
Documents

Close Documentation

Close How to support PMP

Close Manuals

Close Today's favourites

Technical

Close Tips

Search




Downloads
Google

Tips - How to check that only one button is pressed?

The deal:

If several push buttons are connected say, to PORTA, what is the best way to check that there is only one button pressed?

Here is an assembler suggestion (a snippet from Scott Dattalo):

     MOVF  PORTA,W
     ANDLW BUTTON_MASK
     SKPNZ 
     return       ; No buttons are pressed

     MOVWF temp   ; Clear one bit by using x &(x-1)
     ADDLW 0xff   ; W=PORTA-1
     ANDWF temp,W ; W=PORTA&(PORTA-1)
     SKPZ 
     return       ; More than one button is pressed 

     ; temp holds the one and only button press

Great isn't it?


And here it is the Pascal equivalent code, ready for use:

Temp := PORTA and BUTTON_MASK;
if Temp = 0 then exit; // exit if no button
if ((Temp - 1) and Temp) <> 0 then exit; // Exit if more than one at once
// Here Temp has the bit of the pressed button.



Creation date : 2009.03.27 5:43 PM
Last update : 2009.03.27 5:55 PM
Category : Tips
Page read 2223 times


Print the article Print the article


react.gifReactions to this article

Nobody gave a comment yet.
Be the first one to do it!


Connection...
 
Members List Members : 90

members Visits of the day: (1) PPA   

Your Username:

Password:

[ Password lost ? ]


[ Join us ]


  Member online:
  Anonymous online: 8

Total visits Total visits: 137460  

Most ever online
Most ever onlineTotal: 43

The 04/11/2009 @ 08:33


Webmaster - Infos

Ip: 38.107.191.100

Search




Friends News
Support PMP

Sentence to think about :  Slow and methodical testing like this will eventually get you to a point where you will either be able to spot the bug, or go quietly insane. Maybe both.   Persistence of Vision 2.1 software documentation.
^ Top ^