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

1

INTERRUPT
PROGRAMMING IN C
CHAPTER 4
2

Interrupt vs Polling

• A single microcontroller can serve several

devices.
• There are 2 methods for a device to receive

service from microcontroller:


• Interrupt

• Polling
3

Polling
• The state of continuous monitoring is known as polling.
• The microcontroller keeps checking the status of the
devices; and while doing so, it does no other operation and
consumes all its processing time for monitoring.
• When the condition is met, it performs task for the device.
• After that, it moves on to monitor the next device until
every devices is serviced.
• The microcontroller checks all devices in a round robin
fashion.
4

Interrupt
• Interrupt is an event that temporarily suspends the main

program, passes the control to a special code section,


executes the event-related function and resumes the main
program flow where it had left off.
• Interrupt is the signal sent to the microcontroller to mark

the event that requires immediate attention.


5

How Interrupt Works (1)


• Whenever any device needs microcontroller service, the device

notifies the microcontroller by sending it an interrupt signal.


• Upon receiving an interrupt signal, the microcontroller stop

whatever it is doing (main program) and serves the device.


• Once microcontroller has finished executing interrupt code, it will

resume the main program.


• The program which is associated with the interrupt is called the

Interrupt Service Routine (ISR) or interrupt handler.


• ISR tells the microcontroller what to do when the interrupt occurs.
6

How Interrupt Works (2)


7

How Interrupt Works (3)


8

Polling vs Interrupt (1)


• In polling, CPU keeps on checking I/O
devices at regular interval whether it
needs CPU service.
• In interrupt, the I/O device interrupts
the CPU and tell CPU that it need
CPU service.
9

Polling vs Interrupt (2)


• The polling method is like a salesman.
• The salesman goes from door to door while offering his
product or service to customer.
• Similarly, the controller keeps monitoring the flags or
signals one by one for all devices and provides service to
whichever devices that needs its service.
• An interrupt is like a shopkeeper.
• If a person needs a service or product, he/she goes to the
shopkeeper to get it.
• For interrupts, when the flags or signals are received, the
device notify the controller that they need to be serviced.
10

Polling vs Interrupt (3)


11

Polling vs Interrupt (4)


12

Sources of interrupt
• Reset, Brown-Out Reset, Watch- • Streaming Parallel Port Read/Write
dog Reset, Power On Reset Interrupt
• External Interrupt 0 (INT0) • EUSART Receive Interrupt
• External Interrupt 1 (INT1) • EUSART Transmit Interrupt
• External Interrupt 2 (INT2) • Master Synchronous Serial Port

• Timer 0 Interrupt
Interrupt
• CCP1 Interrupt (Capture, Compare,
• Timer 1 Interrupt
PWM)
• Timer 2 Interrupt
• Oscillator Fail Interrupt
• Timer 3 Interrupt
• USB Interrupt
• ADC Interrupt • Data EEPROM/Flash Write
• Analog Comparator Interrupt Operation Interrupt
• RB Port change Enable Interrupt • Bus Collision Interrupt
• High/Low-Voltage Detect Interrupt
• CCP2 Interrupt
13

Registers For Interrupt Operation


• RCON (Reset Control)

• INTCON, INTCON2, INTCON3 (Interrupt Control)

• PIR1, PIR2, PIR3, PIR4, PIR5 (Peripheral Interrupt

Request (Flag))
• PIE1, PIE2, PIE3, PIE4, PIE5 (Peripheral Interrupt

Enable)
• IPR1, IPR2, IPR3, IPR4, IPR5 (Peripheral Interrupt

Priority)
14

Interrupt Bits
• Every interrupt source (except INT0) has three bits to

control its operation.


• These bits are:

1. A flag bit to indicate whether an interrupt has occurred.

This bit has a name ending in . . .IF


2. An interrupt enable bit to enable or disable the interrupt

source. This bit has the name ending in . . .IE


3. A priority bit to select high or low priority. This bit has a

name ending in . . .IP


15

Enabling And Disabling An Interrupt


• Upon reset, all interrupts are disabled (masked)

• The interrupts must be enabled (unmasked) by software

in order for the microcontroller to respond to them.


• The D7 bit of INTCON (Interrupt Control) register is

responsible for enabling and disabling the interrupt


globally.

GIE = 0 //Disable all interrupt


GIE = 1 //Enable all interrupt
16

PIC18 Interrupt Logic


Our focus

Note 1: The RBIF interrupt also requires the individual pin IOCB enables.
17
18
19

Interrupt C Code
• For XC8 compiler, use the following code to perform
interrupt:
……………… IF = 0; //Clear xx Interrupt Flag
……………… IE = 1; //Enable xx Interrupt Enable bit
INTCONbits.GIE = 1; //Enable all interrupt

while(1) {
} //Main program here
}

static void interrupt isr (void) // Interrupt Service Routine


{
if (……………………IF = = 1) //Check flag bit
{
//Perform interrupt task here
…………..IF = 0; //Clear flag bit
}
}
20

Default: IPEN = 0
In this course,
we will not set interrupt priority.
21

Enabling Interrupt Source


• The interrupt enable bit of the interrupt source must be
enabled (…IE = 1).
• The interrupt flag of the interrupt source must be cleared.
(…IF = 0).
• The peripheral interrupt enable/disable bit of PIEx of
INTCONx must be set to 1 if the interrupt source is
peripheral.
• The global interrupt enable/disable bit GIE of INTCON
must be set to 1 (GIE = 1).
22

External Hardware Interrupts (1)


Interrupt Flag Bit Register Enable Bit Register
INT0 (RB0) INT0IF INTCON INT0IE INTCON
INT1 (RB1) INT1IF INTCON3 INT1IE INTCON3
INT2 (RB2) INT2IF INTCON3 INT2IE INTCON3
23

External Hardware Interrupts (2)


• PIC18 has 3 external hardware interrupt: RB0/INT0, RB1/INT1 and

RB2/INT2.
• These interrupt is triggered whenever there is a change of state on the

these pins (RB0/RB1/RB2).


• The change of state means the pin went from high to low or low to high.

• When these pins are activated, PIC18 gets interrupted in whatever it is

doing and will execute interrupt task.


• Bit INTxIE will will enable or disable the interrupt.

• Bit INTxIF will show whether interrupt has occurred or not.

• INTxIF must be cleared by software in the Interrupt Service Routine

before re-enabling the interrupt.


24

External Hardware Interrupts (3)


• INTEDGx bit in the INTCON2 register will determine the

interrupt is triggered by rising or falling edge.


• INT1IP and INT2IP of the INTCON3 register will
determine interrupt priority for INT1 and INT2.
• There is no priority bit associated with INT0.

• It is always a high priority interrupt source.


25

External Hardware Interrupts (4)


Flag Bit Function
INTxIE = 0 Enable external hardware interrupt
INTxIE = 1 Disable external hardware interrupt
INTxIF = 0 External hardware interrupt did not occurred
INTxIF = 1 External hardware interrupt occurred
INTEDGx = 0 Interrupt on falling (negative) edge
INTEDGx = 1 Interrupt on rising (positive) edge
26
27
28

//INT0 source code example


#include <xc.h>
void main (void)
{
TRISBbits.TRISB0 = 1; //INT0/RB0 as input
TRISAbits.TRISA0 = 0; //RA0 as output
INTCONbits.INT0IF = 0; //Clear INT0 External Interrupt Flag
INTCONbits.INT0IE = 1; //Enable INT0 External Interrupt Enable bit
INTCON2bits.INTEDG0 = 0; //Interrupt on falling edge
INTCONbits.GIE = 1; //Enable all interrupt

while(1) { //Do nothing


}}

static void interrupt isr (void) //ISR


{
if (INTCONbits.INT0IF == 1) //INT0 is set?
{
LATAbits.LATA0=~LATAbits.LATA0; //Toggle RA0
INTCONbits.INT0IF = 0; //Clear INT0 bit
}
}
29

Timer0 Interrupts (1)


• In 8-bit mode, an overflow in theTMR0 register (FFh to

00h) will set bit TMR0IF (TMR0IF = 1).


• In 16-bit mode, an overflow in the TMR0H:TMR0L register

pair (FFFFh to 0000h) will set TMR0IF (TMR0IF = 1).


• Bit TMR0IE will will enable or disable the interrupt.

• Bit TMR0IF will show whether interrupt has occurred or

not.
• Bit TMR0IP will determine interrupt priority bit.
30

Timer0 Interrupts (2)


Flag Bit Function
TMR0IE = 0 Enable Timer0 interrupt
TMR0IE = 1 Disable external hardware interrupt
TMR0IF = 0 Timer0 interrupt did not occurred
TMR0IF = 1 Timer0 interrupt occurred
TMR0IP= 0 High priority interrupt
TMR0IP = 1 Low priority interrupt
31
32
//Timer0 time delay using interrupt
#include <xc.h>
void T0Delay (void);

void main (void) {


TRISC0 = 0; //RC0 as output
INTCONbits.TMR0IF = 0; //Clear TMR0 Overflow Interrupt Flag bit
INTCONbits.TMR0IE = 1; //Enable TMR0 Overflow Interrupt Enable bit
INTCONbits.GIE = 1; //Enable all interrupt

while(1) {
LATC0 ^ = 1; //Toggle RC0
T0Delay ( ); } }

void T0Delay ( ) {
T0CON = 0xC7; //8 bit timer
TMR0L = 0xFF; }

static void interrupt isr (void) //ISR


{
if (INTCONbits.TMR0IF = = 1) //TMR0 is overflow?
{
T0CONbits.TMR0ON = 0; //Off Timer0
INTCONbits.TMR0IF = 0; //Clear TMR0 bit
TMR0L = 0xFF; } }
33

//Timer0 counter using interrupt


#include <xc.h>

void main (void) {


TRISAbits.TRISA4 = 1; //RA4/TOCKI as input
TRISD=0;
T0CON = 0x68; //Counter0, 8 bit mode, no prescale,
TMR0L = 0; //Count from 0 to 255
GIE = 1;
TMR0IE = 1;
TMR0IF = 0;

while (1)
{
T0CONbits.TMR0ON=1; //Turn ON TMR0
PORTD = TMR0L; } }

static void interrupt ISR(void) //ISR


{
if(TMR0IF == 1) {
T0CONbits.TMR0ON=0;
TMR0L = 0;
TMR0IF = 0;
}}

You might also like