Interrupt with no return

SD2100

New Member
Hi

I did a quick search on this but did't really turn up anything
but it's probably all been done before.

When using interrupts sometimes you don't want the program
to return to where it was before the interrupt occured, you just
want to stop everything that was happening and perform some
function in the interrupt routine and then return to the start
of the program and wait for a new command. I've removed the
SETINT and RETURN commands and replaced them with POKE 02,0
to set the program counter at the reset address 0x00.

I've tested this and it appears to be working as intended, anybody
see any problems doing this ???.

Code:
#Picaxe 18x
setint %00000001, %00000001  ;Set interrupt to pin0

do
    high 0         ;Pin0 high while waiting for interrupt
loop

Interrupt:	
    low 0          ;Pin0 low on interrupt
    high 1         ;Pin1 high, will go low on reset  
    pause 1000  ;1 second delay before reset
    poke $02,0   ;Put the program counter at reset vector 0x00
 

hippy

Ex-Staff (retired)
POKE 2,0 ... But that is only 8-bits of the PICmicro PC, so it's possibly only luck that it's not jumping off to the start of some 256 byte page. It could be 'crashing and resetting' rather than resetting cleanly.
 

MartinM57

Moderator
When using interrupts sometimes you don't want the program to return to where it was before the interrupt occured, you just
want to stop everything that was happening and perform some
function in the interrupt routine and then return to the start
of the program and wait for a new command. I'v
That's a pretty unusual requirement - why do you think you need to do that?

The normal way real-time system with interrupts work is exactly as you have done - an infinite do...loop with an overarching interrupt routine....although in more complex systems, for various reasons, the interrupts usually just set a flag to indicate that they have occurred and the main do...loop processing does the processing required

And in your example program, replacing the POKE with RETURN will just put you back into the do...loop, which 're-starting' your program will also do...
 

SD2100

New Member
POKE 2,0 ... But that is only 8-bits of the PICmicro PC, so it's possibly only luck that it's not jumping off to the start of some 256 byte page. It could be 'crashing and resetting' rather than resetting cleanly.
Hippy, sorry there should have been a POKE for the PCLATH as well.

MartinM75, Yes it's a bit unusual but I'm just playing with some ideas here, with a very large program the interrupt could come from anywhere and setting a flag in the interrupt routine and checking it after the return is ok
but there would need to be many checks throughout the program and when there is only limited space left every check takes up more room. Also after the interrupt and before the flag checking some things could still happen like turning an output ON and you really don't want to do that. The idea here is when an interrupt occurs some function is performed in the interrupt routine
and the program returns to the start without adding large amounts of code for checking etc.

Tarzan, Reset is for X1, X2's only, I maybe using this on 08m's as well ???
 

hippy

Ex-Staff (retired)
It's hard to really say what heppens without knowing the actual firmware. If you poke PC before PCLATH you could get the previous problem, but poke PCLATH and the first PICmicro GOTO between interpreting your two pokes makes it go off to somewhere in page zero. If the poke handler is a subroutine, PCLATH simply gets restored to what it should be when the PICmicro RETURN gets executed. Either way you have potential problems.

The correct solution is to overwrite the code address which is stored when the interrupt occurs with the address of the start of program, when the interrupt then returns it will use that address and the program restarts. Variables and SFR won't get cleared so it is possible to determine if the program started because of a power-on/reset or an interrupt which can be useful.

I'm sure the location of the return address was determined but I cannot recall what it was.
 

BCJKiwi

Senior Member
Excuse me if I'm off track here, but why couldn't you simply have a
goto Main (or whatever you begin the main program loop with)
in the
interrupt:
procedure?

SetInt would immediately follow the
Main: label.
 

inglewoodpete

Senior Member
Whatever you do after the Interrupt will have to fully reset the chip, otherwise the Interrupt's return address will still be stored. Also, any future interrupts will not be enabled until both the SetInt and Return instructions have both been executed.

"GoTo Main" -type instructions do not reset the chip (zeroise/clear everything).
 

SD2100

New Member
My first thought was to see what was pushed onto the stack but I don't think it is accessable thats when I had the POKEing the PC & PCLATH idea, even though it appears to work with all the outputs going low and variables being cleared it might not be the best approach.

Anyway thanks guys I'll see if I can work something else out.
 

Technical

Technical Support
Staff member
If you have a spare output, you can simply connect a transistor between the reset pin and 0V, and then drive the base of that transistor from the spare output. When the transistor is switched on the reset pin will be activated and the whole device will cleanly reset as you want.

You may also need an extra 10k between the PICAXE output pin and ground, to guarantee the transistor is help off whilst the chip momentarily initialises.
 

SD2100

New Member
Technical: Good option, I was just trying to do it within the code.

Dippy: Missed the shovel joke, but a good wack with the back of a shovel would reset it. maybe that was it ???. :)
 

hippy

Ex-Staff (retired)
For all PICAXE's, including those without a Reset pin, it should be possible to diode mix an output high with Serial In and fake a 'download which doean't happen' reset that way. The PICAXE reset turns the pin back to an input or output low which clears the signal which forced the reset in the first place.

Code:
                              -.- 5V
                               |
TX <---------------------------|------------------------.
                               |   .------------.       |
                  ___          `---| +V      0V |---.   |
RX >---------.---|___|---.---------| SI      O0 |---|---'
            .|.   22K    `---|<|---| X4      X1 |   |
            | |                    | I3      X2 |   |
0V >---.    |_| 10K                `------------'   |
      _|_   _|_                      PICAXE-08M    _|_
Code:
MainProgram:
  SerTxd( "Reset" )
  SetInt %........, %........
  Do
    SerTxd( "Running " )
  Loop

Interrupt:
  HIGH 4
 
Last edited:

SD2100

New Member
Good one Hippy I'll remember that trick, I'm still keen to keep the resetting bizzo inside the chip though.
 
Top