Professional Documents
Culture Documents
Unit 4 Interrupt
Unit 4 Interrupt
INTERRUPT
PROGRAMMING IN C
CHAPTER 4
2
Interrupt vs Polling
devices.
• There are 2 methods for a device to receive
• 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
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
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
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
}
Default: IPEN = 0
In this course,
we will not set interrupt priority.
21
RB2/INT2.
• These interrupt is triggered whenever there is a change of state on the
not.
• Bit TMR0IP will determine interrupt priority bit.
30
while(1) {
LATC0 ^ = 1; //Toggle RC0
T0Delay ( ); } }
void T0Delay ( ) {
T0CON = 0xC7; //8 bit timer
TMR0L = 0xFF; }
while (1)
{
T0CONbits.TMR0ON=1; //Turn ON TMR0
PORTD = TMR0L; } }