Sunteți pe pagina 1din 12

Introduction to PIC Microcontroller Programming & Interfacing

August 19-21, 2010


Franz Duran

20-Aug-2010

Paranz

Interrupts
DAY 2 Morning Session August 20, 2010
20-Aug-2010 Paranz 2

OVERVIEW
Interrupt Basics
Interrupt sources Interrupt service routine (ISR)

Interrupts in PIC16F84A, PIC16F877A


Associated SFRs RB0 Interrupt PORTB Interrupt on-change
20-Aug-2010 Paranz 3

BASICS OF INTERRUPTS
Very important microcontroller feature Allows internal & external events to trigger an interrupt
notify the CPU

CPU temporarily halt program execution and respond to the interrupt


Execute an interrupt service routine
20-Aug-2010 Paranz 4

BASICS OF INTERRUPTS
void main() { InitializeSystem(); while(1) { Task1(); Task2(); Task3(); Task4(); Task5(); } } an interrupt is triggered - CPU saves context (Program Counter, STATUS, W)
20-Aug-2010 Paranz 5

CPU executes the interrupt service routine

void interrupt isr(void) { //Execute relevant instructions. //Clear interrupt flag. }

CPU exits from interrupt service routine; previous context is restored, continue program execution.

INTERRUPTS: PIC16F84A
4 Interrupt sources
RB0 interrupt PORTB-on-change interrupt TMR0 interrupt Data EEPROM Write Complete Interrupt

SFR: INTCON
20-Aug-2010 Paranz 6

INTERRUPTS: PIC16F877A
15 interrupt sources
RB0 interrupt PORTB-on-change interrupt TMR0 interrupt Data EEPROM Write Complete Interrupt UART TX, UART RX ADC, TMR1, TMR2 SSP, CCP, etc..

SFRs: INTCON, PIR1, PIR2, PIE1, PIE2


20-Aug-2010 Paranz 7

INTERRUPTS: INTCON

PIC16F84A

PIC16F877A
20-Aug-2010 Paranz 8

INTERRUPTS: PIC16F84A

Interrupt Logic

20-Aug-2010

Paranz

INTERRUPTS: PIC16F877A

Interrupt Logic

20-Aug-2010

Paranz

10

RB0/INT INTERRUPT
RB0 pin is an interrupt source Generate an interrupt on the ff. event:
falling-edge signal transition
+5v 0v 0v +5v

rising-edge signal transition

20-Aug-2010

Paranz

11

RB0/INT INTERRUPT

1 interrupt on rising edge 0 interrupt on falling edge

Set to enable interrupt


20-Aug-2010

Set to enable RB0/INT interrupt


Paranz

Set automatically by hardware on interrupt; cleared via software


12

RB0/INT INTERRUPT

0 1 1

ISR is executed

20-Aug-2010

Paranz

13

RB0/INT INTERRUPT
#include <pic.h> __CONFIG(HS & WDTDIS & LVPDIS & PWRTDIS & void main() {

Ex am ple UNPROTECT);

#1

//Button at RB0. INTEDG = 0; //Interrupt on falling edge. INTF = 0; //Clear RB0 interrupt flag. INTE = 1; //Enable RB0 interrupt. GIE = 1; //Enable interrupt. TRISC1 = 0; //LED at RC1 RC1 = 0; while(1); }
20-Aug-2010 Paranz 14

//Do nothing but wait for RB0 interrupt, // to occur then execute the ISR.

RB0/INT INTERRUPT

//Interrupt Service Routine void interrupt isr_RB0(void) { RC1 ^= 1; //Toggle LED. INTF = 0; //Clear RB0 interrupt flag. }

Ex am ple

#1

20-Aug-2010

Paranz

15

RB0/INT INTERRUPT
EXERCISE:
Change to INTEDG=1
rising edge Observe behaviour

20-Aug-2010

Paranz

16

RB0/INT INTERRUPT
EXERCISE:
Modify Example#1
Toggle another LED in program loop
LED1 toggled when RB0/INT triggers LED2 blink in program loop toggle LED2 every 500 msec use delay.c and delay.h

Ex am ple

#2

20-Aug-2010

Paranz

17

BASICS OF INTERRUPTS
Other interrupt INFOs:
GIE is automatically cleared during ISR execution; GIE is set on ISR exit
prevent the ISR from being interrupted

Interrupt latency
time between interrupt & execution of ISR
CPU complete current instruction, do context save

3-4 instruction cycles for external interrupts 2-3 instruction cycles for internal interrupts
20-Aug-2010 Paranz 18

BASICS OF INTERRUPTS
other INFOs: (cont)
ISR should be as short as possible
set event flags inside the ISR & do associated tasks in program loop

20-Aug-2010

Paranz

19

RB0/INT INTERRUPT
#include <pic.h> #define #define #define #define LED RC1 TRUE 1 FALSE TOGGLE

__CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);

Ex am ple

#3

0 1 //event flag variable

volatile bit rb0_int_flag = FALSE; void interrupt ISR(void) { rb0_int_flag = TRUE; INTF = 0; }
20-Aug-2010

//clear RB0 interrupt flag

Paranz

20

RB0/INT INTERRUPT
void main() { InitSystem(); while(1) { if(rb0_int_flag == TRUE) { rb0_int_flag = FALSE; LED ^= 1; } } }
20-Aug-2010 Paranz

Ex am ple

#3

//Poll flag. //Clear flag. //Do task.

21

BASICS OF INTERRUPTS
other INFOs: (cont)
ISR should be as short as possible
set event flags inside the ISR & do associated task in program loop

Global variables modified inside the ISR AND program loop should be declared as volatile

20-Aug-2010

Paranz

22

BASICS OF INTERRUPTS
volatile keyword
Variable type qualifier inform the compiler that the value of a variable may be modified by the hardware in ways that are not explicitly specified during normal program operation. Compiler should not optimize the variable in main (or in other functions)
20-Aug-2010 Paranz 23

RB0/INT INTERRUPT
#include <pic.h> void main() { INTEDG = 0; INTF = 0; INTE = 0; GIE = 0; TRISC1 = 0; RC1 = 0; //Falling edge //Clear RB0 interrupt flag. //Disable RB0 interrupt. //Disable all interrupt. //LED

Ex am ple

#4

while(1) { if(INTF==1) { RC1 ^= 1; INTF = 0; } } }


20-Aug-2010

//poll INTF if it is set // then toggle LED. //Clear RB0 interrupt flag.

Paranz

24

PORTB INTERRUPT ON CHANGE PORTB<7:4> pins can be configured as an interrupt source Generate an interrupt when the logic signal level on any pin changes
20-Aug-2010 Paranz 25

PORTB INTERRUPT ON CHANGE Generate an interrupt on both rising edge and falling edge Operation:
old value of PORTB<7:4> during last read is stored if current value not equal to old value,
mismatch condition RBIF=1
20-Aug-2010 Paranz 26

PORTB INTERRUPT ON CHANGE

1 0 1

ISR is executed

20-Aug-2010

Paranz

27

PORTB INTERRUPT ON CHANGE To clear PORTB interrupt:


read value of PORTB<7:4> AND clear RBIF

20-Aug-2010

Paranz

28

PORTB INTERRUPT ON CHANGE


Set to enable PORTB interrupt

Set to enable interrupt

Set by hardware automatically on interrupt; cleared via software

20-Aug-2010

Paranz

29

PORTB INTERRUPT ON CHANGE


#include <pic.h> void interrupt isr(void) { unsigned char portb_temp; RC1 ^= 1; portb_temp = PORTB & 0xF0; RBIF = 0; } void main() { TRISB = 0b00010000; RBIF = 0; RBIE = 1; GIE = 1; TRISC1 = 0; RC1 = 0; while(1); } 20-Aug-2010 Paranz

Ex am ple

#5

//Toggle LED //Read to clear mismatch //Clear interrupt flag

The ISR is executed twice during a button press

//RB4 is input, rest are output & unused. //Clear interrupt flag. //Enable interrupt. //Enable all (global) interrupt. //LED

//do nothing, simply wait for ISR 30

10

PORTB INTERRUPT ON CHANGE


If using multiple PORTB pins, use switch() and masks Example: RB4 and RB5 are interrupt source void interrupt isr(void) { unsigned char portb_temp; portb_temp = PORTB & 0b00110000; //read PORTB switch(portb_temp) { case 0b00100000: //if Pushbutton1 is pressed, LED1 ^= TOGGLE; //toggle LED1 break; case 0b00010000: //if Pushbutton2 is pressed, LED2 ^= TOGGLE; //toggle LED2 break; } RBIF = 0; //clear interrupt flag }
20-Aug-2010 Paranz 31

Multiple Interrupts
PIC16F has several interrupts But only one ISR If using multiple interrupts:
Poll the interrupt enable bit and interrupt flag bit

Example: RB0 int & PORTB Int.


Poll INTE and INTF Poll RBIE and RBIF
20-Aug-2010 Paranz 32

Multiple Interrupts
void interrupt isr(void) { unsigned char portb_temp; //Check if PORTB Int. triggered the interrupt if(RBIE && RBIF) { //codes here RBIF = 0; //clear interrupt flag } //Check if RB0 triggered the interrupt if(INTE && INTF) { //codes here INTF = 0; //clear interrupt flag }
20-Aug-2010 } Paranz 33

11

Multiple Interrupts
EXERCISE: using multiple interrupts
Toggle LED1 with RB0 interrupt Toggle LED2 and LED3 with PORTB interrupt
Toggle LED2 with RB4 Toggle LED3 with RB5

20-Aug-2010

Paranz

34

BASICS OF INTERRUPTS
other INFOs: (cont)
ISR should be as short as possible Global variables modified inside the ISR AND program loop should be declared as volatile interrupt flags are set even if interrupt is disabled
INTF is set by hardware, even if INTE=0 & GIE=0
20-Aug-2010 Paranz 35

INTERRUPTS: Summary
Interrupts expands MCU functionality Main benefit:
speed of response to internal/external events
max. 4 TOSC interrupt latency

reduced software overhead


program efficiency greatly increased

facilitates multitasking
20-Aug-2010 Paranz 36

12

S-ar putea să vă placă și