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

Close General documentation

Close How to support PMP

Close Manuals

Close Tips

Other stuff

Close Documentation

Close How to support PMP

Close Off Topic

Close Today's favourites


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):

     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)
     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:

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 : 2013.08.05 11:23 AM
Category : Tips
Page read 20749 times

react.gifReactions to this article

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

 Members List Members : 75

Your Username:


[ Password lost ? ]

[ Join us ]

Member online :  Member online :
Anonymous online :  Anonymous online : 14

Total visits Total visits: 1600252  

Most ever online
Most ever onlineTotal : 170

The 01/01/2021 @ 17:50

Webmaster - Infos



Friends News
Where are you from?

Sentence to think about :  Sometimes I'm quite happy that butter wire cutter was never taken up by computer specialists: mayhap it would no longer be compatible with all butters.  René Cougnenc, June 2, 1995.
^ Top ^