interrupt_setup( intpin, type, func )

Use: interrupt_setup( intpin, type, func )
Description: Sets up an interrupt on a pin or a circuit
Parameters: intpin Interrupt pin or circuit, this can be a Pinname, or WATCHDOG_TIMER, or UART_RX. If Pinname is used it must be in the form Px_y. Where x is the port number, which can be 1 or 2, and y is the bit number which is between 0 and 7. (e.g. P2_3)
type Defines the type of interrupt,If this Interrupt is on a Pinnamethen type must be:RISE :  interrupt is triggered when intpin signal changes from 0 to 1
FALL:   interrupt is triggered when intpin signal changes from 1 to 0If the interrupt is on WATCHDOG_TIMERthen type specifies the interrupt delay. This must be one of the following values:WDT_MDLY_32         32ms
WDT_MDLY_8           8ms
WDT_MDLY_0_5       0.5ms
WDT_MDLY_0_064  0.064ms
WDT_ADLY_1000     3 sec
WDT_ADLY_250       0.75 sec
WDT_ADLY_16          50ms
WDT_ADLY_1_9        6ms

If the interrupt is UART_RX then the type value must be

0 :    there are no parameter settings for UART_RX

func Name of the function that will be run when the interrupt happens (do not use quotes)
Returns: None
Related: eint(), dint(), interrupt clear(), interrupt_disable()

The interrupt_setup command is used to configure an interrupt.

It sets up an interrupt to occur based on the intpin parameter. This can be a pinname, or a circuit such as the the WATCHDOG_TIMER, or UART_RX.

The second parameter is the type, it is used to further specify the type of the interrupt. eg. for an interrupt on a pinname you can specify that the interrupt should occur when there is a change from 0 to 1 on the pin by using RISE.

The last parameter is func, it is the name of the interrupt function that will be run when the interrupt occurs. The interrupt function is the same as a regular function except it has no parameters and it has no return value. If you want to return a value then you should use a global variable, which can be then be used anywhere else in the program.

Once the interrupt has been configuered it must be enabled using the eint( ) command.

Input Pin Interrupts

If the intpin is a specified as a pinname then this will setup an interrupt to occur when the pin changes. If the type is specified as RISE then the interrupt will happen when the input changes from 0 to 1,  if the type is specified as FALL then the interrupt will happen when the pin changes from 1 to 0.

When the pin changes it will cause an interrupt and the interrupt function will be run.  Inside the interrupt function you should run the interrupt_reset( pinname ) command. This will reset the interrupt flag for that pinname, and make it ready for the next change on that input pinname so it can generate another interrupt.

Example

# The interrupt function flash_led is defined in the same way as any other mpy function
def toggle_led():   
    # First operation is to reset the interrupt flag for P1_3, this allows new interrupts to occur 
    interrupt_clear(P1_3)
    global x
    x = ~x       # x is the on/off value for the Led , toggle its value  
    out(P1_0,x)   # Turn the LED on or off

# Main code
x = 0             # variable holding the LED state, set it off to start with
dirout(P1_0)      # set the REDLED pin as an output
dirinpu(P1_3)     # set the switch as an input with a pullup resistor
interrupt_setup( P1_3, RISE, toggle_led)    # setup a Rising pin interupt on P1_3, which will run the flash_led function 
eint()            # Enable the global interrupt switch. No interrupts will happen until this has been set

Watchdog Timer Interrupts

There are several timer circuits in the MSP430 microcontroller. The Watchdog circuit uses a timer that is running continuously, it is normally used to check that your program doesn’t get stuck. However for many programs the Watchdog circuit is not required, and the timer used in the Watchdog circuit can be used as a general purpose timer for other uses.

The watchdog timer circuit can be used as an interrupt. If you setup an interrupt using the WATCHDOG_TIMER then this will cause the interrupt function to be run at a regular interval. The timer interval is specified by the type parameter. There are a number of preset intervals that a defined (see the command description above for the full list of type values), the intervals are from 0.064ms upto 3 seconds (note the interval times listed are for the default 1MHz MCLK setting)

The interrupt_clear( ) command is not needed for a WATCHDOG_TIMER interrupt.

Example

def toggle_led():   
    global x      # x is defined at the toplevel, but to change it within the function it needs to be made a global variable
    x = x ^ 1     # x is the on/off value for the Led , toggle its value using an exclusive-or function
    out(P1_0,x)   # Turn the LED on or off

# Main code
x = 0             # variable holding the LED state, set it off to start with
dirout(P1_0)      # set the REDLED pin as an output
interrupt_setup( WATCHDOG_TIMER, WDT_ADLY_250, toggle_led)  # setup an interval timer interrupt which will run toggle_led every 750ms
eint()           # no need for enable Global Interrupt Enable when using the WATCHDOG_TIMER.

UART Interrupts

If interrupt_setup( ) uses UART_RX then an interrupt is run everytime  a character is received on the MSP430 UARTUART is short for Universal Asynchronous Reciever Transmitter.   It is a circuit for receiving and sending serial data. It can be used to communicate with a computer  to a bluetooth module. So with a UART_RX interrupt we can run an interrupt function everytime new data is received from another device.

Note only the larger MSP430G2553 chip has a UART.

The UART receive data on the MSP430 is always connected to pin P1_1.  Therefore UART_RX interrupts only work when data is connected to P1_1.  There is no need to setup P1_1 as an input as this is done by the interrupt_setup( ) command.

The received data is written into register UCA0RXBUF

The UART_RX is set to receive data at 9600 baud, 8 bits, 1 stop bit. (note this is for the default 1MHz MCLK setting)

The type parameter is currently ignored.

Example

def read_bt():
    global c_hex
    c_hex = UCA0RXBUF

def print_bt_command():
    global count
    if count % 5 == 0:
        lcd_print( c_hex )    
    count += 1

count = 0    
c_hex = 0
DATA = P1_0
CLOCK = P1_6
lcd2w_enable( DATA, CLOCK )
lcd_print( 'program started')
interrupt_setup( UART_RX, 0, read_bt) 
interrupt_setup( WATCHDOG_TIMER, WDT_MDLY_32, print_bt_command ) 
eint()

This example sets up two interrupts, and uses a 2 wire LCD interface to write out the value of the data from the UART

The first interrupt is on UART_RX, when a character is received on pin P1_1 from another device (in this case a bluetooth module) it will run the interrupt function read_bt( ) which will copy the register value UCA0RXBUF into global variable c_hex.

The second interrupt is an interval timer WATCHDOG_TIMER  with an interval delay of WDT_MDLY_32  which is 32ms. Every 32ms the interrupt function print_bt_command( ) is run. However inside the print_bt_command( )  function the global variable count is incremented each time the interrupt is run. When count modulo 10 is equal to 0 the  value of c_hex is printed onto the LCD display. ie. every tenth time the interval timer interrupt runs (or 320ms) it will print the current value of UART receive register onto the LCD display.

Obviously to run this example you will need the 2 wire LCD display and a connected bluetooth module.