Servicing hardware interrupts in a 40X2

pjrebordao

Senior Member
I didn't find the manual completely clear on the servicing of hardware interrupts (INT0, 1, 2)...

There's some talk about clearing the int flags inside the interrupt routine, but what flags exactly ?
And must the interrupts be enabled again before returning ?
 

geoff07

Senior Member
A Picaxe only uses polled interrupts, i.e. they occur when the firmware goes looking for them. They aren't the same as the interrupts that you might use in a raw PIC programmed in "C".

They can be set to occur on pin conditions, or in the case of X2 parts, flag conditions. If you set an interrupt to occur when a particular condition (pin or flag) exists, then you either have to wait for it to go away or clear it directly, if you don't want to be in a loop of continuous interrupts. Given that the interrupts must be cleared in order to prevent such loops, you then naturally have to re-enable them when exiting the ISR.

First you use hintsetup to make it known what hardware conditions are to set flags
Then use setintflags to cause the picaxe to respond to the conditions

If a flag is set by the hardware and used to trigger an interrupt then you must clear that flag before proceeding (by setting it to zero). Otherwise you just get another interrupt.

It is all in the manual but needs careful reading and maybe some experimenting.
 

Goeytex

Senior Member
The hardware interrupt flags are individual bits of the system "flags" variable. They are:

hint0flag .... interrupt on INT0 hintsetup
hint1flag .... interrupt on INT1 hintsetup
hint2flag .... interrupt on INT2 hintsetup
hintflag ..... interrupt on any pin 0,1,2
compflag ... comparator flag compsetup
hserflag .... hserial background receive
hi2cflag ..... hi2c write
toflag ........timer overflow flag

To clear the flag simply set it to zero. eg .. let hint1flag = 0 or ..... let flags = 0 ( Clear all flag bits in case more than one is set)

So at the end of the interrupt routine you will have two lines, one that clears the flag, and one that re-enables the interrupt. eg

hint1flag = 0
setintflags %00000010,%00000010
return
 

bryanl

Member
Its not quite so simple under the sheets from what I can tell. See my blog entry on this: PICAXE clock exercise: backwards compatibility creates confusion for interrupts and touch.

The interrupt routine you put in your program does not automatically re-enable interrupts like the chip level routine does. That means you need to re-issue a SETINT or SETINTFLAGS command to get PICAXE to start checking between commands for an interrupt condition. It also means that PICAXE likely checks for interrupt conditions even between commands in the interrupt routine.
 

srnet

Senior Member
The interrupt routine you put in your program does not automatically re-enable interrupts like the chip level routine does. That means you need to re-issue a SETINT or SETINTFLAGS command to get PICAXE to start checking between commands for an interrupt condition.
Yep, as described in the manual.

It also means that PICAXE likely checks for interrupt conditions even between commands in the interrupt routine.
Maybe it does, but then it does not act on it.
 

geoff07

Senior Member
I can't see a problem really. It says what it does and does what it says. It is easy to use and works. Don't confuse a Picaxe with a raw PIC. With the latter you can do a lot more but the learning curve is much longer and steeper and your code may be much more complex. If you don't understand some aspect of how a Picaxe works, ask on the forum and, as you have seen, you will get good friendly advice, even in minutes.
 

srnet

Senior Member
Its not quite so simple under the sheets from what I can tell. See my blog entry on this
If you think there is an error or bug or even a clarification required in the way interrupts are dealt with on a PICAXE, why not post them here ?
 

inglewoodpete

Senior Member
Hardware interrupts do work and work well. Have a look here, when in 2011 I introduced myself to them. hippy provided a short piece of working code that clarified how you can make them work. Later, I posted code for a Background IR Receiver for the X2 models.

I made some suggestions at the time for updating the manual. I haven't checked whether the manual was updated.
 
Top