Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

Sinhgad College of Engineering

Dept. of Information Technology


Class - SE

Processor Architecture (214451)


Unit-3: PIC Microcontroller Interrupts

Faculty – Prof. Madhukar V. Nimbalkar


mvnimbalkar.scoe@sinhgad.edu
9890586765
Introduction
 Interrupts can be defined as asynchronous request for
service by peripherals to processor.
 Interrupts are mechanisms which enable instant response
to events such as counter overflow, pin change, data
received, etc.
 In normal mode, microcontroller executes the main
program as long as there are no occurrences that would
cause an interrupt.
 Upon interrupt, microcontroller stops the execution of
main program and commences the special part of the
program(ISR) which will analyze and handle the interrupt.

The PIC uCs


PIC18 interrupts
 PIC can serve multiple devices using mechanisms of
 Polling
 PIC continuously monitors the status of each device
 Each device get the attention of the CPU as the same
level of priority
 Wastes u-Controllers time by polling devices that do not
need service.
 Interrupt
 Devices get the attention of the CPU only when it needs
a service
 Can service many devices with different level of
priorities

The PIC uCs


Interrupt service routine (ISR)

 When an interrupt is invoked


the uC runs the Interrupt
Service Routine(ISR)
 Interrupt vector table holds
the address of ISRs
 Power-on Reset 0000h
 High priority interrupt
0008h
 Low priority interrupt
0018h

The PIC uCs


Steps in executing an interrupt
 Upon activation of interrupt the microcontroller
 Finishes executing the current instruction
 Pushes the PC of next instruction in the stack
 Jumps to the interrupt vector table to get the address
of ISR and jumps to it
 Begin executing the ISR instructions to the last
instruction of ISR (RETFIE)
 Executes RETFIE
 Pops the PC from the stack
 Starts to execute from the address of that PC

The PIC uCs


Sources of interrupts in PIC18
 External hardware interrupts  USART Interrupts
Pin RB0(INT0),RB1(INT1),  Receive Interrupt
RB2(INT2)  Transmit Interrupt
 PORTB change (RB4- RB7)  A/D Conversion Complete
 Peripheral Interrupts Interrupt
 Timers Overflow Interrupt  LCD Interrupt.
 Timer0 , Timer1 ,Timer2  Data EEPROM Write
 Comparator Change Complete Interrupt
Interrupt  SSP Interrupt
 Parallel Slave Port Interrupt  CAN Interrupt
 CCP Interrupt  Bus Collision Interrupt
 ETC…….

All are Maskable & Vectored Interrupts


The PIC uCs
Enabling and disabling an interrupt
 When the PIC is powered on (or resets)
 All interrupts are masked (disabled)
 The default ISR address is 0008h
 No interrupt priorities for interrupts

The PIC uCs


Enabling and disabling an interrupt
In general, interrupt sources have three bits to control their
operation. They are:
 Flag bit
 to indicate that an interrupt event occurred
 Enable bit
 that allows program execution to branch to the
interrupt vector address when the Flag bit is set
 Priority bit
 to select high priority or low priority

The PIC uCs


Steps in enabling an interrupt
 Set the GIE bit from INTCON register
 Set the IE bit for that interrupt
 If the interrupt is one of the peripheral (timers 1,2 , serial, etc. )
set PEIE bit from INTCON register

The PIC uCs


Assigning priorities to an interrupt
 Set the IPEN bit, bit7 from RCON register
 Use P - priority bit for that interrupt to assign either HIGH or LOW
priority
 P = 1, HIGH priority
 P = 0, LOW priority
 To enable priority interrupts (when IPEN = 1), set GIE / GIEH bit of
INTCON reg. = 1 for HIGH priority interrupts &
set PEIE / GIEL bit of INTCON reg. = 0 for LOW priority interrupts
For INT0 interrupt – no priority bit because it is having default high
priority

The PIC uCs


PIC18 interrupts
 The PIC18FXX8 devices have multiple interrupt sources and an
interrupt priority feature that allows each interrupt source to
be assigned a high priority level or a low priority level.
 The high priority interrupt vector is at 000008h and the low
priority interrupt vector is at 000018h.
 High priority interrupt events will override any low priority
interrupts that may be in progress.
 There are 13 registers that are used to control interrupt
operation. These registers are:
RCON
INTCON, INTCON2, INTCON3
PIR1, PIR2, PIR3
PIE1, PIE2, PIE3
IPR1, IPR2, IPR3
The PIC uCs
RCON,INTCON, INTCON2, INTCON3

The PIC uCs


RCON – Reset Control Register

The PIC uCs


INTCON – Interrupt Control Register

The PIC uCs


INTCON2 – Interrupt Control Register2

The PIC uCs


INTCON3 – Interrupt Control Register3

The PIC uCs


PIR1, PIE1, IPR1

Used to programme TMR1, TMR2, CCP, SSP,USART- TX,


USART – RC, ADC, PSP Peripheral Interrupts

The PIC uCs


PIR1 – Peripheral Interrupt Request Register1

The PIC uCs


PIE1 – Peripheral Interrupt Enable Register1

The PIC uCs


IPR1 – Peripheral Interrupt Priority Register1

The PIC uCs


PIR2, PIE2, IPR2

Used to programme ECCP, TMR3, LVD, BCL, EEPROM-


Write, CM Peripheral Interrupts

The PIC uCs


PIR2 – Peripheral Interrupt Request Register2

The PIC uCs


PIE2 – Peripheral Interrupt Enable Register2

The PIC uCs


IPR2 – Peripheral Interrupt Priority Register2

The PIC uCs


PIR3, PIE3, IPR3

Used to programme CAN Invalid message, CAN Bus


Wakeup, CAN Error, Transmit Buffer 0,1,2, Receive
Buffer 0,1 Peripheral Interrupts
The PIC uCs
PIR3 – Peripheral Interrupt Request Register3

The PIC uCs


PIE3 – Peripheral Interrupt Enable Register3

The PIC uCs


IPR3 – Peripheral Interrupt Priority Register3

The PIC uCs


The PIC uCs
The PIC uCs
PIC18F Interrupt Logic for TIMER Interrupts
without priorities

The PIC uCs


PIC18F Interrupt Logic – Detailed Diagram

The PIC uCs


The PIC uCs
The PIC uCs
The PIC uCs
The PIC uCs
Program organization in Assembly

The PIC uCs


Embedded C - #pragma section directive
Assembler allows placing code/data at a specific ROM address using ORG
directive.
To do the same in Embedded C , use #pragma section directive, where
Section is portion of an application (code/data) that can be assigned specific
memory address location. In case of on-chip ROM program memory, section
can be either code or romdata
#pragma code– used for program with executable instructions &
#pragma romdata – used for fixed data such as strings and look-up tables.

Ex. }
#include <p18f458.h> #pragma code msdelay = 0X30
#pragma code main = 0X50 Void msdelay
Void msdelay {
Void main (void)
{ }

The PIC uCs


The PIC uCs
The PIC uCs
The PIC uCs
The PIC uCs
The PIC uCs
The PIC uCs
Programming timer using interrupt
Write down a program to generate a square wave
on RB5 of frequency 1KHz by using Timer 0
interrupt method ,while data is being transferred
from PORTC to PORTD.

RB5 Square Wave = 1KHz


By Interrupt Method

P18F452

PORTC PORTD

The PIC uCs


Program
# include <p18f452.h> #pragma interrupt HighISR
#define mybit PORTBbits.RB5 void HighISR(void)
void T0_ISR(void) {
#pragma code HighVector=0x08 if (INTCONbits.TMR0IF==1)
void HighVector(void) T0_ISR();
{ }
_asm void T0_ISR (void)
GOTO HighISR {
_endasm mybit = ~mybit;
} TMR0H = 0xFE;
TMR0L=0x0C;
INTCONbits.TMR0IF=0;
}

The PIC uCs


void main (void)
{
TRISBbits.TRISB5 = 0; // Make RB5 as output pin
TRISC=0xFF; // Make Port C as Input
TRISD =0x00; // Make Port D as output
T0CON = 0x08; // Timer 0, 16 bit mode , no pre-scaler
TMR0H=0xFE; // for freq. = 1 KHz , Delay = 0.5 ms, count = FE0C
TMR0L=0x0C;
INTCONbits.TMR0IF=0; // Timer overflow bit
INTCONbits.GIE=1; // enable all interrupts globally
INTCONbits.PEIE=1 // enable all peripheral interrupt
INTCONbits.TMR0IE=1; // enable Timer 0 Interrupt
T0CONbits.TMRON=1 ; // start timer 0
while (1) //keep looping until interrupt comes
{
PORTD= PORTC
}
}

The PIC uCs


Programming timer using interrupt

Write an Embedded C program for Timer programming ISR based


buzzer on/off.

RB5
Buzzer

P18F452

The PIC uCs


Program
# include <p18f452.h> #pragma interrupt HighISR
#define mybit PORTBbits.RB5 void HighISR(void)
void T0_ISR(void) {
#pragma code HighVector=0x08 if (INTCONbits.TMR0IF==1)
void HighVector(void) T0_ISR();
{ }
_asm void T0_ISR (void)
GOTO HighISR {
_endasm mybit = ~mybit;
} TMR0H = 0xFE;
TMR0L=0x0C;
INTCONbits.TMR0IF=0;
}

The PIC uCs


void main (void)
{
TRISBbits.TRISB5 = 0; // Make RB5 as output pin
T0CON = 0x08; // Timer 0, 16 bit mode , no pre-scaler
TMR0H=0xFE; // Delay = 0.5 ms, count = FE0C
TMR0L=0x0C;
INTCONbits.TMR0IF=0; // Timer overflow bit
INTCONbits.GIE=1; // enable all interrupts globally
INTCONbits.PEIE=1 // enable all peripheral interrupt
INTCONbits.TMR0IE=1; // enable Timer 0 Interrupt
T0CONbits.TMRON=1 ; // start timer 0
while (1) //keep looping until interrupt comes
}

The PIC uCs


Programming external hardware interrupt
Connect a switch to INT0 (RB0) and an LED to PIN RB7.
Every time INT0 is activated, it toggles the LED while at the
same time data is transferred from PORTC to PORTD

NOTE: By default or on power on reset, INT0,INT1 and INT2


are positive edge triggered.

SW(INT0/RB0) LED (RB7)

Port C Port D

The PIC uCs


Program
#include <p18f452.h> #pragma code
#define mybit PORTBbits.RB7 My_HiPrio_Int=0x0008
void chk_isr(void); void My_HiPrio_Int (void)
void INT0_ISR(void); {
_asm
# pragma interrupt chk_isr GOTO chk_isr
void chk_isr(void) _endasm
{ }
if (INTCONbits.INT0IF==1) Void INT0_ISR(void)
INT0_ISR(); {
} mybit=~mybit;
INTCONbits.INT0IF=0;
}

The PIC uCs


void main(void)
{
TRISBbits.TRISB7=0;
TRISBbits.TRISB0=1;

TRISC =0xFF;
TRISD=0;

INTCONbits.INT0IF=0; // clear INT01


INTCONbits.INT0IE=1; //Enable INT0 interrupt
INTCONbits.GIE=1; //enable all interrupts
While(1) //keep looping until interrupt comes
{
PORTD=PORTC;
}
}
The PIC uCs
Programming external hardware interrupt
Write an Embedded C program for External interrupt input
switch press, output at relay.

NOTE: By default or on power on reset, INT0,INT1 and INT2


are positive edge triggered.

SW(INT0/RB0) RELAY (RB7)

The PIC uCs


Program
#include <p18f452.h> #pragma code
#define mybit PORTBbits.RB7 My_HiPrio_Int=0x0008
void chk_isr(void); void My_HiPrio_Int (void)
void INT0_ISR(void); {
_asm
# pragma interrupt chk_isr GOTO chk_isr
void chk_isr(void) _endasm
{ }
if (INTCONbits.INT0IF==1) Void INT0_ISR(void)
INT0_ISR(); {
} mybit=~mybit;
INTCONbits.INT0IF=0;
}

The PIC uCs


void main(void)
{
TRISBbits.TRISB7=0;
TRISBbits.TRISB0=1;

INTCONbits.INT0IF=0; // clear TF1


INTCONbits.INT0IE=1; //Enable Timer0 interrupt
INTCONbits.GIE=1; //enable all interrupts
While(1) //keep looping until interrupt comes

The PIC uCs


PORTB change interrupt
Connect SW1 and SW2 to pins RB4 and RB5
respectively. Upon activation of SW1 and
SW2 will result in changing the state of LED1
and LED2 respectively.

SW1(RB4) LED1(RC4)

SW2(RB5) LED2(RC5)

The PIC uCs


Program
#include <p18f458.h>
#define LED1 PORTCbits.RC4 #pragma code
#define LED2 PORTCbits.RC5 My_HiPrio_Int=0x0008
void My_HiPrio_Int (void)
void chk_isr(void); {
void RBINT_ISR(void); _asm
GOTO chk_isr
# pragma interrupt chk_isr _endasm
void chk_isr(void) }
{
if (INTCONbits.RBIF==1) Void RBINT_ISR(void)
RBINT_ISR(); {
} LED1=PORTBbits.RB4;
LED2=PORTBbits.RB5;
INTCONbits.RBIF=0;
}

The PIC uCs


void main(void)
{
TRISCbits.TRISC4=0;
TRISCbits.TRISC5=0;
TRISBbits.TRISB4=1;
TRISBbits.TRISB5=1;

INTCONbits.RBIF=0; // clear RBIF


INTCONbits.RBIE=1; //Enable RB interrupt
INTCONbits.GIE=1; //enable all interrupts
While(1); //keep looping until interrupt comes
}

The PIC uCs

You might also like