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
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
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
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)
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
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);
}
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++);
}
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.