Playing with code

Fowkc

Senior Member
I can attempt a concise explanation of interrupts:

As you probably already know, an interrupt looks for a given set of input conditions. Those conditions are set by your INPUT and MASK parameters in:

SETINT input, mask

We'll look at the mask first. It is an 8-bit binary number. Labelling the bits 0 to 7:
<code><pre><font size=2 face='Courier'>
76543210
||||||||---input0
|||||||---input1
||||||---input2
|||||---input3
||||---input4
|||---input5
||---input6
|---input7
</font></pre></code>

The interrupt will only check the inputs corresponding to the bits you have set HIGH in the mask. For example, a mask of %00110100
will only look at inputs 5, 4 and 2. It will not matter what the other pins are doing.<i> Important bit -&gt; The mask says nothing about whether the interrupt is looking for HIGH or LOW inputs, only what pins to look at in the first place </i> .

Once you've set your mask, you need to tell the interrupt what those pins should look like. Keeping the mask of %00110100 (inputs 5, 4 and 2), let's say we want the interrupt to trigger when 5 and 4 are HIGH and 2 is LOW. Our INPUT parameter will then be:

<code><pre><font size=2 face='Courier'>
%00110000
||||||||---input0 (not looked at)
|||||||---input1 (not looked at)
||||||---input2 (must be LOW)
|||||---input3 (not looked at)
||||---input4 (must be HIGH)
|||---input5 (must be HIGH)
||---input6 (not looked at)
|---input7 (not looked at)
</font></pre></code>

<i>Also important --&gt; ALL the input conditions must be satisfied, not just some. In the above example, 5 must be high AND 4 must be high AND two must be low. </i>

To give a full example, let's take your suggestion and have an interrupt that responds to two pins and lights an LED when BOTH pins are HIGH. Then when BOTH pins are low, the LED should turn off.

The input pins will be 5 and 4 again, and let's say the LED is on output 0.

The mask will always be %00110000 to look at inputs 5 and 4.

Let's assume the LED is off, so we want to look for both inputs going HIGH. This is an input of %00110000. So your SETINT is:

SETINT %00110000, %00110000

Now, when this condition is met, the interrupt routine will be run. We need to check whether the inputs are HIGH or LOW so we can turn the LED on or off and reset the SETINT correctly:

<code><pre><font size=2 face='Courier'>
interrupt:
'If inputs are both HIGH, turn on LED
IF pin5 = 1 AND pin4 = 1 THEN TurnLEDOn
'Else, turn LED off...
LOW 0
'Set the next interrupt to look for HIGH pins
SETINT %00110000, %00110000
'Return to the main program
RETURN

TurnLEDOn:
'Turn LED on...
LOW 1
'Set the next interrupt to look for LOW pins
SETINT %00000000, %00110000
'Return to the main program
RETURN
</font></pre></code>

Look carefully at that last SETINT:

SETINT %00000000, %00110000

Our mask is the same (because we're looking at the same pins), but the input is different, because now we want those pins to be LOW before the interrupt is triggered.

I hope that all made some sense... (I hope I haven't made any crashingly stupid mistakes).

Edited by - Fowkc on 16/11/2006 08:48:59
 

TEZARM

Senior Member
Excellent example Fowkc. There has been a few things about interrupts flying around in my head all getting confused., but you have just answered most of my questions. Well Done, good work. Give yourself a pat on the back. Now it's really just a matter of practice. Don't worry Beaniebots, this example of Fowkc's is really all I needed. Thanks to all for your help. I can leave you all alone now so you can help other people with there picaxe problems. I'll be back later on of course but by then I'll be heaps better and helping you guys out with your code problems instead (Ah, no, we don't think so TEZARM) lol. TEZARM OUT.
 

BeanieBots

Moderator
Fowkc, thanks for helping me out of the whole that I kept digging deeper and deeper.
A much better explanation than I could ever have put together.
 
Top