Interrupt with two input?

Puuhaaja

Senior Member
At recent project I'm using Picaxe 20x2 uc. There would be need for interrupt which can be activated from pins C.1 OR C.2. Pin C.1 is getting all the time hits from hall effect sensor and pin C.2 is normal button and by pressing it you end up to a menu where is much different kind of parameters which you can adjust. I know that in addition of normal setint command there is also setinflags command but if I understood right from manual I can't use setint and setinflags commands similarly because only one interrupt can be active at any time.
 

inglewoodpete

Senior Member
The 20X2 offers both types of interrupts. As you note, only one type can be used at a time.

If you use SetInt polled interrupts, you must have the input condition valid for long enough so that you can read the input pins after entering the Interrupt Service Routine. Eg b1 = pinsC

If the input condition can be very brief, then use SetIntFlags edge-triggered interrupts. If you use this option, you must use the hInt pins B.0/hint1 and B.1/hint2. The interrupt condition will be latched into the Flags byte (bits hintflag1 or hintflag2).
 

westaust55

Moderator
When using the SETINT command to monitor two pins at once, it is necessary to use the NOT parameter and check when they are NOT both at the "normal" state.

For example, say pins C.1 and C.2 are normally low and either may go high when an interrupt condition occurs.

Then using the NOT parameter we establish the interrupt with
SETINT NOT %00000000 %00000110 ; interrupt if both C.1 and C.2 are not low
 

Puuhaaja

Senior Member
Interrupt:
if bit0 = 1 then
setint %00000000, %00000010 ' Next interupt when C.1=0
let bit0 = 0
end if

if bit0 = 0 then
setint %00000010, %00000010 ' Next interrupt when C.1=1
let bit0 = 1
end if
Every second time interrupt is reacting when C.1 = 1 and vice versa. In addition of this pin state varying interrupt should react when button connected to pin C.2 is pressed. So there is quite complex system but I think that I can solve this problem but I need little help before that.

I read hintsetup and setinflags commands from basic commands section from web but right now I don't have any idea how to write my code.
Pin C.1 is contacted to hall sensor and it varies easily 100 times per second between 0 volt - 2.7volt. Program should react to C.1 interrupts even if the button C.2 is not pressed.
 

westaust55

Moderator
A pulse 100 times a second equates to a pulse every 10 ms.
At default X2 clock speed (8 MHz) the chip will execute approximately 40 BASIC instructions. Note commands some such as GOSUB, RETURN, take a little longer as there are greater overheads.

If you establish the interrupt with SETINT then you will also need a subroutine called/labelled Interrupt: to handle the action.
This subroutine must also re-enable the interrupts prior to the RETURN command so you will need a new SETINT within the Interrupt: subroutine.

The time you spend in the Interrupt: subroutine, you may miss any other interrupt signals coming from the push button switch on input pin C.2.
For that reason you should do the minimum processing in the interrupt subroutine, just identify what is the source of the interrupt and set a flag (variable) that the main program loop will use to perform consequent actions.

If there is a prospect that there could be a back –log with pulses coming faster than being handled, for the flag increment the value in the Interrupt: subroutine each time a pulse is received. Then in the main program decrement the flag/counter variable each time the required action is undertaken.

Without a lot more details about what the required functionality of your program/project is it is difficult to give specific information.
 

Puuhaaja

Senior Member
It's really true that interrupt needs to be really fast if input ports states varies about 100 times per second. It's not a problem because I have tested it earlier without pin c.2 connection and there's 64Mhz option with setfreq command. The reason why every second interrupt reacts when C.1=1 and every second when C.1 = 0 is that there's a rotating magnet which states hall sensor is reading. If rotating stops and magnet ends up to a position where hall sensor reads it states all the time like C.1 = 1 it will have a bad affect to counter. So...before next counting there should be situation when C.1 = 0

What I want that my code would do:
1.st interrupt when C.1=1 OR C.2=1
2.st interrupt when C.1=0 OR C.2=1
3.st interrupt when C.1=1 OR C.2=1
4.st interrupt when C.1=0 OR C.2=1
etc...
Any ideas?
 

hippy

Technical Support
Staff member
What I want that my code would do:
1.st interrupt when C.1=1 OR C.2=1
2.st interrupt when C.1=0 OR C.2=1
3.st interrupt when C.1=1 OR C.2=1
4.st interrupt when C.1=0 OR C.2=1
etc...
Any ideas?
What you can do is have the interrupt routine determine what the next interrupt condition will be and select that before returning but there may be potential issues with simultaneous activations should they occur together.

Systems which are simpler are often the easiest to develop and program so is there some way that you can make it so only the wheel sensor interrupts while the infrequently used button push is polled ?

Is this in any way related to the hay baler project ... ?

http://www.picaxeforum.co.uk/showthread.php?24764-Assistance-with-Interrupts-and-Button-Command
 

Puuhaaja

Senior Member
Reacting to button C.2 pushing can also be done in the program's main loop like
if pinc.2 = 1 then....
but then I have to put this command for so many places. That's the reason Why I'm doing that this way. I have used "normal" interrupt on some projects and I think now it's time to learn more about interrupts. Thanks for the link Hippy. I will follow it because it's quite much similar system than I have.

@Inglewoodpete. I need to learn and test hintsetup and setinflags command. Maybe those command can solve this issue.

@Westaust55. Sorry that I forgot to mention that pin state which is causing interrupt varies but now I know better which is SETINT NOT command and how it works.

I will do some testing during the next few days and will report here. Any advices still wellcome.
 

westaust55

Moderator
Untested but something like this may work:

Code:
Interrupt:
    LET Speed = pinC.1
    LET button = pinC.2
    IF button <> oldbutton THEN
      IF button = 1 THEN
        Buttonflag = 1 ; set flag to indicate a push button switch just been activated
      ELSE
        Buttonflag = 0
      ENDIF
    ENDIF
    Oldbutton = button
    IF Speed = 1 THEN ; speed sensor is detected
	INC Speedflag
    ENDIF
    IF button = 0 THEN
      IF Speed = 1 THEN
        SETINT NOT %00000010 %00000110 ; interrupt if Not C.1 = 1 and C.2 = 0 
      ELSE
        SETINT NOT %00000000 %00000110 ; interrupt if Not C.1 = 0 and C.2 = 0
      ENDIF
    ELSE 
     IF Speed = 1 THEN
        SETINT NOT %00000110 %00000110 ; interrupt if Not C.1 = 1 and C.2 = 1 
      ELSE
        SETINT NOT %00000100 %00000110 ; interrupt if Not C.1 = 0 and C.2 = 1
      ENDIF
    RETURN
You need to action the results of finding the buttonflag and/or speedflag >0 in the main program code.
 

Puuhaaja

Senior Member
I have been away from Picaxe but now I'm back again.

Systems which are simpler are often the easiest to develop and program
I got my system working well when I forgot interrupt which react when button is pushed. Programs main loop is scanning now if button is pushed. It works enough fast. This time I didn't use setint not and setint flags commands but now I know much more how they work.

I wrote my first pieces of code about 11 month ago and now I'm writing programs where are 600-700 lines of code. Quite impressing learning curve and without this forum it would had never happened. Thanks for that.
 

Puuhaaja

Senior Member
I have been away from Picaxe but now I'm back again.

Systems which are simpler are often the easiest to develop and program
I got my system working well when I forgot interrupt which react when button is pushed. Programs main loop is scanning now if button is pushed. It works enough fast. This time I didn't use setint not and setint flags commands but now I know much more how they work.

I wrote my first pieces of code about 11 month ago and now I'm writing programs where are 600-700 lines of code. Quite impressing learning curve and without this forum it would had never happened. Thanks for that.
 
Top