interrupts

22
INTERRUPTS Prepared by: Islam Samir

Upload: engislam90

Post on 06-May-2015

2.112 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: Interrupts

INTERRUPTS

Prepared by:

Islam Samir

Page 2: Interrupts
Page 3: Interrupts

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.

Page 4: Interrupts

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.

Page 5: Interrupts

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’ :

I. The MCU leaves the current program.

II. Executes the predefined code for this event and returns back to the original program.

Page 6: Interrupts

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.

Page 7: Interrupts

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.

Page 8: Interrupts
Page 9: Interrupts

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.

Page 10: Interrupts

INTERRUPTS When the event (interrupt source) happens, the following steps happen:

1) The corresponding interrupt flag (IF) will equal ‘1’.

2) 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

3) The return address is pushed into the stack and the PC is loaded with 0004h (the interrupt vector).

Page 11: Interrupts

INTERRUPTS4) 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.

Page 12: Interrupts
Page 13: Interrupts

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.

Page 14: Interrupts

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’.

Page 15: Interrupts

Fetch

Decode

Execute

Interrupt signal?

Stack <-- return address

PC <-- Interrupt vector

GIE=0

Interrupt cycle Instruction cycle

In S.W., make IF=0

T F

Page 16: Interrupts

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.

Page 17: Interrupts

INTERRUPT SOURCES IN THE PIC 16 MCU INT Pin Interrupt (external interrupt) TMR0 Overflow Interrupt PORTB Change Interrupt (pins RB7:RB4) Comparator Change Interrupt Parallel Slave Port Interrupt USART Interrupts Receive Interrupt Transmit Interrupt A/D Conversion Complete Interrupt Data EEPROM Write Complete Interrupt Timer1 Overflow Interrupt Timer2 Overflow Interrupt CCP Interrupt SSP Interrupt

Page 18: Interrupts

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).

Page 19: Interrupts

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.

Page 20: Interrupts

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.

Page 21: Interrupts

EXAMPLE

Make the led illuminates when the button is pressed, using RB0 interrupt.

Page 22: Interrupts

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