What is an Interrupt? This is best explained with a simple analogy.
You are waiting for a friend to come to your house, and you don’t want to keep your friend waiting. What do you do? You can either:
- Continuously go to the door to see if your friend has arrived. or
- Sit down, relax, and wait for the doorbell to ring.
Clearly method 2. is more efficient. In method 2. the doorbell is called an interrupt. Using an interrupt means you don’t spend all your time going back and forth to the door, you can do other stuff while you wait for your friend.
In software, method 1. is called software polling, and method 2. is called interrupt driven.
A mpy program running on a microcontroller can be interrupted. The microcontroller can be busy doing some task, and then an external signal interrupts the micrcontroller program. At which point it will run an Interrupt Function which will do something with the external signal. When the Interrupt Function has finished the microcontroller will continue with what ever it was doing before.
There are 3 different types of mpy interrupt:
- Pin interrupts – a change on an input pin will cause an interrupt
- Timer interrupts – an interrupt is run at a regular time interval
- UART interrupt – an interrupt is run when UART receives
In order to use a mpy intrerrupt, it must be setup and an interrupt function must be defined
- the interrupt setup – this is where we specify which event will cause the interrupt, and which interrupt function is to be run when the pin changes or certain event happens. Setup an interrupt using the following commands:
- interrupt_setup( intpin, type, func ) – specify the interrupt event or pin, and the function which is to be run. Interrupts can be setup when pins change, or after a certain time, or when data has been received on the UART.
- eint( ) – enable the interrupts – this causes the microcontroller to start listening for interrupts.
- the interrupt function – is the function that is run when the interrupt event happens
- def ( ) – define an interrupt functions the same way as regular function. Note that an interrupt function has no parameters, and it does not have a return value.
- interrupt_clear( intpin) – clears the interrupt on the pin so it can be used again.
In the following example we use an interrupt to switch the red LED on and off.
# Interrupt 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 is the on/off state of the LED, it needs to be a global variable to remember the on/off state x = x ^ 1 # toggle the x value using an exclusive-or function out(P1_0,x) # Turn the LED on or off # Main code dirout(P1_0) # set the REDLED pin as an output dirinpu(P1_3) # set the switch as an input with a pullup resisto x = 0 # variable holding the LED state, set it off to start with 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
Looking at this example more closely, lets first start with the main code.
The first two lines define the Launchpad Red LED pin P1_0 to be used as an output, and the Launchpad Switch pin P1_3 to be used as an input with pullup resistor.
Next variable x will be used to store the on/off state of the Red LED. It is set to a 0 so the LED will be off to start.
In the following line interrupt_setup( P1_3, RISE, toggle_led) we specify that when there is a RISE change on the switch pin P1_3 then run the function toggle_led( ). RISE is a change from 0 to 1, we could of use FALL which would be a 1 to 0 change.
Up to this point the interrupt has been setup but nothing will happen. The final command is eint( ), this command enables the interrupts on the microcontroller, this command makes the system live.
Now lets look back at the beginning of the code at the function toggle_led( ), this is the interrupt function, it is defined just like a regular mpy function. This function turns that will turn the LED on pin P1_3 on and off. Interrupt Functions do not have any parameters and they do not return any data (ie there is no return statement).
The first thing we do in the function is use the interrupt_clear( P1_3 ) command. This command clears the interrupt flag on pin P1_3 so that it can be used again. If we didn’t use the interrupt_clear( ) command it would work the first time only. We need to use the interrupt_clear( ) command to clear it so as to tell it to wait for the next interrupt on that pin.
The next command is global x . The variable x is used to to tun on or off the LED. If we want to know the value of the LED in the rest of the program we must use global variable. By using the command global x we are telling the mpy program to remember the value of x from one interrupt to the next. As interrupt functions are unable to return a value, we must make the variable x global so that is can be used in other parts of the mpy program, and so that it can be remembered from one interrupt to the next.
The next line is x = x^1 , this will take the value x and perform an exclusive-or function with it, and save the result back into variable x. So if the value of x starts with 0, after the command x will be set to 1, then the next time it will go set to 0 again. This is a toggle function.
The final line is out(P1_0,x) this is the output to the LED pin.