Documente Academic
Documente Profesional
Documente Cultură
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)
BASICS OF INTERRUPTS
Very important microcontroller feature Allows internal & external events to trigger an interrupt
notify the CPU
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 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..
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
20-Aug-2010
Paranz
11
RB0/INT INTERRUPT
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
Ex am ple
#3
volatile bit rb0_int_flag = FALSE; void interrupt ISR(void) { rb0_int_flag = TRUE; INTF = 0; }
20-Aug-2010
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
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
//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
1 0 1
ISR is executed
20-Aug-2010
Paranz
27
20-Aug-2010
Paranz
28
20-Aug-2010
Paranz
29
Ex am ple
#5
//RB4 is input, rest are output & unused. //Clear interrupt flag. //Enable interrupt. //Enable all (global) interrupt. //LED
10
Multiple Interrupts
PIC16F has several interrupts But only one ISR If using multiple interrupts:
Poll the interrupt enable bit and interrupt flag bit
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
facilitates multitasking
20-Aug-2010 Paranz 36
12