title: How can I use an interrupt service routine to detect button events on this MSP430 code?
body: <p>below is a code for the MSP430G2 micro-controller. I am having trouble adding to this assignment so far to include using the Interrupt Service Routine to my code. This code is a two bit counter which counts up using the S2 button on the launchpad and then counts down using an externally attached switch connected to pins 1.7 and 2.4, which can be seen in the code. If anyone can help explain how to add the ISR to this code, I would highly appreciate the help!</p>
  
  <pre><code>#include &lt;msp430.h&gt;
  
  #define LEDS (BIT6 + BIT0)
  
  // Global variables to hold current state and # of pushes
  char pushes;
  
  
  // Initialization function
  void init(void)
  {
      WDTCTL = WDTPW + WDTHOLD;                         // Stop watchdog timer
      P1DIR |= LEDS;                                   // Set P1.0, P1.6 to output direction
      P1DIR &amp;= ~ BIT3;                                // Turn P1.3 into input
      P1REN |= BIT3;                                 // Enable internal resistor
      P1OUT |= BIT3;                                // Enable as pullup resistor
      P1DIR |= BIT7;                               // Set BIT 7 to output
      P1OUT |= BIT7;                              // Set BIT 7 as VCC
  
  
  
      P2DIR &amp;= ~ (BIT4);                       // Makes this an input
      P2REN |= (BIT4);                        // Turns on internal resistor
      P2OUT &amp;= ~(BIT4);                      // Make internal resistor pulldown
  
      pushes = 0;                          // Initialize global variable
      P1OUT &amp;= ~(LEDS);                   // Clear output LEDs
  }
  
  // Read input function
  char readInput(void)
  {
      char local = 0;
  
      if (!(BIT3 &amp; P1IN))                        // Check for button push
      {
          local = 1;
          _delay_cycles(10000);                 // Wait for bouncing to end
      }
      if ((P2IN &amp; BIT4))                       // Check for button push
      {
          local = 2;
          _delay_cycles(10000);              // Wait for bouncing to end
      }
  
      return local;
  }
  
  // Count up function
  void countUp(void)
  {
      pushes += 1;                          // increment pushes variable
      if (pushes &amp; BIT0){
      P1OUT |= BIT6;
      }
      else{
          P1OUT &amp;= ~BIT6;
          }
      if (pushes &amp; BIT1){
          P1OUT |= BIT0;
          }
      else{
          P1OUT &amp;= ~BIT0;
      }
  
  }
  
  // Count down function
  void countDown(void)
  {
      pushes -= 1;                          // decrement pushes variable
      if (pushes &amp; BIT0){
          P1OUT |= BIT6;
          }
          else{
              P1OUT &amp;= ~BIT6;
              }
          if (pushes &amp; BIT1){
              P1OUT |= BIT0;
              }
          else{
              P1OUT &amp;= ~BIT0;
          }
  }
  
  
  // Main function
  int main(void)
  {
    char enable = 0;                        // Holds status of the S2 button
    char previousState = 0;                 // Holds previous state of S2 button
    init();                                 // One time initialization function
  
    while (1)
    {
  
        enable = readInput();               // Check the buttons
  
        if(enable &amp;&amp; !previousState){
                                            // If button is pressed, allow counter to increment/decrement once
           if(readInput()==1){
               countUp();
  
         }
  
           if(readInput()==2){
               countDown();
  
               }
          }
        previousState = enable;
  
  
    }
  }
  </code></pre>