PIC Interrupts &
Interfacing-I
Interrupt Vs Polling
•There are 2 methods for communicating between the
microcontroller and the external system:
1. Polling
2. Interrupt
PA UNIT 3 (FR INFORMATION TECHNOLOGY) SPPU
What is Interrupt:
► A signal to the CPU to take an immediate action is called an interrupt.
Thus, interrupt is a process with the help of which the CPU is notified
of requiring attention. The interrupt is considered as a hardware
mechanism. The interrupt requires the operating system to stop and
figure out what to do next.
What is Polling :-
► The process in which the CPU constantly checks the status of the
device- to see if it needs the CPU's attention is called polling. It is
basically a protocol in which the CPU services the I/O devices.
Thus, the polling is the process of periodically checking status of
a device to see if it is time for the next I/O operation.
Interrupt Vs Polling
Interrupt Polling
An interrupt is a process with the help of which the
CPU is notified of requiring attention.
Polling is the process in which the CPU constantly
checks the status of a device to see if it needs the
CPU's attention.
It is considered as a hardware mechanism. It is a protocol.
An interrupt handler services/works with the device. In this protocol, the CPU services the device.
Interrupt-request line indicates that the device needs
to be serviced.
The command-ready bit indicates that the device
needs to be serviced.
An interrupt handler services/works with the device. In this protocol, the CPU services the device.
Interrupt-request line indicates that the device needs
to be serviced.
The command-ready bit indicates that the device
needs to be serviced.
CPU is used only when a device requires servicing. CPU is used only when a device requires servicing.
Interrupts save the CPU cycles. Polling wastes many of the CPU cycles.
Interrupts can occur at any point in time. CPU polls the devices at regular intervals of time.
It becomes inefficient if devices frequently interrupt
the CPU.
Polling becomes inefficient when the CPU rarely
finds a device that is ready to be serviced.
Steps in executing an interrupt
•Upon activation of interrupt the microcontroller
• Finishes executing the current instruction
• Store next instruction address in PC & current status in stack memory.
• Processor Jumps to the interrupt vector table(IVT) to get the address of ISR
and jumps to it
• Transfer program control to the ISR
• Begin executing the ISR instructions to the last instruction of ISR (RETFIF)
• Executes RETFIE
• Pops the PC from the stack
• Starts to execute from the address of that PC
11-9
Classification of Interrupts
•Maskable Interrupts
•Nonmaskable Interrupts
•Vectored Interrupts
•Non-vectored Interrupts
Note: Non Maskable : cannot be disabled or ignored by the instructions of CPU
Maskable : disabled or ignored by the instructions of CPU
PA UNIT 3 (FR INFORMATION TECHNOLOGY) SPPU
Sources of Interrupts:
► External hardware interrupts : INT0, INT1 AND INT2
► Timer Interrupts : TMR0IF, TMR1IF, TMR2IF AND TMR3IF
► Serial communication interrupts : TXIF and RCIF
► RB port change interrupt : RBIF
► A/D converter interrupt : ADIF
► Compare, Capture, PWM interrupt : CCP1IF
Simplified interrupt structure of PIC 18
Enabling and Disabling Interrupts
GIE PEIE TMR0IE INT0IE RBIE
Bit 7 GIE Global Interrupt Enable bit
1 = Enables all unmasked interrupts
0 = Disables all interrupt
Bit 6 PEIE Peripheral Interrupt Enable bit
1 = Enable all unmasked peripheral interrupts
0 = Disable all peripheral interrupts
Bit 5 TMR0IE TMR0 Overflow Interrupt Enable bit
1 = Enables the TMR0 overflow interrupt
0 = Disables the INT0 overflow interrupt
Bit 4 INT0IE INT0 External Interrupt Enable bit
1 = Enables the INT0 external interrupt
0 = Disables the INT0 external interrupt
Bit 3 RBIE RB port change Interrupt Enable bit
1 = Enables the RB port change interrupt
0 = Disables the RB port change interrupt
Interrupt Registers
► For PIC18F458 microcontroller, 13 registers are used to control
interrupt operation. These registers are :
1. RCON
2. INTCON
3. INTCON2
4. INTCON3
5. PIR1, PIR2, PIR3
6. PIE1, PIE2, PIE3
7. IPR1, IPR2, IPR3
RCON Register : Reset control register
IPEN SBOREN - R1 TO PD POR BOR
IPEN INTERRUPT PRIORITY ENABLE BIT
1= ENABLE PRIORITY LEVELS ON INTERRUPT
0= DISABLE PRIORITY LEVELS ON INTERRUPT
SBOREN BOR SOFTWARE ENABLE BIT
R1 RESET INSTRUCTION FLAG BIT
TO WATCHDOG TIME OUT FLAG BIT
PD POWER DOWN DETECTION FLAG BIT
POR POWER ON RESET FLAG BIT
BOR BROWN OUT RESET STATUS BIT
INTCON REGISTERS
► The INTCON registers are readable and writable registers that
contains various enable, priority and flag bits. Because of the
number of interrupts to be controlled, pic18F devices have three
INTCON registers.
► 1. INTCON registers
► 2. INTCON2 registers
► 3. INTCON3 registers
INTCON register:-
GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF
GIE/GIEH When IPEN=0, 1=enables all unmasked interrupts 0=disable
When IPEN=1, 1=enable all high priority interrupts 0=disable
PEIE/GIEL When IPEN=0, 1=enables all unmasked peripheral interrupts 0=disable
When IPEN=1, 1=enable all low priority peripheral interrupts 0=disable
TMR0IE 1=enable the TMR0 overflow interrupt 0=disable
INT0IE 1=enables the INT0 external interrupt 0=disable
RBIE 1=enables the RB port change interrupt 0=disable
TMR0IF 1=The timer 0 register has overflowed 0=-not overflow
INT0IF 1=The INT0 external interrupt occurred 0=disable
RBIF 1=At least one of the RB7 : RB4 pins changed state
0=None of the pins changed
RBPU INTEDG0 INTEDG1 --- --- TMROIP ----- RBIP
INTCON2 REGISTER:
INTCON3 REGISTER:
INT2IP INT1IP ---- INT2IE INT1IE ---- INT2IF INT1IF
INTCON
INTCON2
INTCON3
GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF
RBPU INTEDG0 INTEDG1 --- --- TMROIP ----- RBIP
INT2IP INT1IP ---- INT2IE INT1IE ---- INT2IF INT1IF
Show the instructions to
•A. Enable (unmask) the Timer 0 interrupt and
external hardware interrupt 0 (INT0)
BSF INTCON, TMR0IE; enable Timer 0 Interrupt
BSF INTCON, INT0IE ; enable external interrupt
BSF INTCON, GIE; allow interrupts to come in
Show the instructions to
B. Disable (mask) the Timer 0 interrupt, then
BCF INTCON, TMR0IE ; mask (disable) Timer 0 Interrupt
C. Show how to disable (mask) all the interrupts
with a single instruction.
BCF INTCON,GIE ;mask all interrupts globally
PIR registers:
► The peripheral Interrupt Request (PIR) register contains the individual
flag bits for the peripheral interrupts.
► Due to the number of peripheral interrupt sources, there are three
peripheral interrupt request registers (PIR1, PIR2, PIR3)
PIR1 (Peripheral Interrupt Request Register 1)
PSPIF: Parallel Slave Port Read/Write Interrupt
Flag bit(1)
1 = A read or a write operation has taken place
0 = No read or write has occurred
ADIF: A/D Converter Interrupt Flag bit
1 = An A/D conversion completed
0 = The A/D conversion is not complete
RCIF: USART Receive Interrupt Flag bit
1 = The USART receive buffer is full
0 = The USART receive buffer is empty
TXIF: USART Transmit Interrupt Flag bit
1 = The USART transmit buffer is empty
0 = The USART transmit buffer is full
SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit
CCP1IF: CCP1 Interrupt Flag bit
TMR2IF: TMR2 to PR2 Match Interrupt Flag bit
1 = TMR2 to PR2 match occurred
0 = No TMR2 to PR2 match occurred
TMR1IF: TMR1 Overflow Interrupt Flag bit
1 = TMR1 register overflowed
0 = TMR1 register did not overflow
CMIF: Comparator Interrupt Flag bit
1 = The comparator input has changed
0 = The comparator input has not changed
EEIF: EEPROM Write Operation Interrupt Flag bit
1 = The write operation completed
0 = The write operation is not complete or has
not been started
BCLIF: Bus Collision Interrupt Flag bit
1 = A bus collision has occurred in the SSP
when configured for I2C Master mode
0 = No bus collision has occurred
CCP2IF: CCP2 Interrupt Flag bit
PIR2 (Peripheral Interrupt Request Register 2)
PIR3
PIE Register (Peripheral Interrupt
Enable)
► PIE Register (Peripheral Interrupt Enable) contain the individual enable bits for
the peripheral interrupts.
► Due to the number of peripheral interrupt sources, there are three peripheral
Interrupt Enable registers (PIE1, PIE2, PIE3)
► When IPEN is clear, the PEIE bit must be set to enable any of these peripheral
interrupts.
PIE Register 1 (Peripheral Interrupt Enable)
PSPIE: Parallel Slave Port Read/Write
Interrupt Enable bit(1)
1 = Enables the PSP read/write interrupt
0 = Disables the PSP read/write interrupt
ADIE: A/D Converter Interrupt Enable bit
1 = Enables the A/D converter interrupt
0 = Disables the A/D converter interrupt
RCIE: USART Receive Interrupt Enable bit
1 = Enables the USART to receive interrupt
0 = Disables the USART receive interrupt
RCIE: USART Receive Interrupt Enable bit
1 = Enables the USART to receive interrupt
0 = Disables the USART receive interrupt
TXIE: USART Transmit Interrupt Enable bit
1 = Enables the USART to transmit interrupt
0 = Disables the USART transmit interrupt
SSPIE: Synchronous Serial Port Interrupt Enable bit
1 = Enables the SSP interrupt
0 = Disables the SSP interrupt
CCP1IE: CCP1 Interrupt Enable bit
1 = Enables the CCP1 interrupt
0 = Disables the CCP1 interrupt
TMR2IE: TMR2 to PR2 Match Interrupt Enable bit
1 = Enables the TMR2 to PR2 match interrupt
0 = Disables the TMR2 to PR2 match interrupt
CMIE: Comparator Interrupt Enable bit
1 = Enables the comparator interrupt
0 = Disable the comparator interrupt
EEIE: EEPROM Write Operation Interrupt Enable bit
1 = Enable EEPROM write interrupt
0 = Disable EEPROM write interrupt
BCLIE: Bus Collision Interrupt Enable bit
1 = Enable bus collision interrupt
0 = Disable bus collision interrupt
CCP2IE: CCP2 Interrupt Enable bit
1 = Enables the CCP2 interrupt
0 = Disables the CCP2 interrupt
PIE Register 3 (Peripheral Interrupt Enable)
IRXIE WAKIE ERRIE TXB2IE TXB1IE TXB0IE RXB1IE RXB0IE
IPR (Interrupt Priority Request)
IPR 1 (Interrupt Priority Request)
IPR 2 (Interrupt Priority Request)
IPR 3(Interrupt Priority Request)
Steps for programming PIC18 timer using
Interrupt:
1. Enable GIE,PEIE,TMRxIE
2. Configure the TxCON register
3. Clear TMRxIF Timer interrupt flag.
4. Load the count in Timer register TMRx.
5. Set TMRxON to start Timer operation.
6. When TMRxIF =1 , code will jump to ISR to execute it, and after
excecution control returns to the main program.
PA UNIT 3 (FR INFORMATION TECHNOLOGY) SPPU
Example: Write a C program for the PIC18 to generate a square wave of 2 KHz frequency
on RB4 using Timer0 ISR and a square wave of 5 KHz frequency on RB5 using TIMER1
ISR. Assume XTAL=10 MHz
Answer : Given : Fosc = 10 MHz , No Prescaler Ftimer =Fosc /4 = 10/4= 2.5 MHz
Period = 1/Timer= 1/ 2.5 MHz = 0.4 µsec.
For 2 KHz
T= 1/2KHz = 0.5 µsec.
Ton = Toff = T/2 = .25 µsec.
Count = Desired Delay/ Timer Period
= .25 µsec./ .4 µsec. = 625
The value to be loaded in 16 bit
Timer0 register is : 65536- period
Count=65536-625 = 65911 = FD8FH
► For 5KHz
T= 1/5KHz = 0.2 µsec.
Ton = Toff = T/2 = .1 µsec.
Count = Desired Delay/ Timer Period
= .1 µsec./ .4 µsec. = 250
The value to be loaded in 16 bit
Timer1 register is : 65536-period
Count=65536-250 = 65286 = FF06H
#include<P18F4550.h>
void Timer0_ISR(void);
void CHK_ISR(void);
#define PORTBit1 PORTbits.RB4
#define PORTBit2 PORTbits.RB5
#pragma interrupt CHK_ISR
void CHK_ISR(void)
{
if (INTCONbits.TMR0IF=1)
TIMER0_ISR();
if (PIR1bits.TMR1IF=1)
TIMER1_ISR();
}
#pragma code
Void main(void)
{
TRISBbits.TRISB4=0; configure pin4 port B as o/p
TRISBbits.TRISB5=0; configure pin5 port B as o/p
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
TOCON=0x08; timer 0 16 bitmode no prescaler
INTCONbits.TMROIF =0; clear timer 0 interrupt flag bit
TMR0H=0xFD; load higher byte
TMR0L=0X8F; load lower byte
INTCONbits.TMR0IE=1; enable timer0 interrupt
T1CON=0x08;
PIR1bits.TMR1IF =0;
TMR1H=0xFF;
TMR1L=0X06;
PIE1bits.TMR1IE=1;
TOCONbits.TMR0ON=1;
T1CONbits.TMR1ON=1;
While(1);
}
void Timer0_ISR(void)
{
PORTBit1= ~PORTBit1;
TMR0H=0xFD;
TMR0L=0x8F;
INTCONbits.TMR0IF=0;
}
void Timer1_ISR(void)
{
PORTBit2= ~PORTBit2;
TMR1H=0xFF;
TMR1L=0x06;
PIRbits.TMR1IF=0;
}
Interfacing of LED
► Current sinking and sourcing capacities in the PIC18 family is about 25 mA.
► We can drive LED using PORT in 2 ways: 1. Current Sourcing mode 2. Current sinking
mode.
► Source current is the ability of the digital output/input port to supply current. Sink current
is the ability of the port to receive current.
a. LED connected in current source mode b. LED connected in current sink mode
Q. Write an embedded pic18 c program to
flash the LED connected to pin 0 of PORTB
Assume that Ton and Toff of LED =
500 msec.
#include<pic18f4550.h>
#define PortBit PORTbits.RB0
void DELAY(unsigned int);
void main(void)
{
TRISBits.TRISB=0;
While(1);
{
PortBit =1;
DELAY(500);
PortBit =0;
DELAY(500);
}
}
Void DELAY (unsigned int t);
{
unsigned int I,j;
for(i=0; i<t; i++)
for(j=0; j<165; j++);
}
Q. Write an embedded pic18 c program to
blink the LED connected to PORTC
Assume that Ton and Toff of LED =
500 msec.
#include<pic18f4550.h>
void DELAY(unsigned int);
void main(void)
{
TRISC=0;
While(1);
{
PORTC=0x00;
DELAY(500);
PORTC=0xFF;
DELAY(500);
}
}
void DELAY (unsigned int t);
{
unsigned int I,j;
for(i=0; i<t; i++)
for(j=0; j<165; j++);
}
Q. An LED connected to each pin of PORT D. Write a
PIC18 C program that will turn each LED from pin D0 to
D7. Call a delay module before turning on the next LED
#include<pic18f4550.h>
#define PD0 PORTDbits.RD0
#define PD1 PORTDbits.RD1
#define PD2 PORTDbits.RD2
#define PD3 PORTDbits.RD3
#define PD4 PORTDbits.RD4
#define PD5 PORTDbits.RD5
#define PD6 PORTDbits.RD6
#define PD7 PORTDbits.RD7
void DELAY(unsigned int);
void main(void)
{
TRISD=0;
While(1);
{
PD0 = 1;
DELAY(500);
PD1 = 1;
DELAY(500);
PD2 = 1;
DELAY(500);
PD3= 1;
DELAY(500);
PD4 = 1;
DELAY(500);
PD5 = 1;
DELAY(500);
PD6 = 1;
DELAY(500);
PD7 = 1;
DELAY(500);
}
}
void DELAY (unsigned int t);
{
unsigned int I,j;
for(i=0; i<t; i++)
for(j=0; j<165; j++);
}
Interfacing of LCD:-
RD0
RD1
RD2
RD3
RD4
RD5
RD6
RD7
RE0
RE1
RE2
DB0
DB1
DB2
DB3
DB4
DB5
DB6
DB7 RS RW E
16 character * 2 line
LCD display module
16 character * 2 line
LCD display module
PIC18XXX
Vcc
VEE
VSS
Vcc
VEE
VSS
The three control lines are referred to as EN, RS, and RW.
The EN line is called “Enable.” This control line is used to tell the LCD that you are sending it data.
To send data to the LCD, your program should make sure this line is low (0) and then set the other
two control lines and/or put data on the data bus.
When the other lines are completely ready, bring EN high (1) and wait for the minimum amount of
time required by the LCD datasheet (this varies from LCD to LCD), and end by bringing it low (0)
again.
The RS line is the “Register Select” line.
When RS is low (0), the data is to be treated as a command or special instruction (such as a clear
screen, position cursor, etc.).
When RS is high (1), the data being sent is text data which should be displayed on the screen. For
example, to display the letter “T” on the screen, you would set RS high.
The RW line is the “Read/Write” control line. When RW is low (0), the information on the data bus
is being written to the LCD. When RW is high (1), the program is effectively querying (or reading)
the LCD. Only one instruction (“Get LCD status”) is a read command.
Program:
#include<PIC18F4550.h>
#define LCDData PORTD
#define RS PORTBbits.RE0
#define RW PORTBbits.RE1
#define EN PORTBbits.RE2
void LCD (unsigned char, unsigned char);
void DELAY (unsigned int);
void main (void)
{
TRISD = 0;
TRISE = 0;
EN=0;
LCD(0x38,0)
DELAY(250);
LCD(0x0E,0);
DELAY(15);
LCD(0x01,0);
DELAY(15);
LCD(0x06,0);
DELAY(15);
LCD(0x86,0);
DELAY(15);
LCD(‘W’,1);
DELAY(15);
LCD(‘E’,1);
DELAY(15);
LCD(‘L’,1);
DELAY(15);
LCD(‘C’,1);
DELAY(15);
LCD(‘O’,1);
DELAY(15);
LCD(‘M’,1);
DELAY(15);
LCD(‘E’,1);
}
void LCD (unsigned char value, unsigned
char flag)
{
LCDdata = value;
RS=flag;
RW=0;
EN=1;
DELAY(1);
EN=0;
}
Void DELAY(unsigned int cnt)
{
Unsigned int I,j;
For(i=0; i<cnt; i++)
for(j=0;j<165;j++);
}
4*4 keyboard matrix interfacing with PIC
controller
Interfacing Relay:-
PIC 18FXXX
#include<PIC18F4550.h>
#define PORTBIT PORTBbits.RB0
void main(void)
{
unsigned int i;
TRISBbits.TRISB0=0;
PORTBIT=1;
DELAY(2000);
PORTBIT=0;
}
void DELAY(unsigned int cnt)
{
unsigned int i,j;
for(i=0; i<cnt; i++)
for(j=0; j<165; j++)
}

More Related Content

PPT
Interrupts
PDF
8051-interrupts-temporary suspension of a program
PDF
Microcontroller part 2
PDF
Registers
PPTX
Interrupt programming
PPTX
unit 3 a.pptxppppppppppppppppppppppppppp
PPT
Interrupt programming with 8051 microcontroller
PPT
Interrupts for PIC18
Interrupts
8051-interrupts-temporary suspension of a program
Microcontroller part 2
Registers
Interrupt programming
unit 3 a.pptxppppppppppppppppppppppppppp
Interrupt programming with 8051 microcontroller
Interrupts for PIC18

Similar to PA UNIT 3 (FR INFORMATION TECHNOLOGY) SPPU (20)

PDF
Microprocessor 8051
PPTX
Micro controller 8051 Interrupts
PPTX
Interrupt.pptx
PDF
Lecture7
PPT
PPTX
Embedded systems, lesson 16
DOCX
moving message display of lcd
PPTX
Mc module5 ppt_msj
PPT
8051 interrupts
PPTX
8051 interrupt Presentation Download for ECE
PPTX
Interrupt in 8051
PPTX
Interrupt in ATMEGA328P.pptx
PPTX
Interrupts programming in embedded C using 8051
PPT
UART Protocol For Serial Communication.ppt
PPT
lesson01.ppt
PDF
8051 Interrupts
PPTX
MICROPROCESSOR AND MICROCONTROLLER ARCHITECTURE
PPTX
Interrupts in 8051
PPT
8051 Inturrpt
PPTX
Basic computer organisation design
Microprocessor 8051
Micro controller 8051 Interrupts
Interrupt.pptx
Lecture7
Embedded systems, lesson 16
moving message display of lcd
Mc module5 ppt_msj
8051 interrupts
8051 interrupt Presentation Download for ECE
Interrupt in 8051
Interrupt in ATMEGA328P.pptx
Interrupts programming in embedded C using 8051
UART Protocol For Serial Communication.ppt
lesson01.ppt
8051 Interrupts
MICROPROCESSOR AND MICROCONTROLLER ARCHITECTURE
Interrupts in 8051
8051 Inturrpt
Basic computer organisation design
Ad

Recently uploaded (20)

DOC
T Pandian CV Madurai pandi kokkaf illaya
PDF
Computer System Architecture 3rd Edition-M Morris Mano.pdf
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PDF
electrical machines course file-anna university
PDF
Design of Material Handling Equipment Lecture Note
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PDF
Present and Future of Systems Engineering: Air Combat Systems
PPTX
MAD Unit - 3 User Interface and Data Management (Diploma IT)
PPT
Programmable Logic Controller PLC and Industrial Automation
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
mechattonicsand iotwith sensor and actuator
PPT
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
PDF
Unit1 - AIML Chapter 1 concept and ethics
PDF
Computer organization and architecuture Digital Notes....pdf
PPTX
Petroleum Refining & Petrochemicals.pptx
PDF
Cryptography and Network Security-Module-I.pdf
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
T Pandian CV Madurai pandi kokkaf illaya
Computer System Architecture 3rd Edition-M Morris Mano.pdf
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
electrical machines course file-anna university
Design of Material Handling Equipment Lecture Note
August -2025_Top10 Read_Articles_ijait.pdf
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
Present and Future of Systems Engineering: Air Combat Systems
MAD Unit - 3 User Interface and Data Management (Diploma IT)
Programmable Logic Controller PLC and Industrial Automation
distributed database system" (DDBS) is often used to refer to both the distri...
mechattonicsand iotwith sensor and actuator
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
Unit1 - AIML Chapter 1 concept and ethics
Computer organization and architecuture Digital Notes....pdf
Petroleum Refining & Petrochemicals.pptx
Cryptography and Network Security-Module-I.pdf
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
Ad

PA UNIT 3 (FR INFORMATION TECHNOLOGY) SPPU

  • 2. Interrupt Vs Polling •There are 2 methods for communicating between the microcontroller and the external system: 1. Polling 2. Interrupt
  • 4. What is Interrupt: ► A signal to the CPU to take an immediate action is called an interrupt. Thus, interrupt is a process with the help of which the CPU is notified of requiring attention. The interrupt is considered as a hardware mechanism. The interrupt requires the operating system to stop and figure out what to do next.
  • 5. What is Polling :- ► The process in which the CPU constantly checks the status of the device- to see if it needs the CPU's attention is called polling. It is basically a protocol in which the CPU services the I/O devices. Thus, the polling is the process of periodically checking status of a device to see if it is time for the next I/O operation.
  • 6. Interrupt Vs Polling Interrupt Polling An interrupt is a process with the help of which the CPU is notified of requiring attention. Polling is the process in which the CPU constantly checks the status of a device to see if it needs the CPU's attention. It is considered as a hardware mechanism. It is a protocol. An interrupt handler services/works with the device. In this protocol, the CPU services the device. Interrupt-request line indicates that the device needs to be serviced. The command-ready bit indicates that the device needs to be serviced. An interrupt handler services/works with the device. In this protocol, the CPU services the device. Interrupt-request line indicates that the device needs to be serviced. The command-ready bit indicates that the device needs to be serviced. CPU is used only when a device requires servicing. CPU is used only when a device requires servicing. Interrupts save the CPU cycles. Polling wastes many of the CPU cycles. Interrupts can occur at any point in time. CPU polls the devices at regular intervals of time. It becomes inefficient if devices frequently interrupt the CPU. Polling becomes inefficient when the CPU rarely finds a device that is ready to be serviced.
  • 7. Steps in executing an interrupt •Upon activation of interrupt the microcontroller • Finishes executing the current instruction • Store next instruction address in PC & current status in stack memory. • Processor Jumps to the interrupt vector table(IVT) to get the address of ISR and jumps to it • Transfer program control to the ISR • Begin executing the ISR instructions to the last instruction of ISR (RETFIF) • Executes RETFIE • Pops the PC from the stack • Starts to execute from the address of that PC 11-9
  • 8. Classification of Interrupts •Maskable Interrupts •Nonmaskable Interrupts •Vectored Interrupts •Non-vectored Interrupts
  • 9. Note: Non Maskable : cannot be disabled or ignored by the instructions of CPU Maskable : disabled or ignored by the instructions of CPU
  • 11. Sources of Interrupts: ► External hardware interrupts : INT0, INT1 AND INT2 ► Timer Interrupts : TMR0IF, TMR1IF, TMR2IF AND TMR3IF ► Serial communication interrupts : TXIF and RCIF ► RB port change interrupt : RBIF ► A/D converter interrupt : ADIF ► Compare, Capture, PWM interrupt : CCP1IF
  • 13. Enabling and Disabling Interrupts GIE PEIE TMR0IE INT0IE RBIE Bit 7 GIE Global Interrupt Enable bit 1 = Enables all unmasked interrupts 0 = Disables all interrupt Bit 6 PEIE Peripheral Interrupt Enable bit 1 = Enable all unmasked peripheral interrupts 0 = Disable all peripheral interrupts Bit 5 TMR0IE TMR0 Overflow Interrupt Enable bit 1 = Enables the TMR0 overflow interrupt 0 = Disables the INT0 overflow interrupt Bit 4 INT0IE INT0 External Interrupt Enable bit 1 = Enables the INT0 external interrupt 0 = Disables the INT0 external interrupt Bit 3 RBIE RB port change Interrupt Enable bit 1 = Enables the RB port change interrupt 0 = Disables the RB port change interrupt
  • 14. Interrupt Registers ► For PIC18F458 microcontroller, 13 registers are used to control interrupt operation. These registers are : 1. RCON 2. INTCON 3. INTCON2 4. INTCON3 5. PIR1, PIR2, PIR3 6. PIE1, PIE2, PIE3 7. IPR1, IPR2, IPR3
  • 15. RCON Register : Reset control register IPEN SBOREN - R1 TO PD POR BOR IPEN INTERRUPT PRIORITY ENABLE BIT 1= ENABLE PRIORITY LEVELS ON INTERRUPT 0= DISABLE PRIORITY LEVELS ON INTERRUPT SBOREN BOR SOFTWARE ENABLE BIT R1 RESET INSTRUCTION FLAG BIT TO WATCHDOG TIME OUT FLAG BIT PD POWER DOWN DETECTION FLAG BIT POR POWER ON RESET FLAG BIT BOR BROWN OUT RESET STATUS BIT
  • 16. INTCON REGISTERS ► The INTCON registers are readable and writable registers that contains various enable, priority and flag bits. Because of the number of interrupts to be controlled, pic18F devices have three INTCON registers. ► 1. INTCON registers ► 2. INTCON2 registers ► 3. INTCON3 registers
  • 17. INTCON register:- GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF
  • 18. GIE/GIEH When IPEN=0, 1=enables all unmasked interrupts 0=disable When IPEN=1, 1=enable all high priority interrupts 0=disable PEIE/GIEL When IPEN=0, 1=enables all unmasked peripheral interrupts 0=disable When IPEN=1, 1=enable all low priority peripheral interrupts 0=disable TMR0IE 1=enable the TMR0 overflow interrupt 0=disable INT0IE 1=enables the INT0 external interrupt 0=disable RBIE 1=enables the RB port change interrupt 0=disable TMR0IF 1=The timer 0 register has overflowed 0=-not overflow INT0IF 1=The INT0 external interrupt occurred 0=disable RBIF 1=At least one of the RB7 : RB4 pins changed state 0=None of the pins changed
  • 19. RBPU INTEDG0 INTEDG1 --- --- TMROIP ----- RBIP INTCON2 REGISTER:
  • 20. INTCON3 REGISTER: INT2IP INT1IP ---- INT2IE INT1IE ---- INT2IF INT1IF
  • 21. INTCON INTCON2 INTCON3 GIE/GIEH PEIE/GIEL TMR0IE INT0IE RBIE TMR0IF INT0IF RBIF RBPU INTEDG0 INTEDG1 --- --- TMROIP ----- RBIP INT2IP INT1IP ---- INT2IE INT1IE ---- INT2IF INT1IF
  • 22. Show the instructions to •A. Enable (unmask) the Timer 0 interrupt and external hardware interrupt 0 (INT0) BSF INTCON, TMR0IE; enable Timer 0 Interrupt BSF INTCON, INT0IE ; enable external interrupt BSF INTCON, GIE; allow interrupts to come in
  • 23. Show the instructions to B. Disable (mask) the Timer 0 interrupt, then BCF INTCON, TMR0IE ; mask (disable) Timer 0 Interrupt C. Show how to disable (mask) all the interrupts with a single instruction. BCF INTCON,GIE ;mask all interrupts globally
  • 24. PIR registers: ► The peripheral Interrupt Request (PIR) register contains the individual flag bits for the peripheral interrupts. ► Due to the number of peripheral interrupt sources, there are three peripheral interrupt request registers (PIR1, PIR2, PIR3)
  • 25. PIR1 (Peripheral Interrupt Request Register 1) PSPIF: Parallel Slave Port Read/Write Interrupt Flag bit(1) 1 = A read or a write operation has taken place 0 = No read or write has occurred ADIF: A/D Converter Interrupt Flag bit 1 = An A/D conversion completed 0 = The A/D conversion is not complete RCIF: USART Receive Interrupt Flag bit 1 = The USART receive buffer is full 0 = The USART receive buffer is empty TXIF: USART Transmit Interrupt Flag bit 1 = The USART transmit buffer is empty 0 = The USART transmit buffer is full SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit CCP1IF: CCP1 Interrupt Flag bit TMR2IF: TMR2 to PR2 Match Interrupt Flag bit 1 = TMR2 to PR2 match occurred 0 = No TMR2 to PR2 match occurred TMR1IF: TMR1 Overflow Interrupt Flag bit 1 = TMR1 register overflowed 0 = TMR1 register did not overflow
  • 26. CMIF: Comparator Interrupt Flag bit 1 = The comparator input has changed 0 = The comparator input has not changed EEIF: EEPROM Write Operation Interrupt Flag bit 1 = The write operation completed 0 = The write operation is not complete or has not been started BCLIF: Bus Collision Interrupt Flag bit 1 = A bus collision has occurred in the SSP when configured for I2C Master mode 0 = No bus collision has occurred CCP2IF: CCP2 Interrupt Flag bit PIR2 (Peripheral Interrupt Request Register 2)
  • 27. PIR3
  • 28. PIE Register (Peripheral Interrupt Enable) ► PIE Register (Peripheral Interrupt Enable) contain the individual enable bits for the peripheral interrupts. ► Due to the number of peripheral interrupt sources, there are three peripheral Interrupt Enable registers (PIE1, PIE2, PIE3) ► When IPEN is clear, the PEIE bit must be set to enable any of these peripheral interrupts.
  • 29. PIE Register 1 (Peripheral Interrupt Enable) PSPIE: Parallel Slave Port Read/Write Interrupt Enable bit(1) 1 = Enables the PSP read/write interrupt 0 = Disables the PSP read/write interrupt ADIE: A/D Converter Interrupt Enable bit 1 = Enables the A/D converter interrupt 0 = Disables the A/D converter interrupt RCIE: USART Receive Interrupt Enable bit 1 = Enables the USART to receive interrupt 0 = Disables the USART receive interrupt RCIE: USART Receive Interrupt Enable bit 1 = Enables the USART to receive interrupt 0 = Disables the USART receive interrupt TXIE: USART Transmit Interrupt Enable bit 1 = Enables the USART to transmit interrupt 0 = Disables the USART transmit interrupt SSPIE: Synchronous Serial Port Interrupt Enable bit 1 = Enables the SSP interrupt 0 = Disables the SSP interrupt CCP1IE: CCP1 Interrupt Enable bit 1 = Enables the CCP1 interrupt 0 = Disables the CCP1 interrupt TMR2IE: TMR2 to PR2 Match Interrupt Enable bit 1 = Enables the TMR2 to PR2 match interrupt 0 = Disables the TMR2 to PR2 match interrupt
  • 30. CMIE: Comparator Interrupt Enable bit 1 = Enables the comparator interrupt 0 = Disable the comparator interrupt EEIE: EEPROM Write Operation Interrupt Enable bit 1 = Enable EEPROM write interrupt 0 = Disable EEPROM write interrupt BCLIE: Bus Collision Interrupt Enable bit 1 = Enable bus collision interrupt 0 = Disable bus collision interrupt CCP2IE: CCP2 Interrupt Enable bit 1 = Enables the CCP2 interrupt 0 = Disables the CCP2 interrupt
  • 31. PIE Register 3 (Peripheral Interrupt Enable) IRXIE WAKIE ERRIE TXB2IE TXB1IE TXB0IE RXB1IE RXB0IE
  • 33. IPR 1 (Interrupt Priority Request)
  • 34. IPR 2 (Interrupt Priority Request)
  • 36. Steps for programming PIC18 timer using Interrupt: 1. Enable GIE,PEIE,TMRxIE 2. Configure the TxCON register 3. Clear TMRxIF Timer interrupt flag. 4. Load the count in Timer register TMRx. 5. Set TMRxON to start Timer operation. 6. When TMRxIF =1 , code will jump to ISR to execute it, and after excecution control returns to the main program.
  • 38. Example: Write a C program for the PIC18 to generate a square wave of 2 KHz frequency on RB4 using Timer0 ISR and a square wave of 5 KHz frequency on RB5 using TIMER1 ISR. Assume XTAL=10 MHz Answer : Given : Fosc = 10 MHz , No Prescaler Ftimer =Fosc /4 = 10/4= 2.5 MHz Period = 1/Timer= 1/ 2.5 MHz = 0.4 µsec. For 2 KHz T= 1/2KHz = 0.5 µsec. Ton = Toff = T/2 = .25 µsec. Count = Desired Delay/ Timer Period = .25 µsec./ .4 µsec. = 625 The value to be loaded in 16 bit Timer0 register is : 65536- period Count=65536-625 = 65911 = FD8FH ► For 5KHz T= 1/5KHz = 0.2 µsec. Ton = Toff = T/2 = .1 µsec. Count = Desired Delay/ Timer Period = .1 µsec./ .4 µsec. = 250 The value to be loaded in 16 bit Timer1 register is : 65536-period Count=65536-250 = 65286 = FF06H
  • 39. #include<P18F4550.h> void Timer0_ISR(void); void CHK_ISR(void); #define PORTBit1 PORTbits.RB4 #define PORTBit2 PORTbits.RB5 #pragma interrupt CHK_ISR void CHK_ISR(void) { if (INTCONbits.TMR0IF=1) TIMER0_ISR(); if (PIR1bits.TMR1IF=1) TIMER1_ISR(); } #pragma code Void main(void) { TRISBbits.TRISB4=0; configure pin4 port B as o/p TRISBbits.TRISB5=0; configure pin5 port B as o/p INTCONbits.GIE=1; INTCONbits.PEIE=1; TOCON=0x08; timer 0 16 bitmode no prescaler INTCONbits.TMROIF =0; clear timer 0 interrupt flag bit TMR0H=0xFD; load higher byte TMR0L=0X8F; load lower byte INTCONbits.TMR0IE=1; enable timer0 interrupt T1CON=0x08; PIR1bits.TMR1IF =0; TMR1H=0xFF; TMR1L=0X06; PIE1bits.TMR1IE=1; TOCONbits.TMR0ON=1; T1CONbits.TMR1ON=1; While(1); }
  • 40. void Timer0_ISR(void) { PORTBit1= ~PORTBit1; TMR0H=0xFD; TMR0L=0x8F; INTCONbits.TMR0IF=0; } void Timer1_ISR(void) { PORTBit2= ~PORTBit2; TMR1H=0xFF; TMR1L=0x06; PIRbits.TMR1IF=0; }
  • 41. Interfacing of LED ► Current sinking and sourcing capacities in the PIC18 family is about 25 mA. ► We can drive LED using PORT in 2 ways: 1. Current Sourcing mode 2. Current sinking mode. ► Source current is the ability of the digital output/input port to supply current. Sink current is the ability of the port to receive current. a. LED connected in current source mode b. LED connected in current sink mode
  • 42. Q. Write an embedded pic18 c program to flash the LED connected to pin 0 of PORTB Assume that Ton and Toff of LED = 500 msec. #include<pic18f4550.h> #define PortBit PORTbits.RB0 void DELAY(unsigned int); void main(void) { TRISBits.TRISB=0; While(1); { PortBit =1; DELAY(500); PortBit =0; DELAY(500); } } Void DELAY (unsigned int t); { unsigned int I,j; for(i=0; i<t; i++) for(j=0; j<165; j++); }
  • 43. Q. Write an embedded pic18 c program to blink the LED connected to PORTC Assume that Ton and Toff of LED = 500 msec. #include<pic18f4550.h> void DELAY(unsigned int); void main(void) { TRISC=0; While(1); { PORTC=0x00; DELAY(500); PORTC=0xFF; DELAY(500); } } void DELAY (unsigned int t); { unsigned int I,j; for(i=0; i<t; i++) for(j=0; j<165; j++); }
  • 44. Q. An LED connected to each pin of PORT D. Write a PIC18 C program that will turn each LED from pin D0 to D7. Call a delay module before turning on the next LED #include<pic18f4550.h> #define PD0 PORTDbits.RD0 #define PD1 PORTDbits.RD1 #define PD2 PORTDbits.RD2 #define PD3 PORTDbits.RD3 #define PD4 PORTDbits.RD4 #define PD5 PORTDbits.RD5 #define PD6 PORTDbits.RD6 #define PD7 PORTDbits.RD7 void DELAY(unsigned int); void main(void) { TRISD=0; While(1); { PD0 = 1; DELAY(500); PD1 = 1; DELAY(500); PD2 = 1; DELAY(500); PD3= 1; DELAY(500); PD4 = 1; DELAY(500); PD5 = 1; DELAY(500); PD6 = 1; DELAY(500); PD7 = 1; DELAY(500); } } void DELAY (unsigned int t); { unsigned int I,j; for(i=0; i<t; i++) for(j=0; j<165; j++); }
  • 45. Interfacing of LCD:- RD0 RD1 RD2 RD3 RD4 RD5 RD6 RD7 RE0 RE1 RE2 DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 RS RW E 16 character * 2 line LCD display module 16 character * 2 line LCD display module PIC18XXX Vcc VEE VSS Vcc VEE VSS
  • 46. The three control lines are referred to as EN, RS, and RW. The EN line is called “Enable.” This control line is used to tell the LCD that you are sending it data. To send data to the LCD, your program should make sure this line is low (0) and then set the other two control lines and/or put data on the data bus. When the other lines are completely ready, bring EN high (1) and wait for the minimum amount of time required by the LCD datasheet (this varies from LCD to LCD), and end by bringing it low (0) again. The RS line is the “Register Select” line. When RS is low (0), the data is to be treated as a command or special instruction (such as a clear screen, position cursor, etc.). When RS is high (1), the data being sent is text data which should be displayed on the screen. For example, to display the letter “T” on the screen, you would set RS high. The RW line is the “Read/Write” control line. When RW is low (0), the information on the data bus is being written to the LCD. When RW is high (1), the program is effectively querying (or reading) the LCD. Only one instruction (“Get LCD status”) is a read command.
  • 47. Program: #include<PIC18F4550.h> #define LCDData PORTD #define RS PORTBbits.RE0 #define RW PORTBbits.RE1 #define EN PORTBbits.RE2 void LCD (unsigned char, unsigned char); void DELAY (unsigned int); void main (void) { TRISD = 0; TRISE = 0; EN=0; LCD(0x38,0) DELAY(250); LCD(0x0E,0); DELAY(15); LCD(0x01,0); DELAY(15); LCD(0x06,0); DELAY(15); LCD(0x86,0); DELAY(15); LCD(‘W’,1); DELAY(15); LCD(‘E’,1); DELAY(15); LCD(‘L’,1); DELAY(15); LCD(‘C’,1); DELAY(15); LCD(‘O’,1); DELAY(15); LCD(‘M’,1); DELAY(15); LCD(‘E’,1); } void LCD (unsigned char value, unsigned char flag) { LCDdata = value; RS=flag; RW=0; EN=1; DELAY(1); EN=0; } Void DELAY(unsigned int cnt) { Unsigned int I,j; For(i=0; i<cnt; i++) for(j=0;j<165;j++); }
  • 48. 4*4 keyboard matrix interfacing with PIC controller
  • 49. Interfacing Relay:- PIC 18FXXX #include<PIC18F4550.h> #define PORTBIT PORTBbits.RB0 void main(void) { unsigned int i; TRISBbits.TRISB0=0; PORTBIT=1; DELAY(2000); PORTBIT=0; } void DELAY(unsigned int cnt) { unsigned int i,j; for(i=0; i<cnt; i++) for(j=0; j<165; j++) }