External Interrupt At PORT 1

Photo of author

By Jackson Taylor

Home » Hardware » External Interrupt At PORT 1

As an Amazon Associate we earn from qualifying purchases.

An external interrupt lets a microcontroller react to a change on a pin without constantly checking that pin in a loop. On the MSP430, the pins of Port 1 (P1.0 to P1.7) can each trigger an interrupt when the signal on them rises or falls. This article walks through the steps to set it up on P1.4, explains each register involved, and shows a complete assembly example for an MSP430G2x31 device.

If you are new to the topic, our MSP430 assembly language series covers interrupts and pull-up/pull-down resistors in more detail.

Why use an external interrupt?

The alternative to an interrupt is polling: the program reads the input pin again and again and checks whether something happened. Polling works, but it keeps the CPU busy and can miss short pulses if the loop is doing other work.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

With an interrupt, the hardware sets a flag when the chosen edge appears on the pin. If interrupts are enabled, the CPU stops what it is doing, runs a short routine (the interrupt service routine, or ISR), and then returns to the main program. This is also what makes low-power designs possible, because the CPU can sleep until a button press wakes it.

Registers used for Port 1 interrupts

Port 1 interrupts are controlled by a handful of registers. Each bit in a register matches the pin with the same number, so bit 4 (0x10) controls P1.4.

  • P1DIR sets each pin as input (0) or output (1).
  • P1REN enables the internal resistor on a pin.
  • P1OUT sets the output level, and for an input with the resistor enabled it chooses pull-up (1) or pull-down (0).
  • P1IE is the interrupt enable bit for each pin.
  • P1IES selects the edge: 0 for a low-to-high (rising) edge, 1 for a high-to-low (falling) edge.
  • P1IFG is the interrupt flag. The hardware sets a bit here when the selected edge occurs.
See also
RASPBERRY PI

Basic steps to enable an external interrupt at Port 1

  1. Enable the global interrupt with eint.
  2. Enable the external interrupt for the pin (here P1.4) by setting its bit in P1IE.
  3. Select the interrupt edge in P1IES.
  4. Clear the P1IFG flag before the program starts waiting for interrupts.
  5. Clear the P1IFG flag inside your interrupt service routine.

The order matters a little. Changing the edge select bit can itself set the flag, so it is safest to configure P1IES first, clear P1IFG, and only then turn on global interrupts with eint.

Assembly example for P1.4

The program below configures P1.0 as an output (the pin usually connected to an LED on development boards) and P1.4 as an interrupt input. When the interrupt fires, the ISR drives P1.0 high.

.include "msp430g2x31.inc"
      org 0xf800
      mov.w #(WDTPW|WDTHOLD), &WDTCTL   ; stop the watchdog timer
      mov.w #0x280, SP                  ; set the stack pointer
      mov.b #0x10, &P1REN               ; enable resistor on P1.4
      clr.b &P1OUT                      ; resistor set to pull-down, outputs low
      mov.b #001h, &P1DIR               ; P1.0 as output
      bis.b #010h, &P1IE                ; enable interrupt on P1.4
      bis.b #010h, &P1IES               ; select falling (negative) edge
      bic.b #010h, &P1IFG               ; clear interrupt flag
      eint                              ; enable global interrupts
gg:   jmp gg                            ; wait here for interrupts

P1ISR: mov.b #001h, &P1OUT              ; set P1.0 high
       bic.b #010h, &P1IFG              ; clear the P1.4 interrupt flag
       reti                             ; return from interrupt

       ORG 0FFE4h                       ; Port 1 interrupt vector
       DW  P1ISR
       ORG 0FFFEh                       ; reset vector
       DW  0xf800

How the program works

  • The watchdog timer is stopped first, otherwise it would keep resetting the chip.
  • The stack pointer is set to 0x280, the top of RAM on the G2x31 parts, because an interrupt pushes the return address and status register onto the stack.
  • The loop gg: jmp gg stands in for the main program. The CPU stays here until an interrupt occurs.
  • The word at 0FFE4h is the Port 1 interrupt vector. It holds the address of P1ISR, so the CPU knows which routine to run.
  • The word at 0FFFEh is the reset vector. It points to the start of the program at 0xf800.
See also
AUTOMATIC CHASER WITH SPEED CONTROL

Common mistakes

Forgetting to clear P1IFG in the ISR

The flag is not cleared automatically on the MSP430 Port 1. If you leave it set, the CPU will jump back into the ISR as soon as it returns, and the program appears to hang.

Mismatching the edge and the resistor

In the example, P1OUT bit 4 is 0, so the internal resistor on P1.4 is a pull-down. The pin idles low, and a button wired to the supply voltage makes a rising edge when pressed. A falling edge only appears when the button is released. If the interrupt seems to fire at the wrong moment, check this first.

For a button wired between the pin and ground, use a pull-up instead: set bit 4 in P1OUT, keep the resistor enabled in P1REN, and select the falling edge. The changed lines look like this:

      mov.b #010h, &P1OUT               ; P1.4 pull-up selected
      mov.b #010h, &P1REN               ; enable resistor on P1.4

Overwriting P1OUT in the ISR

The ISR in the example uses mov.b #001h, &P1OUT, which writes all eight bits and clears bit 4. That is harmless with a pull-down, but it would switch off a pull-up. Use bis.b, bic.b or xor.b to change only P1.0, for example xor.b #001h, &P1OUT to toggle an LED on each press.

Ignoring switch bounce

A mechanical button does not make a clean edge. It can bounce and trigger several interrupts for one press. Common fixes are a small capacitor, a short software delay or a timer-based debounce routine.

Assuming each pin has its own vector

All Port 1 pins share one interrupt vector. If you enable more than one pin, the ISR has to read P1IFG to see which one caused the interrupt, then clear only that bit.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

Related interrupt topics

The same idea of enabling, selecting an edge, and clearing a flag appears on other platforms. If you work with other boards, you can compare this with a button interrupt on an Arduino, read about interrupts on Arduino boards in general, or see how interrupts work on ARM.

See also
PIC16F877A

FAQ

What does P1IFG do on the MSP430?

P1IFG holds one interrupt flag per Port 1 pin. The hardware sets the bit when the selected edge occurs on that pin, and your code must clear it so the next interrupt can be detected.

How do I choose between rising and falling edge?

Use the P1IES register. A 0 bit selects a low-to-high transition and a 1 bit selects a high-to-low transition. Pick the edge that matches how your switch or sensor is wired and which event you care about.

Why does my interrupt run only once or never stops running?

The usual cause is the flag. If P1IFG is never cleared, the ISR runs again and again. If global interrupts are not enabled with eint, or the vector at 0FFE4h does not point to your ISR, it will not run at all.

Can I use the same code on other MSP430 devices?

The register names are the same across the MSP430G2xx family, but the memory layout, stack address and vector locations depend on the device. Check the datasheet and the matching include file before reusing the code.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works