PDA

View Full Version : Which interrupt ?



skybox
- 7th July 2013, 19:33
Hi All,

I'm using PBP and 16F887 MCU.

I'm employing PortB pin 2 & 3 for interrupts and it works great. I have two push buttons, one for each pin. What I want to happen is when I push the first button, pin 2, a variable increases by one and when pushing the second button, pin 3, it decrements by one. As stated, the interrupt piece works as expected and I have no issue with writing the code inside the handler, I just don't know how the mechanics work for directing either interrupt to the correct handler. Right now they both share the same handler.

Lots of information available for designing the interrupt but I can't find anything relating to my problem. Any assistance would be appreciated.

Archangel
- 7th July 2013, 19:54
Just thinking out loud here . . . when the interrupt (IOC) occurs you want to read the port, & save it to a temp variable. in your main loop read the variable & act early on, if your main loop is long and busy then check often. I would not act in the ISR unless critical, just to keep the ISR short & sweet, if it is critical, if micro second response is required then act there.
example;



switchVar var byte ' var to store port value
doSomething var byte ' counter var

main:
do this . . . bla bla 'your code
gosub Check
do more . . .' more yourcode

for doSomething = 0 to 50
gosub check
yourcode
next doSomething

morecode . . .
gosub check

goto Main

Check:
if switchVar = 0 then
return
else
do something with the information in switchVar
switchVar = 0 ' clear the variable after work done
endif

return


You could also read the interrupt flag bit to see if interrupt has come, esp if using PBP on interrupt. Asm interrupts are so fast, they would likely be cleared before you loop ever go there though. I am sure others have better solutions, but this will get you started.
IN Conclusion:
ISR
read the port
store port value in a var
exit ISR 'assuming ISR clears IOC Flag
do something with the information in your program

skybox
- 7th July 2013, 22:52
I like the idea but its weird as to what happens. I push the button which generates the interrupt. I watch the status of the "HoldB" variable which is the first statement in the ISR - and it does change to reflect the change on pin 2 or 3 (what ever is pressed) but then HoldB immediately switches back to reflect POrtB again. I don't get it?

Example:

- Push button
- bit 3 of Port B changes from high to low causing interrupt
- Goes to ISR
- HoldB grabs PortB status
- Pin 2 switches back to high state
- HoldB automatically changes also (this is the part I don't understand)

Here is the sample code I used:

Disable

MyISR:
HoldB = PortB ' copy Port B into HoldB

Pause 100
CV = CV + 1
endif
INTCON.0 = 0

Resume

Archangel
- 7th July 2013, 23:12
Are you using ON INTERRUPT GOTO ?
If so did you DISABLE and Enable interrupts during your ISR ? it sounds as if it is repeatedly interrupting before action is taken. I E Fast interrupt, slow finger . . . You can wait to re-enable the interrupt until after your subprogram does it's job . . .

skybox
- 7th July 2013, 23:33
It's saving PortB in HoldB but after the pin goes back to its high state. In other words, Pin 2 is changing back to it's high state before it goes to ISR - at least it seems that way.


Disable Interrupt

MyISR:
HoldB = PortB
' Interrupt service routine
Pause 100
CV = CV + 1

INTCON.0 = 0

Resume
Enable Interrupt