Shouldn't need setint OFF on 18M2, but I seem to need one anyway

I'm sure that I am missing something here but can't figure out what it is.

I thought an interrupt condition resulted in disabling the interrupt. The following example seems to contradict that. It works as expected when simulated but when downloaded into the 18M2, it doesn't do what's expected. Namely, after entering the interrupt, it gets stuck there. After a little trial and a lot of error, the situation was corrected with a "setint OFF' statement in the interrupt routine. From what I understand, and please don't overestimate that, the interrupt should be disabled once an interrupt condition occurs.

The code (with comments):

Code:
' =========================== WhatsUpButton.bas ============================
' This program runs on a PICAXE-18M2. Eight segments of an
' an LED bar display are connected to outputs B.0-B.7 w/resistors bla bla.
' Input switches are on inputs C.(0,1,6,7) with pull-down resistors
' which hold them low (4.7k resistor to ground) and go high when activated.
' Pressing any switch results in counting up binary ONE on the LEDs.
' =========================================================================


' === Constants ====
symbol simu = 1 	'A simulated complex program.


' === Variables ====
symbol LEDs = outpinsB ' Control some LEDs.


' === Directives ===
#picaxe 18M2 	' ID which Rev Ed MCU is used.
#no_data     	' Don't waste time sending nothing.


' ========================== Main Program ==========================
main:
dirsB = %11111111 ' All outputs.
dirsC = %00001000 ' C.3 is an output, C.1,C.2,and C.4-7 are inputs.
LEDs  = %00000000 ' start count at 0
setint OR %11000011, %11000011, C ; interrupt if C.(0,1,6,7) are high

do                ' Just sitting around and waiting. . .
	b11 = pinsC ' See what's going on here.
	debug       ' Show what's going on here.
	wait simu   ' Simulate a significant and useful program.
loop			' Round and round . . .


' ==================== Here Come The Subroutines=== ================

interrupt:
	setint OFF  ' Exactly why is this a 'works' vs. 'don't work' item?
	
' If you remove the "setint OFF" statement, the simulation will still
' run just like one would expect. However, download to the 18M2 (v2.B)
' without the 'setint OFF' statement and it just don't do right.
' The first press of the button sends the program into the land of limbo.
' The latest manual (as of 8-19-2011) states: "When the interrupt occurs, 
' the interrupt is permanently disabled." If this is correct for my 18M2
' (v2.B), then, AFAIK, the 'setint OFF' statement shouldn't be necessary.
' BUT, after trying everything else, it seems to be necessary.  ??????

b11 = pinsC 	' See what's going on here.
	debug 	' Show what's going on here.



	inc LEDs 	' Do something interesting after pressing a button.
	pause 25 	' Debounce for make
	
	ecnuob:
		if pinC.0 is on then GOTO ecnuob ' Wait for break.
		if pinC.1 is on then GOTO ecnuob ' Wait for break.
		if pinC.6 is on then GOTO ecnuob ' Wait for break.
		if pinC.7 is on then GOTO ecnuob ' Wait for break.
		pause 25 ' Debounce for break.
		
	if LEDs = 256 then
		LEDs = 1   'Roll over if you don't have enough LED's etc.
	endif
	
setint OR %11000011, %11000011, C 'Since it's disabled, enable it.
return       	' Get back to where you once belonged.
What am I missing?
 
Last edited:

inglewoodpete

Senior Member
Trye the following variation of your code. It will dump heaps of data to the terminal window but it should tell you how often and when it is getting interrupts. You will probably only have to run it for a few seconds.

The SerTxd Command offers some benefits over debug: you can change the printed value to clarify where the program is executing.

Code:
' =========================== WhatsUpButton.bas ============================
' This program runs on a PICAXE-18M2. Eight segments of an
' an LED bar display are connected to outputs B.0-B.7 w/resistors bla bla.
' Input switches are on inputs C.(0,1,6,7) with pull-down resistors
' which hold them low (4.7k resistor to ground) and go high when activated.
' Pressing any switch results in counting up binary ONE on the LEDs.
' =========================================================================


' === Constants ====
symbol simu = 1 	'A simulated complex program.


' === Variables ====
symbol LEDs = outpinsB ' Control some LEDs.


' === Directives ===
#picaxe 18M2 	' ID which Rev Ed MCU is used.
#no_data     	' Don't waste time sending nothing.
#terminal 4800


' ========================== Main Program ==========================
main:
dirsB = %11111111 ' All outputs.
dirsC = %00001000 ' C.3 is an output, C.1,C.2,and C.4-7 are inputs.
LEDs  = %00000000 ' start count at 0
setint OR %11000011, %11000011, C ; interrupt if C.(0,1,6,7) are high

do                ' Just sitting around and waiting. . .
	b11 = pinsC ' See what's going on here.
	'debug       ' Show what's going on here.
	SerTxd("[", #b11, "]")
	wait simu   ' Simulate a significant and useful program.
loop			' Round and round . . .


' ==================== Here Come The Subroutines=== ================

interrupt:
	'setint OFF  ' Exactly why is this a 'works' vs. 'don't work' item?
	
' If you remove the "setint OFF" statement, the simulation will still
' run just like one would expect. However, download to the 18M2 (v2.B)
' without the 'setint OFF' statement and it just don't do right.
' The first press of the button sends the program into the land of limbo.
' The latest manual (as of 8-19-2011) states: "When the interrupt occurs, 
' the interrupt is permanently disabled." If this is correct for my 18M2
' (v2.B), then, AFAIK, the 'setint OFF' statement shouldn't be necessary.
' BUT, after trying everything else, it seems to be necessary.  ??????

	b11 = pinsC ' See what's going on here.
	'debug 	' Show what's going on here.
	SerTxd #b11)


	inc LEDs 	' Do something interesting after pressing a button.
	pause 25 	' Debounce for make
	
ecnuob:
	SerTxd(".")
	if pinC.0 is on then GOTO ecnuob ' Wait for break.
	if pinC.1 is on then GOTO ecnuob ' Wait for break.
	if pinC.6 is on then GOTO ecnuob ' Wait for break.
	if pinC.7 is on then GOTO ecnuob ' Wait for break.
	pause 25 ' Debounce for break.
		
	if LEDs = 256 then
		LEDs = 1   'Roll over if you don't have enough LED's etc.
	endif
SerTxd("-")	
setint OR %11000011, %11000011, C 'Since it's disabled, enable it.
return       	' Get back to where you once belonged.
Obviously, remove the SerTxds once you've sorted your bugs out.
 

hippy

Ex-Staff (retired)
Welcome to the PICAXE Forum.

I'm not sure how you mean by gets stuck in the interrupt or how you are determining that. Your code appeared to work for me using a physical 18M2, with "SETINT OFF" present and removed.

Could it be that one or more of your interrupt inputs is floating ? If from push switches are all pull-down resistors fitted ?

Though I don't think it is the problem you are experiencingm note that the 'if LEDs = 256 then" statement ( where LEDs is 'outpinsB' ) will never evaluate as true; byte variables can only hold values 0 to 255.
 
Thanks for the very useful responses. I'm not sure what was going wrong. I made final tests with and without the 'settint OFF' statement immediately before my post last night with the results as described. Then I threw a towel over it all and turned in. This morning, I tried the 'SerTxd' method instead of 'debug' as suggested by inglewoodpete and it worked fine with, and without the 'setint OFF.' Then I tried my original code as posted and that worked fine with and without 'setint OFF' too. I hadn't done anything to the hardware so I guess SerTxd chased out those pesky Leprechauns. Or perhaps something wiggled about on the proto bench and made better contact.

Anyway, 'SerTxd' has been added to my toolkit for future use.

What happened when I was 'getting stuck' was the hardware would shift to the second LED being lit after the first button push and then there was no response from pushing any other buttons and no change on which LED was lit. The 'if LEDs = 256 then' statement was a holdover from another program. I guess I could have left that whole 'if .... endif' section out because 'inc LEDs' rolls over to 0 anyway when LEDs is 255 and gets inc'd.

Here's the diagram for the input buttons I am using:
PB(input).jpg

Now I can move on to actually finish the project.

Thanks again for the responses.

Great forum!
 
Last edited:

premelec

Senior Member
I would suggest adding a small capacitor between input pin and V- just to filter possible noise pick up and siwtch bounce...
 

inglewoodpete

Senior Member
Another small point. The maximum number for an 8-bit number is 255.

You have the following commands etc:

' === Variables ====
symbol LEDs = outpinsB ' Control some LEDs. [IWP: not really a variable but an 8-bit port]

LEDs = %00000000 ' start count at 0 [IWP: that's fine]

inc LEDs ' Do something interesting after pressing a button. [IWP: that's fine too]

if LEDs = 256 then [IWP: Will never reach this value max = 255]
LEDs = 1 'Roll over if you don't have enough LED's etc.
endif
 
Top