SETINT input,mask (AND condition)
SETINT AND input,mask (AND condition)

Additional options for M2, X1 and X2 parts

SETINT OR input,mask (OR Condition)
SETINT NOT input,mask (NOT the AND Condition)
SETINT input,mask,port
(port selection on X2 only)
SETINT NOT input,mask,port
(port selection on X2 only)

Input - is a variable/constant (0-255) which specifies input condition.

Mask - is variable/constant (0-255) which specifies the mask.

Port - is the X2 port (A,B,C,D).   


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.


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.

08M only inputs 1 to 4 may be used
14M only inputs 0, 1 and 2 may be used
18M only inputs 0, 1, 2 plus 6 and 7 may be used
20M only inputs 1 to 5 may be used
08M2 only inputs C.1 to C.4 may be used
14M2 only inputs C.0, C.1 and C.2 may be used
18M2 only inputs C.0, C.1, C.2 and C.4 to C.7 may be used
20M2 only inputs C.1 to C.5 may be used
20X2 only inputs C.1 to C.5 may be used
28X2 only inputs C.0 to C.7, B.0 to B.7 and A.0 to A.3 may be used
40X2 only inputs C.0 to C.7, B.0 to B.7, A.0 to A.3 and D.0 to D.7 may be used


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 then disabled and does not automatically re-enable. Therefore to immediately re-enable the interrupt (if desired) when the interrupt processing is finished another 'setint' command must be used within the interrupt sub-procedure itself. This interrupt will not be enabled until the interrupt sub-procedure's '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.

5) If the interrupt was not re-enabled within the interrupt sub-procedure; an interrupt can be enabled later with a 'setint' command.

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.

Applies To:
All (except 08, 18, 28)
See Also:
Related Create:

Handle an interrupt

Handle an interrupt whenever the signal on input pin C.7 goes high. Set output B.1 high while the interrupt is being handled

Code Example:
		setint %10000000,%10000000,C	; interrupt when pinC.7 goes high

main:		low B.1				; switch output B.1 off
		pause 2000			; wait 2 seconds
		goto main			; loop back to start

interrupt:	high B.1			; switch output B.1 on
		if pinC.7 = 1 then interrupt	; loop here until the interrupt cleared
		pause 2000 			; wait 2 seconds
		setint %10000000,%10000000,C	; re-activate interrupt
		return				; return from sub
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.