picaxe Interrupts and multi processing/parallel processing

kando

Senior Member
Quick question I hope someone can answer fairly quickly.

If I have

start0
code....
start1
code...

In Picaxe multi processing I believe, a line from start0 gets processed then a line from start1 gets processed etc.
If in start0 an interrupt goes off how does it affect start1s code?
 
Last edited:

AllyCat

Senior Member
Hi,

Interesting Question ! Personally, I never use multiple tasks (prefer to keep control myself of the "flow" of a program) and was going to suggest that mixing tasks and interrupts is an "advanced" application which the manual says is not recommended.

However, this thread indicates that it is possible and the following quote from page 64 of Manual One may give your answer:

"There is one interrupt (setint command) which will interrupt all current tasks."

Not absolutely explicit, but I presume that means that the interrupt continues to be processed (in preference to any tasks) until its Return.

Cheers, Alan.
 

kando

Senior Member
Thanks for the information. I had not seen the flow chart bit. So I copied the basic and produced this.
It would appear that you chaps are right. Once into an interrupt it stops all other tasks. You can't interrupt and interrupt!
Try the program in the simulator.

Code:
#picaxe 08m2
;  reminder of     08m2    pinouts
;                        +V |¯¯¯|0V
;             serial in->c.5|¯¯¯| c.0->serial out 
;                     <->c.4|___|c.1<->
;                      ->c.3|___|c.2<->
;
start0: ;first task
setint %01000,%01000;set the interupt (only allowed on c.1 to c.4 pixcaxe 08M2)
;im looking here to interrupt on c.3 when high.

pause 5000
high c.0
pause 500
low c.0
goto start0 ; end of first task

start1: ;second task
if pinC.4 = 1 then alarm
goto start1; end of second task

;subroutines under here==================
alarm:
high c.2
goto alarm

interrupt:
high c.1
pause 1000 ' no pause here
low c.1
pause 1000 ' or here

setint or %01000,%01000 ;reset set the same interrupt

return
If I want to set off an internal and external alarm together by an interrupt and I want the external alarm to shut off after 5 minutes on a timer the interrupt routine will be just working by itself (back to single processing). Any programming in other tasks being ignored. Any ideas?
 

AllyCat

Senior Member
Hi,

The main purpose of interrupts is to perform simple, brief, tasks (e.g. transfer a single character from a hardware serial port to memory) and then immediately return control to the main program. So I was quite surprised that technical even conceded that the inability to use a PAUSE within an interrupt (when multi-tasking) was a bug.

What you are attempting to do can almost certainly be done with a "polling" loop. For example, to stop an alarm after 5 minutes, do something like the following partial code:

Code:
mainloop:
do
   .....
   if alarminput = 1 then gosub startalarm
   if alarmon = 0 then time = 0 : endif    ; hold system variable "time" at zero until triggered
   if time >= 300 then gosub alarmtimeout
   .....
loop

startalarm:    ; subroutine
   if alarmon = 1 then return  : endif  ; alarm already activated
   high alarmpin    ; sound the alarm
   alarmon = 1     ; flag to mark that alarm has been triggered
......
return

alarmtimeout:    ;subroutine
   low alarmpin
return
Cheers, Alan..
 

kando

Senior Member
Hi Alan,
Thank you for your reply. Nice little polling program hope you don't mind me using it in a program.

I'm not quite sure what you meant about the use of interrupts being only for brief simple tasks as the manual just states "A polled interrupt is a quicker way of reacting to a particular input combination" and "When the sub-procedure has been carried out, program execution continues from the main program". Then it shows a couple of example sub-routines which are just normal to me.

To me a sub-procedure, I call them sub-routines can be any size from small simple brief tasks to large complicated ones and anything in between. Is this not correct?

The manual also states "With a standard if pin7 =1 then.... type statement the program could take up to two seconds to light the LED as the if statement is not processed during the pause 2000 delay time in the main program loop". This is exactly why I want to use an interrupt because it works very quickly.

I'm not sure what you meant about the pause and multi tasking. Could you explain?
I've just tried my little interrupt program (shown above) again to look at the pause I changed the pause to 5000 it doesn't
seem to pause (how weird). Is this what you mean?
If it is is the only way around that quirk to build in a for.. next counting loop?

Thanks for the input
Ken
 

AllyCat

Senior Member
Hi Ken,

Don't confuse Interrupts with Subroutines which fundamentally are (or should be) rather different. However, interrupts with a PICaxe are a poor imitation of how interrupts would normally be used. Only polled by the operating system (so not necessarily very fast), only one entry point (label), no access to the/a Stack, "nesting" not possible, etc.. Also, for simplicity, PICaxe tends to use PAUSEs where more "serious" programming would use status flags (to prevent actions being attempted "too soon").

Of course you can construct your programs any way you wish (or can make work) but to me, the statement above "Once into an interrupt it stops all other tasks" is a very good reason to exit the interrupt routine as soon (or quickly) as possible ! But also beware that when a PAUSE is interrupted then any remaining time of that pause is not executed after the RETURN (Manual 2, page 216).

My reference to mixing PAUSE with multi-tasking concerned the discussion in the thread that I linked in #2 above. You didn't say where "the manual states...", but anyway I'm not going to attempt to "defend" the manual since there is a very recent thread where several of us have been "commenting" about inaccuracies in the manual(s). Also, single statements often need to be taken in context. ;)

Cheers, Alan.
 

kando

Senior Member
Thank you Alan for that reply. Very nicely put. I agree that you should exit the interrupt as soon as possible but I do think the picaxe interrupt is a very good thing to have.

My reason behind all this is an alarm circuit which is my first attempt with Picaxe stuff and I wanted to know how things should be done. The program uses 3 buttons up-down-select, OLED 16x2 and 3 zones of protection. I have used a 14M2 chip. I needed to know a bit more about interrupts and you have supplied that thank you. I would be quite happy to put the program up on the forum for anyone to use or discuss but I don't know where to put it or for that matter whether any one would like to see it!

Thanks again :)
Ken.
 
Top