SlideShare a Scribd company logo
1 of 14
Download to read offline
Handling Interrupts in Microchip PIC18F
Microcontrollers
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro Interrupts in PIC18F Family
Interrupts in MCUs
The core of MCUs manages interrupts, as in normal CPUs
In a MCU, each peripheral event can generate an
interrupt, e.g.:
The overflow of a timer;
The reception/trasmission of a byte through the UART
(serial port);
The end of conversion of the ADC;
...
The peripheral is called interrupt source
When an interrupt is generated, the normal program flow is
interrupted, a specific function is invoked, called ISR -
Interrupt Service Routine; at the end, the normal
program flow is resumed.
Corrado Santoro Interrupts in PIC18F Family
Interrupts in Microchip MCUs
Each peripheral which can generate interrupt has two
control bits:
xxxIF, the interrupt flag, it is set to ‘‘1’’, by the
hardware, when the “event” occurs; it must be reset by the
software;
xxxIE, the interrupt enable bit; when set to ‘‘1’’ (by the
software) the “event”, when occurs, generates a CPU
interrupt
Moreover, there are other bits which control the interrupt
circuit:
GIE, the global interrupt enable flag; when set to ‘‘1’’,
interrupt sources are routed to the CPU;
IPEN, the interrupt priorities enable flag; when set to
‘‘1’’, two priorities are handled low and high/urgent;
PEIE, the peripheral interrupt enable flag; when set to
‘‘1’’, interrupt sources from peripherals are enabled.
Corrado Santoro Interrupts in PIC18F Family
The Interrupt Circuit
Corrado Santoro Interrupts in PIC18F Family
Enabling Interrupts in Microchip MCUs (No priorities)
1 Program the peripheral according to its working mode;
2 Reset the peripheral interrupt flag xxxIF = 0;;
3 Set the peripheral interrupt enable flag xxxIE = 1;;
4 Disable priorities handling RCONbits.IPEN = 0;;
5 Enable global interupt flag INTbits.GIE = 1;;
6 Enable peripheral interupt flag INTbits.PEIE = 1;;
Corrado Santoro Interrupts in PIC18F Family
Enabling Interrupts in Microchip MCUs (With priorities)
1 Program the peripheral according to its working mode;
2 Reset the peripheral interrupt flag xxxIF = 0;;
3 Set the peripheral interrupt enable flag xxxIE = 1;;
4 Set the peripheral interrupt priority flag (0 = low, 1 = high)
xxxIP = yy;
5 Enable priorities handling RCONbits.IPEN = 1;;
6 Enable/disable high interupts INTbits.GIEH = yy;;
7 Enable/disable low priority interupts INTbits.GIEL =
yy;;
Corrado Santoro Interrupts in PIC18F Family
Handling Interrupts in Microchip MCUs
Define a C function marked as interrupt;
Check if the peripheral interrupt flag is on;
Serve the peripheral interrupt;
Reset the peripheral interrupt flag.
Handling TMR0 interrupt
...
void interrupt isr()
{
if (INTCONbits.T0IF == 1) {
// ... handle the TMR interrupt
INTCONbits.T0IF = 0;
}
}
...
Corrado Santoro Interrupts in PIC18F Family
Handling Several Interrupts in Microchip MCUs
Handling TMR0 & TMR1 interrupts
...
void interrupt isr()
{
if (INTCONbits.TMR0IF == 1) {
// ... handle the TMR0 interrupt
INTCONbits.TMR0IF = 0;
}
if (PIR1bits.TMR1IF == 1) {
// ... handle the TMR1 interrupt
PIR1bits.TMR1IF = 0;
}
}
...
Corrado Santoro Interrupts in PIC18F Family
Example
A LED RB0
A pushbuttons RA3
Pressing pushbutton starts/stops flashing at a period of
200ms
Corrado Santoro Interrupts in PIC18F Family
Let’s determine timer setup
We want to use the system clock, T0CS = 0;;
We have FOSC = 64MHz, therefore the basic frequency is
FOSC/4 = 16MHz, the P = 62.5ns;
Let’s use the prescaler and divide the frequency by 256, so PSA = 0;
T0PS = 0b111;
The timer increments using a period P = 62.5ns ∗ 256 = 16µs.
So 200ms/16µs = 12500 counts, therefore the TMR0 setup value is
−12500.
Corrado Santoro Interrupts in PIC18F Family
Let’s determine timer setup
Timer Setup
...
T0CONbits.TMR0ON = 0; // stop the timer
T0CONbits.T08BIT = 0; // timer configured as 16-bit
T0CONbits.T0CS = 0; // use system clock
T0CONbits.PSA = 0; // use prescaler
T0CONbits.T0PS = 0b111;
// prescaler 1:256 (’0b’ is a prefix for binary)
TMR0 = -12500; // setup initial timer value
INTCONbits.T0IF = 0; // reset timer interrupt flag
INTCONbits.T0IE = 1; // enable timer interrupts
RCONbits.IPEN = 0; // do not use priorities
INTCONbits.PEIE = 1; // enable peripheral interrupts
INTCONbits.GIE = 1; // enable interrupts globally
...
Corrado Santoro Interrupts in PIC18F Family
Let’s handle interrupts
Timer Interrupt Handling
...
void interrupt isr()
{
if (INTCONbits.T0IF == 1) {
TMR0 = -12500; // reload timer value
// invert the LED
LATBbits.LATB0 = !LATBbits.LATB0;
INTCONbits.T0IF = 0;
}
}
...
Corrado Santoro Interrupts in PIC18F Family
Let’s handle timer on/off
Timer on/off
...
for (;;) { // loop forever
while (PORTAbits.RA3 == 0) {};
// if the push button is DOWN, wait
while (PORTAbits.RA3 == 1) {};
// if the push button is UP, wait
// transition got, let’s invert the TMRON flag
T0CONbits.TMR0ON = !T0CONbits.TMR0ON;
}
}
Corrado Santoro Interrupts in PIC18F Family
Handling Interrupts in Microchip PIC18F
Microcontrollers
Corrado Santoro
ARSLAB - Autonomous and Robotic Systems Laboratory
Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy
santoro@dmi.unict.it
L.A.P. 1 Course
Corrado Santoro Interrupts in PIC18F Family

More Related Content

What's hot

PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programmingAkash Puri
 
Interrupts at AVR
Interrupts at AVRInterrupts at AVR
Interrupts at AVRHamdy Fouad
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C raosandy11
 
Interfacing with Atmega 16
Interfacing with Atmega 16Interfacing with Atmega 16
Interfacing with Atmega 16Ramadan Ramadan
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Aarav Soni
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerCorrado Santoro
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converterMohamed Ali
 
Microcontroller Instruction Set atmel
Microcontroller Instruction Set atmelMicrocontroller Instruction Set atmel
Microcontroller Instruction Set atmelRuderocker Billy
 
Interrupts for PIC18
Interrupts for PIC18Interrupts for PIC18
Interrupts for PIC18raosandy11
 
AVR Fundamentals
AVR FundamentalsAVR Fundamentals
AVR FundamentalsVinit Vyas
 

What's hot (20)

PIC timer programming
PIC timer programmingPIC timer programming
PIC timer programming
 
Ccp
CcpCcp
Ccp
 
Lecture 2 timers, pwm, state machine IN PIC
Lecture 2   timers, pwm, state machine IN PIC Lecture 2   timers, pwm, state machine IN PIC
Lecture 2 timers, pwm, state machine IN PIC
 
Interrupts at AVR
Interrupts at AVRInterrupts at AVR
Interrupts at AVR
 
Class9
Class9Class9
Class9
 
Lecture7
Lecture7Lecture7
Lecture7
 
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND CPIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
PIC18 TIMER PROGRAMMING IN ASSEMBLY AND C
 
Interfacing with Atmega 16
Interfacing with Atmega 16Interfacing with Atmega 16
Interfacing with Atmega 16
 
Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)Timer counter in arm7(lpc2148)
Timer counter in arm7(lpc2148)
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontroller
 
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
AVR_Course_Day6 external hardware  interrupts and analogue to digital converterAVR_Course_Day6 external hardware  interrupts and analogue to digital converter
AVR_Course_Day6 external hardware interrupts and analogue to digital converter
 
Microcontroller Instruction Set atmel
Microcontroller Instruction Set atmelMicrocontroller Instruction Set atmel
Microcontroller Instruction Set atmel
 
89c5131datasheet
89c5131datasheet89c5131datasheet
89c5131datasheet
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
Interrupts for PIC18
Interrupts for PIC18Interrupts for PIC18
Interrupts for PIC18
 
AVR Fundamentals
AVR FundamentalsAVR Fundamentals
AVR Fundamentals
 
Ii avr-basics(1)
Ii avr-basics(1)Ii avr-basics(1)
Ii avr-basics(1)
 
Interrupt
InterruptInterrupt
Interrupt
 
Ec8791 lpc2148 timer unit
Ec8791 lpc2148 timer unitEc8791 lpc2148 timer unit
Ec8791 lpc2148 timer unit
 
AVR introduction
AVR introduction AVR introduction
AVR introduction
 

Viewers also liked (20)

Interrupts
Interrupts Interrupts
Interrupts
 
Interrupts
InterruptsInterrupts
Interrupts
 
Interrupts
InterruptsInterrupts
Interrupts
 
Interrupts
InterruptsInterrupts
Interrupts
 
Presentation on
Presentation onPresentation on
Presentation on
 
Fit unit 2 interrupts
Fit unit 2 interruptsFit unit 2 interrupts
Fit unit 2 interrupts
 
63071507 interrupts-up
63071507 interrupts-up63071507 interrupts-up
63071507 interrupts-up
 
System quality attributes
System quality attributes System quality attributes
System quality attributes
 
37471656 interrupts
37471656 interrupts37471656 interrupts
37471656 interrupts
 
Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)Mouse interrupts (Assembly Language & C)
Mouse interrupts (Assembly Language & C)
 
Producers and Consumers
Producers and ConsumersProducers and Consumers
Producers and Consumers
 
Producers, Consumers, Decomposers
Producers, Consumers, DecomposersProducers, Consumers, Decomposers
Producers, Consumers, Decomposers
 
Interrupt
InterruptInterrupt
Interrupt
 
Interrupts
InterruptsInterrupts
Interrupts
 
Interrupt
InterruptInterrupt
Interrupt
 
Interrupt
InterruptInterrupt
Interrupt
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Interrupts
InterruptsInterrupts
Interrupts
 
OS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and MonitorsOS Process Synchronization, semaphore and Monitors
OS Process Synchronization, semaphore and Monitors
 

Similar to Handling Interrupts in PIC18F MCUs

unit 3 a.pptxppppppppppppppppppppppppppp
unit 3 a.pptxpppppppppppppppppppppppppppunit 3 a.pptxppppppppppppppppppppppppppp
unit 3 a.pptxpppppppppppppppppppppppppppsachin397946
 
Interrupt programming
Interrupt programming Interrupt programming
Interrupt programming vijaydeepakg
 
Interrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptxInterrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptxSujalKumar73
 
Interrupts programming in embedded C using 8051
Interrupts programming in embedded C using 8051Interrupts programming in embedded C using 8051
Interrupts programming in embedded C using 8051Vikas Dongre
 
Micro controller 8051 Interrupts
Micro controller 8051 InterruptsMicro controller 8051 Interrupts
Micro controller 8051 Interruptsdharmesh nakum
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxnaveen088888
 
Lecture on PIC-1.pptx
Lecture on PIC-1.pptxLecture on PIC-1.pptx
Lecture on PIC-1.pptxgodfrey35
 
subroutines and interrupts
subroutines and interruptssubroutines and interrupts
subroutines and interruptsManoj Chowdary
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxRAJEEVKUMARYADAV11
 
03- introduction in Interrupts AndTimers.ppt
03- introduction in Interrupts AndTimers.ppt03- introduction in Interrupts AndTimers.ppt
03- introduction in Interrupts AndTimers.pptssuser3cbb4c
 
moving message display of lcd
 moving message display of lcd moving message display of lcd
moving message display of lcdabhishek upadhyay
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming IIOmar Sanchez
 

Similar to Handling Interrupts in PIC18F MCUs (20)

ES-CH6.ppt
ES-CH6.pptES-CH6.ppt
ES-CH6.ppt
 
unit 3 a.pptxppppppppppppppppppppppppppp
unit 3 a.pptxpppppppppppppppppppppppppppunit 3 a.pptxppppppppppppppppppppppppppp
unit 3 a.pptxppppppppppppppppppppppppppp
 
8051 interrupts
8051 interrupts8051 interrupts
8051 interrupts
 
Mc module5 ppt_msj
Mc module5 ppt_msjMc module5 ppt_msj
Mc module5 ppt_msj
 
Interrupt programming
Interrupt programming Interrupt programming
Interrupt programming
 
Interrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptxInterrupt in ATMEGA328P.pptx
Interrupt in ATMEGA328P.pptx
 
Interrupts programming in embedded C using 8051
Interrupts programming in embedded C using 8051Interrupts programming in embedded C using 8051
Interrupts programming in embedded C using 8051
 
Micro controller 8051 Interrupts
Micro controller 8051 InterruptsMicro controller 8051 Interrupts
Micro controller 8051 Interrupts
 
DPA
DPADPA
DPA
 
Unit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptxUnit 3 timer and counter and there application .pptx
Unit 3 timer and counter and there application .pptx
 
Embedded systems, lesson 16
Embedded systems, lesson 16Embedded systems, lesson 16
Embedded systems, lesson 16
 
Lecture on PIC-1.pptx
Lecture on PIC-1.pptxLecture on PIC-1.pptx
Lecture on PIC-1.pptx
 
subroutines and interrupts
subroutines and interruptssubroutines and interrupts
subroutines and interrupts
 
Timers and Endge-aligned PWM
Timers and Endge-aligned PWMTimers and Endge-aligned PWM
Timers and Endge-aligned PWM
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
 
Introduction to PIC.pptx
Introduction to PIC.pptxIntroduction to PIC.pptx
Introduction to PIC.pptx
 
03- introduction in Interrupts AndTimers.ppt
03- introduction in Interrupts AndTimers.ppt03- introduction in Interrupts AndTimers.ppt
03- introduction in Interrupts AndTimers.ppt
 
moving message display of lcd
 moving message display of lcd moving message display of lcd
moving message display of lcd
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 
Assembly programming II
Assembly programming IIAssembly programming II
Assembly programming II
 

More from Corrado Santoro

Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Corrado Santoro
 
Pulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsPulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsCorrado Santoro
 
Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Corrado Santoro
 
Presentation @ WOA 2015
Presentation @ WOA 2015 Presentation @ WOA 2015
Presentation @ WOA 2015 Corrado Santoro
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scriptingCorrado Santoro
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converterCorrado Santoro
 
Introduction to microcontrollers
Introduction to microcontrollersIntroduction to microcontrollers
Introduction to microcontrollersCorrado Santoro
 
How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...Corrado Santoro
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous RobotsCorrado Santoro
 
Reactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolReactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolCorrado Santoro
 

More from Corrado Santoro (13)

Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
Physical Flying Agents: Ummanned Aerial Vehicles Control, Coordination and Em...
 
The I2C Interface
The I2C InterfaceThe I2C Interface
The I2C Interface
 
Pulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUsPulse Width Modulation Signal Generation with MCUs
Pulse Width Modulation Signal Generation with MCUs
 
Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015Presentation @ Miniscuola WOA 2015
Presentation @ Miniscuola WOA 2015
 
Presentation @ WOA 2015
Presentation @ WOA 2015 Presentation @ WOA 2015
Presentation @ WOA 2015
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
Introduction to shell scripting
Introduction to shell scriptingIntroduction to shell scripting
Introduction to shell scripting
 
Pillole di C++
Pillole di C++Pillole di C++
Pillole di C++
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converter
 
Introduction to microcontrollers
Introduction to microcontrollersIntroduction to microcontrollers
Introduction to microcontrollers
 
How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...How does a Quadrotor fly? A journey from physics, mathematics, control system...
How does a Quadrotor fly? A journey from physics, mathematics, control system...
 
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
Integrating Cloud Services in Behaviour  Programming for Autonomous RobotsIntegrating Cloud Services in Behaviour  Programming for Autonomous Robots
Integrating Cloud Services in Behaviour Programming for Autonomous Robots
 
Reactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA toolReactive Autonomous System Programming using the PROFETA tool
Reactive Autonomous System Programming using the PROFETA tool
 

Recently uploaded

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Handling Interrupts in PIC18F MCUs

  • 1. Handling Interrupts in Microchip PIC18F Microcontrollers Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro Interrupts in PIC18F Family
  • 2. Interrupts in MCUs The core of MCUs manages interrupts, as in normal CPUs In a MCU, each peripheral event can generate an interrupt, e.g.: The overflow of a timer; The reception/trasmission of a byte through the UART (serial port); The end of conversion of the ADC; ... The peripheral is called interrupt source When an interrupt is generated, the normal program flow is interrupted, a specific function is invoked, called ISR - Interrupt Service Routine; at the end, the normal program flow is resumed. Corrado Santoro Interrupts in PIC18F Family
  • 3. Interrupts in Microchip MCUs Each peripheral which can generate interrupt has two control bits: xxxIF, the interrupt flag, it is set to ‘‘1’’, by the hardware, when the “event” occurs; it must be reset by the software; xxxIE, the interrupt enable bit; when set to ‘‘1’’ (by the software) the “event”, when occurs, generates a CPU interrupt Moreover, there are other bits which control the interrupt circuit: GIE, the global interrupt enable flag; when set to ‘‘1’’, interrupt sources are routed to the CPU; IPEN, the interrupt priorities enable flag; when set to ‘‘1’’, two priorities are handled low and high/urgent; PEIE, the peripheral interrupt enable flag; when set to ‘‘1’’, interrupt sources from peripherals are enabled. Corrado Santoro Interrupts in PIC18F Family
  • 4. The Interrupt Circuit Corrado Santoro Interrupts in PIC18F Family
  • 5. Enabling Interrupts in Microchip MCUs (No priorities) 1 Program the peripheral according to its working mode; 2 Reset the peripheral interrupt flag xxxIF = 0;; 3 Set the peripheral interrupt enable flag xxxIE = 1;; 4 Disable priorities handling RCONbits.IPEN = 0;; 5 Enable global interupt flag INTbits.GIE = 1;; 6 Enable peripheral interupt flag INTbits.PEIE = 1;; Corrado Santoro Interrupts in PIC18F Family
  • 6. Enabling Interrupts in Microchip MCUs (With priorities) 1 Program the peripheral according to its working mode; 2 Reset the peripheral interrupt flag xxxIF = 0;; 3 Set the peripheral interrupt enable flag xxxIE = 1;; 4 Set the peripheral interrupt priority flag (0 = low, 1 = high) xxxIP = yy; 5 Enable priorities handling RCONbits.IPEN = 1;; 6 Enable/disable high interupts INTbits.GIEH = yy;; 7 Enable/disable low priority interupts INTbits.GIEL = yy;; Corrado Santoro Interrupts in PIC18F Family
  • 7. Handling Interrupts in Microchip MCUs Define a C function marked as interrupt; Check if the peripheral interrupt flag is on; Serve the peripheral interrupt; Reset the peripheral interrupt flag. Handling TMR0 interrupt ... void interrupt isr() { if (INTCONbits.T0IF == 1) { // ... handle the TMR interrupt INTCONbits.T0IF = 0; } } ... Corrado Santoro Interrupts in PIC18F Family
  • 8. Handling Several Interrupts in Microchip MCUs Handling TMR0 & TMR1 interrupts ... void interrupt isr() { if (INTCONbits.TMR0IF == 1) { // ... handle the TMR0 interrupt INTCONbits.TMR0IF = 0; } if (PIR1bits.TMR1IF == 1) { // ... handle the TMR1 interrupt PIR1bits.TMR1IF = 0; } } ... Corrado Santoro Interrupts in PIC18F Family
  • 9. Example A LED RB0 A pushbuttons RA3 Pressing pushbutton starts/stops flashing at a period of 200ms Corrado Santoro Interrupts in PIC18F Family
  • 10. Let’s determine timer setup We want to use the system clock, T0CS = 0;; We have FOSC = 64MHz, therefore the basic frequency is FOSC/4 = 16MHz, the P = 62.5ns; Let’s use the prescaler and divide the frequency by 256, so PSA = 0; T0PS = 0b111; The timer increments using a period P = 62.5ns ∗ 256 = 16µs. So 200ms/16µs = 12500 counts, therefore the TMR0 setup value is −12500. Corrado Santoro Interrupts in PIC18F Family
  • 11. Let’s determine timer setup Timer Setup ... T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 (’0b’ is a prefix for binary) TMR0 = -12500; // setup initial timer value INTCONbits.T0IF = 0; // reset timer interrupt flag INTCONbits.T0IE = 1; // enable timer interrupts RCONbits.IPEN = 0; // do not use priorities INTCONbits.PEIE = 1; // enable peripheral interrupts INTCONbits.GIE = 1; // enable interrupts globally ... Corrado Santoro Interrupts in PIC18F Family
  • 12. Let’s handle interrupts Timer Interrupt Handling ... void interrupt isr() { if (INTCONbits.T0IF == 1) { TMR0 = -12500; // reload timer value // invert the LED LATBbits.LATB0 = !LATBbits.LATB0; INTCONbits.T0IF = 0; } } ... Corrado Santoro Interrupts in PIC18F Family
  • 13. Let’s handle timer on/off Timer on/off ... for (;;) { // loop forever while (PORTAbits.RA3 == 0) {}; // if the push button is DOWN, wait while (PORTAbits.RA3 == 1) {}; // if the push button is UP, wait // transition got, let’s invert the TMRON flag T0CONbits.TMR0ON = !T0CONbits.TMR0ON; } } Corrado Santoro Interrupts in PIC18F Family
  • 14. Handling Interrupts in Microchip PIC18F Microcontrollers Corrado Santoro ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Universit`a di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course Corrado Santoro Interrupts in PIC18F Family