2.6 Interrupts Interrupts are a mechanism of a microcontroller which makes it possible to respond to some events at the moment when they occur, regardless of what microcontroller is doing at the time. This is a very important part, because it provides connection between a microcontroller and a real world which surrounds us. Generally, each interrupt changes the flow of program execution, interrupts it and after executing an interrupt subprogram (interrupt routine) it continues from that same point on. One of the possible sources of an interrupt and how it affects the main program Control register of an interrupt is called INTCON and is found at 0Bh address. Its role is to allow or disallowed interrupts, and in case they are not allowed, it registers specific interrupt requests through its own bits. INTCON Register bit 0 RBIF (RB Port Change Flag bit) Bit which informs about changes on pins 4, 5, 6 and 7 of port B. 1=at least one pin has changed its status 0=no change occured on any of the pins bit 1 INTF (INT External Interrupt Flag bit) External interrupt occured. 1=interrupt occured 0=interrupt did not occur If a rising or falling edge is detected on pin RB0/INT, (which is defined with bit INTEDG in OPTION register), bit INTF is set. Bit must be reset in interrupt subprogram in order to detect the next interrupt. bit 2 T0IF (TMR0 Overflow Interrupt Flag bit) Overflow of counter TMR0. 1= counter changed its status with FFh 00h 0=overflow did not occur Bit must be reset in program in order for an interrupt to be detected. bit 3 RBIE (RB port change Interrupt Enable bit) Enables interrupts to occur at the change of status of pins 4, 5, 6, and 7 of port B. 1= enables interrupts at the change of status 0=interrupts disabled at the change of status If RBIE and RBIF are simultaneously set, an interrupt will occur. bit 4 INTE (INT External Interrupt Enable bit) Bit which enables external interrupt from pin RB0/INT. 1=external interrupt enabled 0=external interrupt disabled If INTE and INTF are set simultaneously, an interrupt will occur. bit 5 T0IE (TMR0 Overflow Interrupt Enable bit) Bit which enables interrupts during counter TMR0 overflow. 1=interrupt enabled 0=interrupt disabled If T0IE and T0IF are set simultaneously, interrupt will occur. Bit 6 EEIE (EEPROM Write Complete Interrupt Enable bit) Bit which enables an interrupt at the end of a writing routine to EEPROM 1=interrupt enabled 0=interrupt disabled If EEIE and EEIF (which is in EECON1 register) are set simultaneously , an interrupt will occur. Bit 7 GIE (Global Interrupt Enable bit) Bit which allows or disallows all interrupts. 1=all interrupts are enabled 0=all interrupts are disabled PIC16F84 has four interrupt sources: 1. Termination of writing data to EEPROM 2. TMR0 interrupt caused by timer overflow 3. Interrupt during alteration on RB4, RB5, RB6 and RB7 pins of port B. 4. External interrupt from RB0/INT pin of microcontroller Generally speaking, each interrupt source has two bits joined to it. One enables interrupts, and the other detects when interrupts occur. There is one common bit called GIE which can be used to disallow or enable all interrupts simultaneously. This bit is very useful when writing a program because it allows for all interrupts to be disabled for a period of time, so that execution of some important part of a program would not be interrupted. When instruction which resets GIE bit is executed (GIE=0, all interrupts disallowed), any interrupt that remained unsolved should be ignored. Outline of 16F84 microcontroller interrupt Interrupts which remained unsolved and are ignored, are processed when GIE bit (GIE=1, all interrupts allowed) is reset. When interrupt is answered, GIE bit is reset so that any additional interrupts would be disabled, return address is pushed onto stack and address 0004h is written in program counter - only after this does replying to an interrupt begin! After interrupt is processed, bit whose setting caused an interrupt must be reset, or interrupt routine will automatically be processed over again during a return to the main program. Keeping the contents of important registers Only return value of program counter is stored on a stack during an interrupt (by return value of program counter we mean the address of the instruction which was to be executed, but was not because interrupt occured). Keeping only the value of program counter is often not enough. Some registers which are already in use in the main program can also be in use in interrupt routine. If they were not retained, main program would during a return from an interrupt routine get completely different values in those registers, which would cause an error in the program. One example for such a case is contents of the work register W. If we suppose that main program was using work register W for some of its operations, and if it had stored in it some value that is important for the following instruction, then an interrupt which occurs before that instruction will change the value of work register W which will directly influence the main program. Procedure of recording important registers before going to an interrupt routine is called PUSH, while the procedure which brings recorded values back, is called POP. PUSH and POP are instructions with some other microcontrollers (Intel), but are so widely accepted that a whole operation is named after them. PIC16F84 does not have instructions like PUSH and POP, and they have to be programmed. One of the possible cases of errors if saving is not done when going to a subprogram of an interrupt Due to simplicity and frequent usage, these parts of the program can be made as macros. The concept of a Macro is explained in "Program assembly language". In the following example, contents of W and STATUS registers are stored in W_TEMP and STATUS_TEMP variables prior to interrupt routine. At the beginning of PUSH routine we need to check presently selected bank because W_TEMP and STATUS_TEMP are found in bank 0. For exchange of data between these registers, SWAPF instruction is used instead of MOVF because it does not affect the status of STATUS register bits. Example is a program assembler for following steps: 1. Testing the current bank 2. Storing W register regardless of the current bank 3. Storing STATUS register in bank 0. 4. Executing interrupt routine for interrupt processing (ISR) 5. Restores STATUS register 6. Restores W register If there are some more variables or registers that need to be stored, then they need to be kept after storing STATUS register (step 3), and brought back before STATUS register is restored (step 5). The same instance can be realized by using macros, thus getting a more legible program. Macros that are already defined can be used for writing new macros. Macros BANK1 and BANK0 which are explained in "Memory organization" chapter are used with macros 'push' and 'pop'. External interrupt on RB0/INT pin of microcontroller External interrupt on RB0/INT pin is triggered by rising signal edge (if bit INTEDG=1 in OPTION<6> register), or falling edge (if INTEDG=0). When correct signal appears on INT pin, INTF bit is set in INTCON register. INTF bit (INTCON<1>) must be reset in interrupt routine, so that interrupt wouldn't occur again while going back to the main program. This is an important part of the program which programmer must not forget, or program will constantly go into interrupt routine. Interrupt can be turned off by resetting INTE control bit (INTCON<4>). Interrupt during a TMR0 counter overflow Overflow of TMR0 counter (with FFh on 00h) will set T0IF (INTCON<2>) bit. This is quite a significant interrupt because many real problems can be solved using this interrupt. One of the examples is time measurement. If we know how much time counter needs in order to complete one cycle from 00h to FFh, then a number of interrupts multiplied by that amount of time will yield the total of elapsed time. In interrupt routine some variable would be incremented in RAM memory, value of that variable multiplied by the amount of time the counter needs to count through a whole cycle, would yield total elapsed time. Interrupt can bi turned on/off by setting/resetting T0IE (INTCON<5>) bit. Interrupt during a change on pins 4, 5, 6 and 7 of port B Change of input signal on PORTB <7:4> sets RBIF (INTCON<0>) bit. Four pins RB7, RB6, RB5 and RB4 of port B, can trigger an interrupt which occurs when status on them changes from logic one to logic zero, or vice versa. For pins to be sensitive to this change, they must be defined as input. If any one of them is defined as output, interrupt will not be generated at the change of status. If they are defined as input, their current state is compared to the old value which was stored at the last reading from port B. Interrupt can be turned on/off by setting/resetting RBIE bit in INTCON register. Interrupt during This interrupt is of practical nature only. Since writing to one EEPROM location takes about 10ms (which is a long time in the notion of a microcontroller), it doesn't pay off to a microcontroller to wait for writing to end. Thus interrupt mechanism is added which allows the microcontroller to continue executing the main program, while writing in EEPROM is being done in the background. When writing is completed, interrupt informs the microcontroller that writing has ended. EEIF bit, through which this informing is done, is found in EECON1 register. Occurrence of an interrupt can be disabled by resetting the EEIE bit in INTCON register. Interrupt initialization In order to use an interrupt mechanism of a microcontroller, some preparatory tasks need to be performed. These procedures are in short called "initialization". By initialization we define to what interrupts the microcontroller will respond, and which ones it will ignore. If we do not set the bit that allows a certain interrupt, program will not execute an interrupt subprogram. Through this we can obtain control over interrupt occurrence, which is very useful. The above example shows initialization of external interrupt on RB0 pin of a microcontroller. Where we see one being set, that means that interrupt is enabled. Occurrence of other interrupts is not allowed, and all interrupts together are disallowed until GIE bit is set to one. The following example shows a typical way of handling interrupts. PIC16F84 has only one location where the address of an interrupt subprogram is stored. This means that first we need to detect which interrupt is at hand (if more than one interrupt source is available), and then we can execute that part of a program which refers to that interrupt.
| Return from interrupt routine can be accomplished with instructions RETURN, RETLW and RETFIE. It is recommended that instruction RETFIE be used because that instruction is the only one which automatically sets the GIE bit which bit allows new interrupts to occur.