Setint Nightmare

outfoxed

New Member
Please help a newbie. I've spent days now trying to figure out how to read multiple switches using the setint command and am getting nowhere. I have a 40x and want to identify which of the eight inputs has been used and simply display a batch of leds in different sequences. I tried without the setint but with the number of pauses I want to use there is a noticable lag with the size of the program. I thought this kind of thing may work:

Code:
let b0= 0
setint %00000001,%00000001
main:
		pause 100
		goto main

interrupt:
		if b0 = 1 then sw_1
		if b0 = 0 then sw_0
		return
		
sw_0:
		gosub chase0
		b0= 1
		goto main

sw_1:
		gosub chase1
		b0= 2
		goto main

chase0:
		high 0
		pause 100
		high 1
		pause 100
		let pins = %00000000
		setint %00000011,%00000010
		return
		
chase1:
		high 1
		pause 100
		high 0
		pause 100
		let pins = %00000000
		setint %00001111,%00000000
		return
and it does in the simulator, but doesn't in real life. I need to the recognise the switches as they also send power to other devices and want to sync it all.

Any help would be a god send.

thanx
peet
 

BeanieBots

Moderator
Only the X1 parts support a NOT condition for interrupts.
For the other parts, it is only possible to interrupt on ONE input.
That does not need to be a problem though if you want multiple switches to trigger an interrupt.
Simply diode OR all the switches to the one input used for the interrupt as well as connecting them to their own inputs.
Thus, ANY of the switches will activate the same interrupt. The interrupt routine itself can then read the other inputs and determine which switch caused the interrupt.
 

outfoxed

New Member
I may indeed have a X1 chip, I can't be sure but believe it is. I can't find any info on the not condition for the interrupt call. Could you please point me in the right direction.

Thanx again
peet
 

BeanieBots

Moderator
Page 146 of manual 2.
The 'NOT' part is the same as a normal interrupt except the interrupt happens when the port/mask conditions are NOT met. ie if ANY of the selected inputs does NOT match the pattern, then interrupt.
This is only available with X1 parts.
 

kranenborg

Senior Member
Hello,

Concerning diode-ORing (also known as diode-mixing on this forum), Beaniebots points to an very simple and practical approach, implemented & documented first by Andrew Whitehead on his AxeBot, hippy refers to the project: http://www.hippy.freeserve.co.uk/picaxeq2.htm#How_do_I_handle_Multiple_Source_Interrupts (currently Andrew's page seems to be unaccessible, hopefully this changes soon)

What I often do in practice is to use part of an I/O expander to function as an interrupt controller as well, as most modern I/O expanders have a common INT output, whose behaviour can be programmed. Good examples are mcp23008, mcp23017, max6956, all based on i2c but have SPI counterparts as well. A pro to this approach is that interrupts will be latched by the I/O device intil the interrupt register is read, and thus you will not miss any other interrupt occuring at almost (but not exactly) the same time.

In general however the diode mixing works perfectly and is very cheap, and I think it will work very well for your application.

Regards,
Jurjen
 
Last edited:

hippy

Technical Support
Staff member
Code:
let b0= 0
setint %00000001,%00000001
main:
		pause 100
		goto main

interrupt:
		if b0 = 1 then sw_1
		if b0 = 0 then sw_0
If 'b0' has to contain an indicator of which button is pushed that caused the interrupt you will need a 'b0=pins' to read the actual pins. You would also be better off testing the 'bit0', 'bit1' etc variables than comparing 'b0' with various values.

There is a caveat with using this method. Due to the delay between pushing a button, SETINT being activated and the interrupt routine being entered, what's on the pins when read may not be the same as what was on the pins to cause the interrupt ( through button bounce etc ). In practice it doesn't appear to be a problem but it is something to be aware of.
 

outfoxed

New Member
Well I must have the x1 chip because I'm getting better feedback, but I've gone back a step!!

I'm now having problems with my switches, it appears they are all on!

My first tests had me running my switches from input 0 to v+. This was fine. With multiple switches its not working any more. I have read you need a resistor but I have the project board with resistors already mounted. The circuit works perfect if i plug into input 0 and hold the wire in my hand, if i plug it into v+ every led lights up! What on earth is happening?

outfoxed again
 

BeanieBots

Moderator
Well I must have the x1 chip because I'm getting better feedback
Do what??

What is the number on the chip?
What firmware is reported when you do a firmware check?

Resistors are required with switches. They can be either pull up or pull down but MUST be fitted one way or the other.

Which project board?
At the start of this thread you had a 40X

All the LEDs light up? Under program control? Your control?
 
Last edited:

outfoxed

New Member
Thanks for your help, as you can tell I'm a complete circuit novice, and had assumed that the switch had the required resistor (as mentioned on schematic). This was obviously not true because of the way it was acting.. :eek:
I've had a quick google about pull-up resistors (the very very basics) and it sounds like it's exactly the problem I have.
I'll head out and get a bunch of 10k resistors and try again.

Most of the help here assumes some knowledge of circuits, it's great to get some feedback when you have no idea what to fix.

thanx for your patience
peet
 

hippy

Technical Support
Staff member
I'm now having problems with my switches, it appears they are all on!

My first tests had me running my switches from input 0 to v+. This was fine. With multiple switches its not working any more. I have read you need a resistor but I have the project board with resistors already mounted. The circuit works perfect if i plug into input 0 and hold the wire in my hand, if i plug it into v+ every led lights up! What on earth is happening?

outfoxed again
If you have code which acts when a switch ( any of the switches ) connected to it is operated and you move those switches elsewhere and do not update the code to reflect that, not a lot is going to happen.

Seeing something happen on input 0 when holding the wire is the same as pushing a button if you don't have the correct resistors in place. It's 'radio waves' picked up which the PICAXE is seeing with you acting as an aerial.

As to every Led lighting up; I presume that's because you've told your software to do that; turn Led's on when input 0 is high. Don't forget we don't have a clue how your circuit is wired or what program code you are running so it's hard to say exactly what is going on.

As you're not familiar with PICAXE's ( we all start knowing nothing ) it may be an idea to start simpler, getting used to turning a Led on when a button is pushed and building up from there. Diode-mixed button inputs and interrupt handling will be hard to understand if you haven't got to grips with the basics first.
 

outfoxed

New Member
My problem was that I need this to actually use in the next week!! It was a cheaper option than an expensive light controller and I can rig it to send power with some 5v relays as well!!

My problem was the switches ... now that I have used the resistors I'm all good. The code was pretty small once I had the 'setint not' command which wasn't in the manual I got with the chip.

Code:
setint not %00000000,%11111111
let pinsc = %00000000
main:		
		pause 2000
		goto main

interrupt:
		if pin0 = 1 then gosub sw_0
		if pin1 = 1 then gosub sw_1
		setint not %00000000,%1111111
		return
		
sw_0:
		let pins = %00000010
		wait 1
		let pins = %00000001
		wait 1
		let pins = %00000000
		return
sw_1:
		let pins = %00010000
		wait 1
		let pins = %00100000
		wait 1
		let pins = %00000000
		return
Again, thanks for your help, mush appreciated.

No longer outfoxed.. :D
 
Top