shaharl2 0 Posted October 17, 2013 Share Posted October 17, 2013 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 Quote Link to post Share on other sites
rockets4kids 204 Posted October 17, 2013 Share Posted October 17, 2013 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. Quote Link to post Share on other sites
roadrunner84 466 Posted October 17, 2013 Share Posted October 17, 2013 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. shaharl2 1 Quote Link to post Share on other sites
jpnorair 340 Posted October 17, 2013 Share Posted October 17, 2013 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. shaharl2 1 Quote Link to post Share on other sites
shaharl2 0 Posted October 18, 2013 Author Share Posted October 18, 2013 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! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.