Problem running simulation on Editor 6.0.7.4

Running simulation on the code shown below I am getting a Stack overflow message. Apparently the simulator is ignoring the Setint command that is located before Return

Code:
[color=Navy]#Picaxe [/color][color=Black]08M2[/color]
[color=Green]'*** Variables ***[/color]
[color=Blue]Symbol [/color][color=Purple]DLY[/color][color=DarkCyan]=[/color][color=Purple]W0[/color]
[color=Blue]Symbol [/color][color=Purple]Pulse_dur[/color][color=DarkCyan]=[/color][color=Purple]W1[/color]
[color=Blue]Symbol [/color][color=Purple]N[/color][color=DarkCyan]=[/color][color=Purple]b4[/color]

[color=Green]'*** Pins ***[/color]
[color=Blue]Symbol [/color][color=Purple]Merc[/color][color=DarkCyan]=[/color][color=Purple]Pin4

Pulse_dur[/color][color=DarkCyan]=[/color][color=Navy]3000

      [/color][color=Blue]setint [/color][color=Navy]%10000[/color][color=Black],[/color][color=Navy]%10000[/color]
[color=Black]Waiting:
      [/color][color=Blue]Low [/color][color=Navy]0[/color][color=Black],[/color][color=Navy]1
      [/color][color=Blue]if [/color][color=Purple]Merc[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Blue]then goto [/color][color=Black]Waiting[/color]
[color=Blue]Interrupt:
      [/color][color=Purple]DLY[/color][color=DarkCyan]=[/color][color=Navy]0
      [/color][color=Blue]If [/color][color=Purple]Pin2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]Dly[/color][color=DarkCyan]=[/color][color=Navy]3      [/color][color=Green]'1.5 minutes
      [/color][color=Blue]endif
      If [/color][color=Purple]Pin3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]Dly[/color][color=DarkCyan]=[/color][color=Navy]6      [/color][color=Green]'3.0 imutes
      [/color][color=Blue]endif
      High [/color][color=Navy]0
      [/color][color=Blue]Low [/color][color=Navy]1
      [/color][color=Blue]Pause [/color][color=Purple]Pulse_dur
      [/color][color=Blue]low [/color][color=Navy]0[/color][color=Black],[/color][color=Navy]1
      [/color][color=Blue]For [/color][color=Purple]N[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]to [/color][color=Purple]DLY
      [/color][color=Blue]Pause [/color][color=Navy]30000
      [/color][color=Blue]Next [/color][color=Purple]N
      [/color][color=Blue]low [/color][color=Navy]0
      [/color][color=Blue]high [/color][color=Navy]1
      [/color][color=Blue]Pause [/color][color=Purple]Pulse_dur
      [/color][color=Blue]low [/color][color=Navy]1[/color][color=Black],[/color][color=Navy]0
      [/color][color=Blue]Pause [/color][color=Navy]1000
      [/color][color=Blue]setint [/color][color=Navy]%10000[/color][color=Black],[/color][color=Navy]%10000
      [/color][color=Blue]pause [/color][color=Navy]1000[/color]
[color=Blue]Return[/color]
 

Technical

Technical Support
Staff member
Your code is flawed.

If the timing of the input pin change happens at an exact moment in time, you can simply fall out of the

if pin4 = 0 then goto waiting

line into the 'interrupt' section of code, without properly 'gosub interrupting' into it. Then the interrupt fires, processes and returns you back into interrupt section.

This will cause a stack error upon the final return.

Why can't this line just be 'goto Waiting' instead?
 

Goeytex

Senior Member
Works ok for me with no stack overflow. Perhaps you could explain when the stack overflow happens, i.e, immediately, after 10 loops through the int routine, etc.
What pins are high or low when it happens?.

Also, there is no need to test pinc.4 (merc) in the wait loop. The interrupt is doing that already. I would remove that line.

Does is work in PE5 ?
 
Thank you Goeytex and Technical it works now

Works ok for me with no stack overflow. Perhaps you could explain when the stack overflow happens, i.e, immediately, after 10 loops through the int routine, etc.
What pins are high or low when it happens?.

Also, there is no need to test pinc.4 (merc) in the wait loop. The interrupt is doing that already. I would remove that line.

Does is work in PE5 ?
Thank you Technical and Goeytex.
I removed the "if Merc=0 then goto Waiting" line and it works fine on the simulator and on the breadboard.
 

hippy

Technical Support
Staff member
I removed the "if Merc=0 then goto Waiting" line and it works fine on the simulator and on the breadboard.
Are you sure ?

It still falls through the start-up code straight into interrupt, so eventually hits the RETURN without any previous GOSUB or interrupt having occurred, generates a stack underflow in the simulation as it should.
 

Goeytex

Senior Member
It does not fall through for me. In fact with PE6 it never showed a stack overflow for me. However I saw the possibility with flawed "if merc = " statement.

I have modified the code to use the proper PINC.x format, removed or remmed unnecessary commands / and added some formatting / comments to make the code more understandable

Code:
[color=Navy]#Picaxe [/color][color=Black]08M2[/color]
[color=Green]'*** Variables ***[/color]
[color=Blue]Symbol [/color][color=Purple]DLY [/color][color=DarkCyan]= [/color][color=Purple]W0[/color]
[color=Blue]Symbol [/color][color=Purple]N [/color][color=DarkCyan]= [/color][color=Purple]b4[/color]

[color=Green]'*** Pins ***[/color]
[color=Blue]Symbol [/color][color=Purple]Merc [/color][color=DarkCyan]= [/color][color=Purple]PinC.4[/color]

[color=Blue]Symbol Pulse_dur [/color][color=DarkCyan]= [/color][color=Navy]3000 [/color][color=Green]'// Constant instead of var w1 / saves variable [/color]

[color=Black]init:[/color]
[color=Blue]setint [/color][color=Navy]%10000[/color][color=Black],[/color][color=Navy]%10000 [/color][color=Green]'// int on Pinc.4 high[/color]
[color=Blue]low C.0[/color][color=Black],[/color][color=Blue]C.1[/color]

[color=Green]'// Waiting for interrupt on Pin C.4
'// Assuming 10K Pull down resistor on C.4
'// to prevent pin floating @ power up[/color]

[color=Black]Waiting:   
     [/color]
[color=Blue]Do
    [/color][color=Green]'// lowC.0,C,1  (unnuecessary/redundant - already done in int routine)
    '// if Merc = 0 then goto Waiting (could cause Problem)  
    [/color][color=Blue]Pause [/color][color=Navy]1000  [/color][color=Green]'// reduces pwr consumption / speeds up int detection [/color]
[color=Blue]loop

Interrupt:

      [/color][color=Purple]DLY [/color][color=DarkCyan]= [/color][color=Navy]0   [/color][color=Green]'// unnecessary  
      [/color][color=Blue]If [/color][color=Purple]PinC.2 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]Dly [/color][color=DarkCyan]= [/color][color=Navy]3      [/color][color=Green]'1.5 minutes
      [/color][color=Blue]endif
      
      If [/color][color=Purple]PinC.3 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Purple]Dly [/color][color=DarkCyan]= [/color][color=Navy]6      [/color][color=Green]'3.0 imutes
      [/color][color=Blue]endif
      
      High C.0 [/color][color=Black]: [/color][color=Blue]Low C.1 [/color][color=Black]: [/color][color=Blue]Pause Pulse_dur
      
      [/color][color=Green]'long delay loop
      [/color][color=Blue]low C.1
      For [/color][color=Purple]N [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]to [/color][color=Purple]DLY
          [/color][color=Blue]Pause [/color][color=Navy]30000
      [/color][color=Blue]Next [/color][color=Purple]N
      
      [/color][color=Blue]low C.0 [/color][color=Black]: [/color][color=Blue]high C.1 [/color][color=Black]: [/color][color=Blue]Pause Pulse_dur
      low C.1[/color][color=Black],[/color][color=Blue]C.0 [/color][color=Black]: [/color][color=Blue]Pause [/color][color=Navy]1000
      
      [/color][color=Blue]setint [/color][color=Navy]%10000[/color][color=Black],[/color][color=Navy]%10000
      [/color][color=Blue]pause [/color][color=Navy]1000[/color]
[color=Blue]Return[/color]
This works for me, but for simulation I would suggest reducing the delays to make it less agonizing to debug.
 
Last edited:

bpowell

Senior Member
I generally try to ensure the interrupt condition is cleared before rearming the interrupt... Otherwise, the picaxe can jump out of the interrupt (before the return) right back into the interrupt.... Causing a push but no pop on the call stack.... This could lead to an overflow condition.
 
Thank you Hippy

Are you sure ?

It still falls through the start-up code straight into interrupt, so eventually hits the RETURN without any previous GOSUB or interrupt having occurred, generates a stack underflow in the simulation as it should.
You are right, it would not work by just removing "if Merc=0 then goto Waiting".
What I did is replace with Goto Waiting and it works in simulation an on the breadboard circuit

Code:
[color=Navy]#Picaxe [/color][color=Black]08M2[/color]
[color=Green]'*** Variables ***[/color]
[color=Blue]Symbol [/color][color=Black]DLY[/color][color=DarkCyan]=[/color][color=Purple]W0[/color]
[color=Blue]Symbol [/color][color=Black]Pulse_dur[/color][color=DarkCyan]=[/color][color=Purple]W1[/color]
[color=Blue]Symbol [/color][color=Black]N[/color][color=DarkCyan]=[/color][color=Purple]b4[/color]

[color=Green]'*** Pins ***[/color]
[color=Blue]Symbol [/color][color=Black]Merc[/color][color=DarkCyan]=[/color][color=Purple]Pin4[/color]

[color=Black]Pulse_dur[/color][color=DarkCyan]=[/color][color=Navy]3000
      [/color][color=Blue]setint [/color][color=Navy]%10000[/color][color=Black],[/color][color=Navy]%10000[/color]
[color=Black]Waiting:
      [/color][color=Blue]Low [/color][color=Navy]0[/color][color=Black],[/color][color=Navy]1
      [/color][color=Blue]Goto [/color][color=Black]Waiting[/color]
[color=Blue]Interrupt:
      [/color][color=Black]DLY[/color][color=DarkCyan]=[/color][color=Navy]0
      [/color][color=Blue]If [/color][color=Purple]Pin2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Black]Dly[/color][color=DarkCyan]=[/color][color=Navy]3      [/color][color=Green]'1.5 minutes
      [/color][color=Blue]endif
      If [/color][color=Purple]Pin3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]then let [/color][color=Black]Dly[/color][color=DarkCyan]=[/color][color=Navy]6      [/color][color=Green]'3.0 imutes
      [/color][color=Blue]endif
      High [/color][color=Navy]0
      [/color][color=Blue]Low [/color][color=Navy]1
      [/color][color=Blue]Pause [/color][color=Black]Pulse_dur
      [/color][color=Blue]low [/color][color=Navy]0[/color][color=Black],[/color][color=Navy]1
      [/color][color=Blue]For [/color][color=Black]N[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Blue]to [/color][color=Black]DLY
      [/color][color=Blue]Pause [/color][color=Navy]30000
      [/color][color=Blue]Next [/color][color=Black]N
      [/color][color=Blue]low [/color][color=Navy]0
      [/color][color=Blue]high [/color][color=Navy]1
      [/color][color=Blue]Pause [/color][color=Black]Pulse_dur
      [/color][color=Blue]low [/color][color=Navy]1[/color][color=Black],[/color][color=Navy]0
      [/color][color=Blue]Pause [/color][color=Navy]1000
      [/color][color=Blue]setint [/color][color=Navy]%10000[/color][color=Black],[/color][color=Navy]%10000[/color]
[color=Blue]Return[/color]
 
Last edited:
Thank you Goeytex, as always, very helpful.

It does not fall through for me. In fact with PE6 it never showed a stack overflow for me. However I saw the possibility with flawed "if merc = " statement.

I have modified the code to use the proper PINC.x format, removed or remmed unnecessary commands / and added some formatting / comments to make the code more understandable
I run your code on the breadboard circuit and runs fine.
Andres
 

bpowell

Senior Member
I run your code on the breadboard circuit and runs fine.
Andres
I would imagine your original code would run on the breadboard as well...even if the Picaxe overflows the stack, it'll just reset (I assume) and start from the beginning with a clean stack. It's a loop!
 
Top