Interrupts are signals from a hardware part that can pause the current code to execute a separate piece of code. An example: An interrupt from an IO ports can be used to turn a LED on. Instead of checking if the IO port is high the interrupt executes the code and the user doesn't have to worry about checking the IO port in the main code.
Example code and libraries can be downloaded from our repository.
To begin it is needed to include the libraries. This is done by these commands:
The libraries for the microcontroller itself and the variables have to be included to:
3. Interrupt on a GPIO port
The Simplecortex can interrupt on every IO pin from port 0.* and port 2.* There are no interrupts on IO port 1.*, 3.* and 4.* To setup and interrupt the command GPIOSetInterrupt(Portnum, Bitposi, Dir); is used. Portnum and Bitposi are for the IO port used. Dir is the direction of the interrupt. An interrupt can be rising edge or falling edge. In rising edge there will be an interrupt if an IO port goes from 0 to 1. In falling edge an interrupt will happen if the IO port goes from 1 to 0. It is recommended to use a internal pull-up/pull-down resistor. How to do this is explained in the GPIO tutorial (APN0001)
A correct instruction how to setup an rising edge interrupt on IO port 0.26 is:
GPIOSetInterrupt(0, 26, 1); or
GPIOSetInterrupt(0, 26, RISING);
GPIOSetInterrupt(0, 26, 0); or
GPIOSetInterrupt(0, 26, FALLING);
Dir 0 is for a falling edge and 1 is for a rising edge. It’s also possible to type the words Rising or Falling.
To handle the interrupts the following code has to be placed under the main code:
void EINT3_IRQHandler (void)
Every time an GPIO interrupt happens, the code in the EINT3_IRQHandler() will be executed. The command GPIOClearInterrupt(); is needed, it will let the interrupt handler know that the interrupts are processed. If the interrupt is not cleared with the command GPIOClearInterrupt(), the code EINT3_IRQHandler will be in a loop forever.
If GPIO interrupts on multiple pins are used, the code in EINT3_IRQHandler will be executed for every interrupt. To check which pin generated an interrupt, the command GPIOCheckInterrupts(Portnum, pinNum, intDir); is used. The command returns a 32 bit variable. If the return value is 1, then an interrupt occurred on the GPIO pin.
There are two ways to check if an interrupt occured on a specific pin. This example uses pin [0,11].
uint32_t checkvalue = GPIOCheckInterrupts(0, 11, RISING);
if (checkvalue == 1)
//Code to execute if interrupt happened}
if ( GPIOCheckInterrupts(0, 11, RISING))
//Code to execute if interrupt happened
An example is available in the repository that checks for an interrupt on port 0,26 and 0,25 and switches an LED on and off accordingly.
4. Interrupt on a timer
The Simplecortex has 4 timers. They can be used for simple delays with the delayms command but interrupting on these timers can be done to. When using interrupts the timer will count to a certain value. when it reaches this value it will interrupt, clear the counter and count again. If the program is busy with a delay it can't do anything else. when using interrupts the program can continue to work.
To begin, a timer has to be initialized with the command TimerInit(Timer_num, TimerInterval); Timer_num is the number of the timer used. TimerInterval is the number the timer counts to. To calculate how long it takes before an interrupts happens the following formula can be used: Delay in seconds = TimerInterval / clock speed. For a delay of 0.5 seconds a value of 60.000.000 is used for TimerInterval. This because the clock speed of the Simplecortex is 120Mhz, or 120.000.000hz.
To initialize timer 1 to interrupt every 0.25 seconds the correct command is:
To use interrupts the times has to be enabled with the enable_timer(Timer_num); command. For timer 1:
If the timer is not enabled it can only be used for the delayms command that is explained in the GPIO tutorial (APN0001). When a timer is used for interrupts it cannot be used for the delayms command.
To use the interrupt add the following code UNDER the main code:
void TIMER1_IRQHandler (void)
This is for timer 1, change the number when using another timer. The TIMER1_interrupt(); command is needed to handle the interrupt. If the command is not used the timer will stop with interrupting! Add any code under the TIMER1_interrupt(); command and it will be executed at every interrupt.
An example is included that sets op all timers and makes the 4 LED's on the Simplecortex blink in a nice pattern.