Interrupts are external events that stop a program's normal flow (interrupting the program) and immediately execute some other subroutine, called an interrupt service routine (ISR).
The On…GoSub statement can be used to tell a program to listen for an interrupt and execute an ISR. When the interrupt occurs, the main program stops execution and jumps to the ISR designated by the ON…GOSUB statement. Once the ISR is finished, the Return statement is used to return execution back to the main program.
External key input can occur at any time and data can be received via RS- 232 at any time. Since the main program cannot wait forever to receive these inputs, we need interrupts. If a key is pressed or serial data is received while the main program is running, an interrupt occurs and the main program jumps to the appropriate ISR.
While an ISR is running, subsequent interrupts of the same type are ignored. For example, if an RS-232 receive interrupt occurs during execution of previous RS-232 receive ISR, the new RS-232 receive interrupt will be ignored.
On the other hand, if a timer interrupt occurs (See the On Timer statement) during execution of an RS-232 receive ISR, it will immediately interrupt the RS-232 receive ISR, execute the timer's ISR, and then return to the RS-232 receive ISR.
|On Timer||Initiates an interrupt on a periodic basis|
|On Int||Initiates an interrupt when an external input is received.|
|On Recv||Initiates an interrupt when data is received via RS-232|
|On LadderInt||Initiates an interrupt when Ladder Logic requests an interrupt|
|On Pad||Initiates an interrupt when Pad receives data|
The Cubloc and Cutouch have a Real-Time Operating System (RTOS) which controls interrupt events. This is slightly different from the microcontroller’s hardware interrupts.
1. When interrupt A occurs, while interrupt A's ISR is executing, another interrupt A cannot occur. But interrupt B can occur. Here A and B are different types of interrupts. (e.g. On Timer and On Recv)
2. When interrupt B occurs while executing interrupt A's ISR, interrupt B's ISR will be executed immediately and execution will return to interrupt A's ISR to finish.
3. At the end of an ISR, be sure to include a Return statement. Otherwise, the program may malfunction.
4. If no interrupt is required for your program, you can increase the execution speed of the Cubloc by setting turning off all interrupts using the Set OnGlobal Off. By default, Set OnGlobal is set to On.
5. In the case of On Recv, data received during an On Recv ISR will simply be stored in the serial port's receive buffer. Therefore the data will not be lost. After the current On Recv ISR is finished, if there’s new data in the receive buffer, another On Recv interrupt will be immediately generated. The BClr statement can be used if you do not want to process another On Recv interrupt.
6. If you declare an interrupt more than once, only the last one declared will be in effect.
Please pay attention when creating the interrupt service routine (ISR). It must require less time to execute than the interval itself. If interval is set to 10ms, the ISR must execute within 10 ms. Otherwise, collisions can occur.
It is quite common for a microcontroller to make use of interrupts, but with the Cubloc, interrupts should be used sparingly.
The Cubloc does not handle hardware interrupts directly. When an interrupt occurs, the handler jumps to the BASIC interrupter. Because the BASIC interpreter is slower than directly processed machine code, extra latency can be added to the interrupt service routine and prevent the main routine from operating smoothly.
Therefore, we recommend using interrupts sparingly with the Cubloc. If interrupts are used, keep the interrupt service routine very light and fast. For example, writing to a serial port or using the Delay command is not recommended.
If you need a device that can handle interrupts with speed and ease, please consider the Moacon. The Moacon is an ARM based modular industrial controller from Comfile Technology designed for high speed, interrupt intensive applications.