Interrupt on a certain inputs condition.
X1 and X2 parts can also alternately interrupt on a certain 'flags' byte condition see setintflags command.
The setint command causes a polled interrupt on a certain input pin condition. This can be a combination of pins on the default input port (portC). X2 parts can also be redirected to look at a different port if required.
The default condition is a logical AND of the selected input pins. On some parts it is also possible to take the NOT of this AND condition. On some parts it is also possible to take a logical OR of the selected input pins.
A polled interrupt is a quicker way of reacting to a particular input combination. It is the only type of interrupt available in the PICAXE system. The inputs port is checked between execution of each command line in the program, between each note of a tune command, and continuously during any pause command. If the particular inputs condition is true, a 'gosub' to the interrupt sub-procedure is executed immediately. When the sub-procedure has been carried out, program execution continues from the main program.
The interrupt inputs condition is any pattern of '0's and '1's on the input port, masked by the byte 'mask'. Therefore any bits masked by a '0' in byte mask will be ignored.
To interrupt on input1 high only: setint %00000010,%00000010
To interrupt on input1 low only: setint %00000000,%00000010
To interrupt on input0 high, input1 high and input 2 low: setint %00000011,%00000111
Only one input pattern is allowed at any time. To disable the interrupt execute a SETINT OFF command. The M2, X1, X2 parts also support the NOT condition, where the interrupt occurs when the pattern is NOT as the port/mask define. They can also use the 'flags' byte (instead of the input port) to generate the interrupt condition.
Restrictions
Due to internal port configuration on some of the chips there is a limitation on which pins can be used. The default input port is portC.
| 14M/14M2 |
only inputs 0,1,2 may be used |
| 20M |
only inputs 1-5 may be used |
| 20M2/20X2 |
only portC may be used, and only C.1 to C.5 on portC |
| 40X2 |
when using portA, only A.0 to A.3 may be used |
Notes
1) Every program which uses the SETINT command must have a corresponding interrupt: sub-procedure (terminated with a return command) at the bottom of the program.
2) When the interrupt occurs, the interrupt is permanently disabled. Therefore to re-enable the interrupt (if desired) a SETINT command must be used within the interrupt: sub-procedure itself. The interrupt will not be enabled until the 'return' command is executed.
3) If the interrupt is re-enabled and the interrupt condition is not cleared within the sub-procedure, a second interrupt may occur immediately upon the return command.
4) After the interrupt code has executed, program execution continues at the next program line in the main program. In the case of the interrupted pause, wait, play or tune command, any remaining time delay is ignored and the program continues with the next program line.
More detailed SETINT explanation
The SETINT must be followed by two numbers - a 'compare with value' (input) and an 'input mask' (mask) in that order. It is normal to display these numbers in binary format, as it makes it more clear which pins are 'active'. In binary format input7 is on the left and input0 is on the right.
The second number, the 'input mask', defines which pins are to be checked to see if an interrupt is to be generated:
| %00000001 |
will check input pin 0 |
| %00000010 |
will check input pin 1 |
| %01000000 |
will check input pin 6 |
| %10000000 |
will check input pin 7 |
These can also be combined to check a number of input pins at the same time:
| %00000011 |
will check input pins 1 and 0 |
| %10000100 |
will check input pins 7 and 2 |
Having decided which pins you want to use for the interrupt, the first number (inputs value) states whether you want the interrupt to occur when those particular inputs are on (1) or off (0).
Once a SETINT is active, the PICAXE monitors the pins you have specified in 'input mask' where a '1' is present, ignoring the other pins.
An input mask of %10000100 will check pins 7 and 2 and create a value of %a0000b00 where bit 'a' will be 1 if pin 7 is high and 0 if low, and bit 'b' will be 1 if pin 2 is high and 0 if low.
The 'compare with value', the first argument of the SETINT command, is what this created value is compared with, and if the two match, then the interrupt will occur, if they don't match then the interrupt won't occur.
If the 'input mask' is %10000100, pins 7 and 2, then the valid 'compare with value' can be one of the following:
| %00000000 |
Pin 7 = 0 and pin 2 = 0 |
| %00000100 |
Pin 7 = 0 and pin 2 = 1 |
| %10000000 |
Pin 7 = 1 and pin 2 = 0 |
| %10000100 |
Pin 7 = 1 and pin 2 = 1 |
So, if you want to generate an interrupt whenever Pin 7 is high and Pin 2 is low, the 'input mask' is %10000100 and the 'compare with value' is %10000000, giving a SETINT command of:
SETINT %10000000,%10000100
The interrupt will then occur when, and only when, pin 7 is high and pin 2 is low. If pin 7 is low or pin 2 is high the interrupt will not happen as two pins are 'looked at' in the mask.