0% found this document useful (0 votes)
33 views30 pages

Chapter 4 e

The document discusses interrupts in PIC microcontrollers. It covers the difference between interrupt and polling methods, hardware and software interrupts, interrupt registers, enabling and disabling interrupts, and examples of external hardware and timer interrupts in C code.

Uploaded by

Nananq Lalalala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views30 pages

Chapter 4 e

The document discusses interrupts in PIC microcontrollers. It covers the difference between interrupt and polling methods, hardware and software interrupts, interrupt registers, enabling and disabling interrupts, and examples of external hardware and timer interrupts in C code.

Uploaded by

Nananq Lalalala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

CHAPTER 4:

INTERRUPT PROGRAMMING IN C
At the end of the chapter, student should be able to;
-Know about Interrupts in the PIC and apply C
program to interface PIC with external devices using
interrupts.
Interrupt vs Polling
• A single microcontroller can serve several
devices.
• There are 2 methods for a device to
receive service from microcontroller:
– Interrupt
– Polling
Polling

• In polling, the microcontroller continuously


monitors the status of a given device and
executes the task the device needed when
the status will satisfy the condition.
• After that, it moves on to monitor the next
device until each one is served.
Interrupt

• In the interrupt method, the device that


needs the microcontroller’s service
notifies it by sending an interrupt signal.
• The microcontroller stops whatever task it
is doing whenever it receives an interrupt
signal and serves that device.
Interrupt vs Polling
Interrupt vs Polling
Hardware and Software Interrupts

• PIC Microcontroller consists of both Hardware and


Software Interrupts.
• If the interrupts are generated by external hardware at
certain pins of microcontroller, or by inbuilt devices like
timer, they are called Hardware Interrupts.
• While Software Interrupts are generated by a piece of
code in the program.
• Also known as External and Internal Interrupts.
Sources of interrupt
• Reset, Brown-Out Reset, Watch- • Streaming Parallel Port
dog Reset, Power On Reset Read/Write 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
• Timer 1 Interrupt • CCP1 Interrupt (Capture,
• Timer 2 Interrupt Compare, PWM)
• Timer 3 Interrupt • Oscillator Fail Interrupt
• ADC Interrupt • USB Interrupt
• Analog Comparator Interrupt • Data EEPROM/Flash Write
Operation Interrupt
• RB Port change Enable • Bus Collision Interrupt
Interrupt
• High/Low-Voltage Detect
Interrupt
• CCP2 Interrupt
Registers For Interrupt Operation
• RCON (Reset Control)
• INTCON, INTCON2, INTCON3 (Interrupt Control)
• PIR1, PIR2 (Peripheral Interrupt Request (Flag))
• PIE1, PIE2 (Peripheral Interrupt Enable)
• IPR1, IPR2 (Peripheral Interrupt Priority)
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
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.
Enabling and disabling an interrupt
Enabling and disabling an interrupt
INTCON Register
Interrupt C coding
• For XC8 compiler, use the following code to
perform interrupt:
INTCONbits.INT0IF = 0; //Clear INT0 External Interrupt Flag
INTCONbits.INT0IE = 1; //Enable INT0 External Interrupt Enable bit
INTCONbits.GIE = 1; //Enable all interrupt (Global Int Enable)

while(1) {
}
}

static void interrupt isr (void) // Interrupt function


{
if (INTCONbits.INT0IF == 1) //Check flag bit
{
//Perform task here
INTCONbits.INT0IF = 0; //Clear flag bit
}
}
External Hardware Interrupts
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
#include <xc.h>
void main (void)
{
TRISBbits.TRISB0 = 1; //INT0/RB0 as input
TRISAbits.TRISA0 = 0; //RA0 as output
ADCON1 = 0x0F; //Disable all ANx
INTCONbits.INT0IF = 0; //Clear INT0 External Interrupt Flag
INTCONbits.INT0IE = 1; //Enable INT0 External Interrupt Enable bit
INTCONbits.GIE = 1; //Enable all interrupt
INTCON2bits.INTEDG0 = 0; //Interrupt on falling edge

while(1) {
}}

static void interrupt isr (void) //Here be interrupt function


{
if (INTCONbits.INT0IF == 1) //INT0 is set?
{
LATAbits.LATA0=~LATAbits.LATA0; //Toggle RA0
INTCONbits.INT0IF = 0; //Clear INT0 bit
}
}
Timer Interrupts
Interrupt Flag Bit Register Enable Bit Register
Timer0 TMR0IF INTCON TMR0IE INTCON
Timer1 TMR1IF PIR1 TMR1IE PIE1
Timer2 TMR2IF PIR1 TMR2IE PIE1
Timer3 TMR3IF PIR2 TMR3IE PIE2
#include <xc.h>
void T0Delay (void);

void main (void)


{
ADCON1 = 0x0F; //Disable all ANx
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) {
T0Delay ( ); }
}

void T0Delay ( ){
T0CON = 0x07;
TMR0H = 0xF0;
TMR0L = 0x00;
}

static void interrupt isr (void) //Here be interrupt function


{
if (INTCONbits.TMR0IF == 1) //TMR0 is overflow?
{
T0CONbits.TMR0ON = 0; //Off Timer0
INTCONbits.TMR0IF = 0; //Clear TMR0 bit
}
}
PORTB Change Interrupt
PORTB Change Interrupt Pins
PORTB Change Int. vs External
Hardware Int.
#include <xc.h>
#define LED LATCbits.LATC4
void main (void)
{
ADCON1=0x0F;
TRISBbits.TRISB4 = 1; //RB4 as input
TRIScbits.TRISC6 = 0; //RC6 as output
INTCONbits.RBIF = 0; //Clear RBIF Flag bit
INTCONbits.RBIE = 1; //Enable PORTB Change Interrupt Enable bit
INTCONbits.GIE = 1; //Enable all interrupt

while(1) {
}
}

static void interrupt isr (void) //Here be interrupt function


{
if (INTCONbits.RBIF == 1) //RBIF is set?
{
LED = PORTBbits.RB4; //ON LED
INTCONbits.RBIF = 0; //Clear RBIF flag
}
}

You might also like