Write code to trigger an interrupt at both a press and a rel
Write code to trigger an interrupt at both a press and a release of push button S2. Turn on and off the red LED to indicate if the button is pressed down or released. Recall that a 0 (or a 1) on the corresponding bit of register P1IES configures an interrupt to react to a rising-edge (or a falling-edge) event.
Put your microcontroller into Low Power Mode 3. Modify the stack so that the green LED is toggled at each release of push button S2 in the main program outside the interrupt service routine.
Solution
#include <msp430x20x2.h>
#define LED0 BIT0
#define LED1 BIT6
#define BUTTON BIT3
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= (LED0 + LED1); // Set P1.0 to output direction
// P1.3 must stay at input
P1OUT &= ~(LED0 + LED1); // set P1.0 to 0 (LED OFF)
P1IE |= BUTTON; // P1.3 interrupt enabled
P1IFG &= ~BUTTON; // P1.3 IFG cleared
__enable_interrupt(); // enable all interrupts
for(;;)
{}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1OUT ^= (LED0 + LED1); // P1.0 = toggle
P1IFG &= ~BUTTON; // P1.3 IFG cleared
P1IES ^= BUTTON; // toggle the interrupt edge,
}
