3. INTERRUPT
-
Definition:
An interrupt is an asynchronous signal indicate for an
event which needs processor’s attention immediately
regardless to the instruction it executes at this moment.
It’s like a
Doorbell.
4. WHY DO WE USE INTERRUPTS?
Example. (Software Polling)
Assume that the MCU is supposed to do two tasks.
-
In one of them, it needs to know if a certain I/O pin is „1‟ or not yet.
-
We can do so by checking if this pin is „1‟ or „0‟, which called
“Polling”.
-
By this method, the MCU is doing nothing but waiting for the pin to
be high, the 2nd task can‟t be done in this time.
Wasting MCU’s time.
-
If the MCU left this task to do the 2nd task. While executing it, the pin
became high. It won‟t know (the check won‟t be done) until it leaves
the 2nd task.
Slow response to important events.
5. INTERRUPT
Instead of checking each pin constantly (polling) and wasting the
processor’s time, the MCU waits for the interrupt signal that
indicates for the waited event.
When this interrupt signal equals ‘1’ :
The MCU leaves the current program.
Executes the predefined code for this event and returns back to
the original program.
I.
II.
6. COMPARISON
Interrupts
Software Polling
Save processor‟s time.
Waste processor‟s time.
Fast response from the CPU to the
event that has happened
The processor‟s response depends
on where it‟s in the program.
Can‟t be used with switches or
sensors. (because of Bouncing)
Used with switches or sensors
Ex. Timers.
Ex. Switches.
7. INTERRUPT DEFINITIONS
-
-
-
Interrupt Flag (IF):
A bit that is automatically set if the interrupt source (event) happens.
Global Interrupt Enable (GIE):
Enables (if set) all un-masked interrupts (interrupts with IE=1) or disables
(if cleared) all interrupts.
Interrupt Enable (IE):
If the GIE was ‘1’, this bit forces the CPU to respond to the interrupt signal
when IF=1 when the waited event happens.
8.
9. INTERRUPT DEFINITIONS
-
-
-
Interrupt service routine (ISR):
The code which the CPU jumps to when an interrupt happens.
Actually, the CPU will automatically jumps to the (interrupt vector)
0004h.
From there the code should be written to force the MCU to jump to
the ISR address.
10. INTERRUPTS
1)
2)
3)
When the event (interrupt source) happens, the following steps happen:
The corresponding interrupt flag (IF) will equal ‘1’.
If the interrupt enable (IE) was ‘1’, and the global interrupt enable
(GIE) was ‘1’ also, the GIE is cleared by hardware to avoid
responding any further interrupt
The return address is pushed into the stack and the PC is loaded with
0004h (the interrupt vector).
11. INTERRUPTS
4)
In the ISR, you can determine the source of interrupt by polling the
interrupt flag bits and do the required action for it.
5)
(Context Switching) You have to save key registers values before
interrupt has happened e.g. W register and STATUS register. This has
to be implemented in software
If you use high level language the compiler will do it for you.
-
4)
At the end of the ISR, you’ve to clear the IF in software then set the
GIE bit in the end of the ISR code.
12.
13. HOW ARE INTERRUPTS IMPLEMENTED IN
MICROPROCESSORS?
If interrupts are not used, the control unit goes through the
ordinary instruction cycle:
I.
Fetch instruction. ( Ins. register<--M[PC] ).
II.
Decode the instruction.
III. Execute it.
14. HOW ARE INTERRUPTS IMPLEMENTED IN
MICROPROCESSORS?
When interrupts are used in the microprocessor, the control
unit checks first if there is an interrupt signal or not.
If the interrupt signal was „1‟, it goes through interrupt cycle:
I.
Store the current address into the stack.
(the address of the next instruction which should have been
loaded if there is no interrupt).
II.
Load the PC with the interrupt vector address.
III. Make the GIE=„0‟.
16. HOW ARE INTERRUPTS IMPLEMENTED IN
MICROPROCESSORS?
The last instruction in the ISR should be:
RETFIE (return from interrupt).
This instruction makes the control unit loads the PC with the address
stored in the stack so that the original program can continue.
18. INTERRUPT SOURCES
-
-
Many peripherals use interrupts when finishing its job (ex.
USART, ADC, Timers …), or when there is a problem needs to
be configured (ex. EEPROM Write Complete ).
Here, we’ll talk about the simplest type of interrupt, and the
rest of interrupt sources should be handled in the same way.
Very important:
If you configure the MCU to respond to more than one
interrupt source at the same time, to determine which one has
happened, pull their flags in the ISR (check which one equals
1).
19. RB0/INT
The RB0/INT pin is a single external interrupt source.
When the waited edge rising or falling edge as selected)
happens on RB0 pin, the interrupt signal is generated.
It can be configured to react to signal raising edge or signal
falling edge.
20. RB0/INT
Steps:
1.
Configure the interrupt source to work as you want, here :
falling or rising edge.
2.
Set the IE bit of this interrupt source.
3.
Write the Interrupt Service Routine (ISR).
4.
Set the GIE bit.
22. ASSIGNMENT
-
Make an application using interrupts,
detects raising edge signals on RB0,
and detect any change happens is PORTB <7:4> and
respond to it as specified in the next table:
PORTB<7:4>
PORTC<7:4>
xxx1
xxx1
xx1x
xx1x
x1xx
x1xx
1xxx
1xxx