Understanding Interrupts / Interrupt Handlers, PIC
I understand WHAT an interrupt is, just not how it works or how it is implemented. I am using a PIC16F886 and have my switch connected to bit 5 on TRISB. With this, I realize that I need to do a bit of setup before an interrupt can occur. RB0-3 are the LEDS, and RB4-7 are for the interrupts/switch.
With that, I know that I need to enable interrupts in INTCON by setting bits GIE, INTF, and I believe RBIE and not INTE because RB0 is being used by an LED? But I am not sure.
My main concern is: how does an interrupt trigger? Also, things like org 0x04. What is the significance of that in an interrupt? These are the important details that just elude me, but without them I cannot proceed. Any help would be greatly appreciated.开发者_JS百科
From a quick look at the datasheet, 0x4 is the interrupt vector. So you should put the address of the code you want to jump to when an interrupt occurs at address 0x4, just like you've presumably already put the address of the code you want to jump to upon reset at 0x0.
Any change on the input to PORTB can trigger an interrupt, if you have the RBIE bit set and the relevant bit in IOCB. You need to read or write PORTB (in practice, you'll probably want to read it, and check the value of bit 5 to make sure it's the switch that changed), or clear RBIF in order to end the interrupt.
So: if you've enabled the relevant interrupt, it's triggered when the value from your switch is spotted to have changed. At that point, the CPU jumps to the address located from 0x4.
精彩评论