# interrupt handler

I have wrote below code segment. But, I could not figure out why it is not work. Can you help to fix ?

PIC16F877

init:
TRISB = b'11110000'
rbif  = 0
rbie  = 1
gie   = 1
isr:
btfss  INTCON, RBIF
goto   not_pressed
goto   pressed

pressed:
btfsc    PORTB, RB4
goto     $+2 goto RB4_handler btfsc PORTB, RB7 goto$+2
goto     RB7_handler

-
No, we can't. A bug is a discrepancy between what the code does and what the spec says it should do. You haven't provided the spec, so obviously we can't say what is wrong. Without a spec, it's working perfectly. In addition to what Wouter said, what language is this in? How did this mess ever compile or assemble? It looks like you are thinking C at the top but later it looks like MPASM. Note that MPASM "=" is very different from C "=" to the point I strongly recommend not using "=" in MPASM, use EQU instead. You should have gotten lots of assembly errors. – Olin Lathrop Apr 14 '12 at 12:45

Do NOT use the $+2 construct! It makes the code difficult to read and error prone to write (is +2 one instruction or two?). Use labels, like you do elsewhere. Inside an interrupt handler you can't be sure what PCLATCH will contain, so you can't do any goto's (or calls) untill you have set PCLATH. And of course you must first save the old PCLATH so you can restore it at the end of the ISR. If you are writing an interrupt hander you will need to save an restore the context, which must be done in a complex way on these chips. Check the datasheet for examples. You don't show the handler code, so I can't check that. It must at least clear the interrupt source bit, and end with a RETFIE. - "$+2" is indeed a Bad Habit™, but maybe you should explain why, and tell him what he should use instead. – stevenvh Apr 14 '12 at 8:21