Interrupts

As already told in PIC 16F84A interrupt is to change operation when something goes wrong. There are few options of interrupting in a microcontroller. Options for 16F84A are as follows.

  • RB0/INT External Interrupt
  • RB Port Change Interrupt
  • TMR0 Overflow Interrupt
  • EE Write Complete Interrupt

Each of these needs to be enabled individually by the intcon register. After enabling your Interrupt you have to enable the Global Interrupt Enable(GIE) bit too. GIE bit is the switch for all the interrupts. If you turn it off none of the interrupts will occur. This becomes very useful because when microcontroller is doing something important you wouldn’t want to respond the interrupt. Also while an interrupt is carried out you wouldn’t want another interrupt to be carried out. You dont have worry about it because whenever an interrupt occurs the GIE bit will automatically set to 0. Any Interrupt will cause the program counter to jump to 04h line of the program. So you will write the interrupt program at 04h. You can use the org directive to let the compiler know that you are writing on the 04h line.

example-

org 00h

The main program…..

org 04h

The interrupt program.….

Whenever any of these interrupts occur a flag will be set. There will be four separate flags for these four types of interrupts. This way the microcontroller lets you know which interrupt has occurred. For example you don’t want to run an interrupt routine you wrote for overheating situation, when the actual interrupt is just a TMR0 overflow.

So you just have to check this specific flag is set or not. Check the flag with btfsc or btfss instruction.

Now I’m not going to tell you which address represent INTCON. It’s up to you. Now you must be able to refer a datasheet of any microcontroller and make your own circuit, write your own program.

org    00h
goto   mainProgram ; now its very clear. main Program can be written as a subroutine.

org    04h         ; whenever an intterupt occur the Program Counter will jump here.
goto  interruptRoutine

interruptRoutine
 btfsc  INTCON, The specified bit of RB0/INT External Interrupt flag 
 goto   ExternalInterruptProgram

 btfsc  INTCON, The specified bit of RB Port Change Interrupt flag
 goto   PortChangeInterruptProgram

 btfsc  ......
 goto   ......

 ExternalInterruptProgram ; This will be somewhere in your program as a subroutine
                           The Interrupt Program written to respond the external interupt can be put here.
                           .......
                           .......
                           retfie ; This will instruction will cause the program counter
                                  ; to return to where it was before the interrupt.

PortChangeInterruptProgram 
                           .......
                           .......
 mainProgram            ;This will be somewhere in your program as a subroutine
The Main Program written can be put here.
Ending of the main program will be as your desire,
I mean for someone it might be usefull to keep
the main program running as a loop. For another it wont.

Worth point to note here is the “retfie” instruction. This sets GIE bit back to 1 (which makes the microcontroller ready to accept more interrupts), then it will also cause the program counter to return to where it was before the interrupt occured. If you used the instruction “return” interrupts will not work again during the program since the GIE is not set to 1 automatically which is very bad for your application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: