Jump to content
43oh

reading two push buttons simultaneously with interrupts


Recommended Posts

Hi,

I am trying to write a code on the launchpad msp430g2553, which would simulate the output of several simple logic gates. 

I have connected 2 push buttons as inputs to pins P1.3 and P1.7 and 4 LEDs as outputs to P1.0, P1.4, P1.5, and P1.6.

2 of the LEDs are representing the "truth" and "false" of the truth table and the other 2 are just to indicate that a push button has been pressed (1 for each). 

At first I did it with polling with 6 diferrent logic gates in switch cases and it is working just fine. 

logicPolling.c

 

But now I am trying to do it with interrupts and I get problems while pressing both buttons at the same time (simulating 2 inputs of

logicTableInterrupts.c

Link to post
Share on other sites

As a general rule, you do not want to use interrupts to monitor a physical switch.  The mechanical contacts bounce creating a chain of interrupts that can be very difficult to handle.  If you are going to use interrupts, you must disable them for a certain period of time (until the contacts have stabilized) in the ISR for the first hit.

Link to post
Share on other sites

In reality, you never start to press both switches at the exact same moment. So you should not read the PxIFG, but the PxIN during your interrupt. You'll still need to clear flags as required, but you need to check for the pin levels, not the signal edges, since that is also the behaviour of your logic gate.

Link to post
Share on other sites

If I were trying to simulate synchronous logic... I would simulate synchronous logic.

  • Implement a timer interrupt that occurs every 50-100ms or so. Experiment, and find a good value that is compatible with the rate at which you push the buttons. This becomes your clock period, effectively.
  • Sample the pin values of the pins each odd clock.
  • Latch these values on each even clock.
Link to post
Share on other sites

Thanks for the comments. 

I got it working with reading the pins state (PxIN) withing the ISR instead of reading the interrupt flags (PxIFG), as roadrunner84 suggested. 

in this manner: 

#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{

switch (random){

case 1:   // logic gate "AND" 
if((P1IN & 0x88) == 0x88){ 
      P1OUT |= 0x10; // set "TRUE" LED at P1.4 ON
      P1OUT &= ~0x20; // 
set "FALSE" LED at P1.5 OFF
            }

else{

      P1OUT |= 0x20; // set "FALSE" LED at P1.5 ON

      P1OUT &= ~0x10; // set "TRUE" LED at P1.4 OFF

             }

break;

case 2:  //logic gate "OR"

(...)

 

next stage will be to Implement a timer interrupts!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...