2. Interrupts , IRQs , ISRs
• Interrupt : “An interrupt is a signal sent to the CPU which indicates
that a system event has a occurred which needs immediate
attention“.
• Interrupt ReQuest (IRQ) can be thought of as a special request to
the CPU to execute a function(small piece of code) when an
interrupt occurs.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
interrupt occurs.
• ISR : This function or ‘small piece of code’ is technically called an
‘Interrupt Service Routine‘ or ‘ISR‘.
• So when an IRQ arrives to the CPU , it stops executing the code
current code and start executing the ISR. After the ISR execution
has finished the CPU gets back to where it had stopped.
3. How We classify them ?
We Classify them as 2 types :
• Fast IRQs or FIQs
• Normal IRQs or IRQs which can be further
classified as :
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
– Vectored IRQ
– Non-Vectored IRQ.
FIQ
Normal IRQ
Vectored IRQ
Non Vectored IRQ
Interrupts
4. Types of Interrupts in LPC2148
Interrupts are Handled by Vectored Interrupt Controller(VIC)
Types of Interrupts in LPC2148
• Fast Interrupt Request i.e FIQ : which has highest priority
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
• Fast Interrupt Request i.e FIQ : which has highest priority
• Vectored Interrupt Request i.e Vectored IRQ : which has
‘middle’ or priority between FIQ and Non-Vectored IRQ.
• Non-Vectored IRQ : which has the lowest priority.
5. What does Vectored mean ?
• ‘Vectored‘ means that the CPU is aware of the address of
the ISR when the interrupt occurs
• Non-Vectored means that CPU doesn’t know the address
of the ISR (nor) the source of the IRQ when the interrupt
occurs.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
• For Non – Vectored interrupts , CPU needs to be supplied
by the ISR address.
• For the Vectored interrupts , the System internally
maintains a table called IVT or Interrupt Vector Table which
contains the information about Interrupts sources and their
corresponding ISR address.
6. How Non-Vectored Interrupts are
handled?
• Non-Vectored ISRs doesn’t point to a unique ISR
• The CPU needs to be supplied with the address of the
‘default’ or a ‘common’ ISR that needs to be executed
when the interrupt occurs.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
when the interrupt occurs.
• In LPC2148 this is facilitated by a register called
‘VICDefVectAddr‘.
• The user must assign the address of the default ISR to
this register for handling Non-Vectored IRQs.
7. In a Nut Shell
• Vectored IRQ(VIRQ) has dedicated IRQ service
routine for each interrupt source
• Non-Vectored IRQ(NVIRQ) has the same IRQ
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
• Non-Vectored IRQ(NVIRQ) has the same IRQ
service routine for all Non-Vectored
Interrupts.
8. How Many Possible Interrupt Sources are there ?
• There are 22 Interrupt Sources in LPC2148
• But there are only 16 Slots in in the Vectored
Interrupt Controller (VIC)
0 to 15.
• These 22 possible sources have to be shared by using
Slots 0 to 15 of VIC
• Slot 0
Highest Priority
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
Slot 0 Highest Priority
• Slot 15
Lowest Priority
9. SFRs Involved
• VICIntSelect (R/W) 0 = IRQ, 1 = FIQ
• VICIntEnable (R/W) Enable Selective Interrupt Source
• VICIntEnClr (R/W) Disable Selective Interrupt Source
• VICIRQStatus (R) to know the status of enabled interrupt
• VICFIQStatus (R) to know the status of enabled FIQ
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
• VICFIQStatus (R) to know the status of enabled FIQ
• VICSoftInt to trigger a software interrupt
• VICSoftIntClear to clear software interrupt
• VICVectCntl0 to VICVectCntl15 Assign interrupt source
• VICVectAddr0 to VICVectAddr15 Assign interrupt address
• VICVectAddr Holds the address of currently active interrupt
• VICDefVectAddr Holds the addressof Non-Vectored ISR
10. VICIntSelect (R/W)
• This register is used to select an interrupt as IRQ or as
FIQ.
• Writing a 0 at a given bit location will make the
corresponding interrupt as IRQ
• Writing a 1 will make it FIQ.
• For e.g if you make Bit 4 as 0 then the corresponding
interrupt source i.e TIMER0 will be IRQ else if you make
Bit 4 as 1 it will be FIQ instead.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
Bit 4 as 1 it will be FIQ instead.
• By default all interrupts are selected as IRQ. Note that
here IRQ applies for both Vectored and Non-Vectored
IRQs.
11. VICIntEnable (R/W)
• This is used to enable interrupts.
• Writing a 1 at a given bit location will make
the corresponding interrupt Enabled.
• If this register is read then 1′s will indicated
enabled interrupts and 0′s as disabled
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
enabled interrupts and 0′s as disabled
interrupts.
• Writing 0′s has no effect.
12. VICIntEnClr (R/W)
• This register is used to disable interrupts.
• This is similar to VICIntEnable expect writing a
1 here will disabled the corresponding
Interrupt.
• This has an effect on VICIntEnable since
writing at bit given location will clear the
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
writing at bit given location will clear the
corresponding bit in the VICIntEnable Register.
• Writing 0′s has no effect
13. VICIRQStatus (R)
• This register is used for reading the current
status of the enabled IRQ interrupts.
• If a bit location is read as 1 then it means that
the corresponding interrupt is enabled and
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
the corresponding interrupt is enabled and
active.
14. VICFIQStatus (R)
• This register is used for reading the current
status of the enabled FIQ interrupts.
• If a bit location is read as 1 then it means that
the corresponding interrupt is enabled and
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
the corresponding interrupt is enabled and
active.
15. VICSoftInt
• This register is used to generate interrupts
using software i.e manually generating
interrupts using code
• If you write a 1 at any bit location then the
correspoding interrupt is triggered i.e. it forces
the interrupt to occur.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
the interrupt to occur.
• Writing 0 here has no effect.
16. VICSoftIntClear
• This register is used to clear the interrupt
request that was triggered(forced) using
VICSoftInt.
• Writing a 1 will release(or clear) the forcing of
the corresponding interrupt.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
17. VICVectCntl0 to VICVectCntl15
• These are the Vector Control registers.
• These are used to assign a particular interrupt source
to a particular slot.
• As mentioned before slot 0 i.e VICVectCntl0 has highest
priority and VICVectCntl15 has the lowest.
• Each of this registers can be divided into 3 parts : {Bit0
to bit4} , {Bit 5} , {and rest of the bits}.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
to bit4} , {Bit 5} , {and rest of the bits}.
• The first 5 bits i.e Bit 0 to Bit 4 contain the number of
the interrupt request which is assigned to this slot. The
interrupt source numbers are given in the table below :
• The 5th bit is used to enable the vectored IRQ slot by
writing a 1 31 . . . 6 EN N4 N3 N2 N1 N0
18. Important Note
• Note that if the vectored IRQ slot is disabled it will not disable
the interrupt but will change the corresponding interrupt to
Non-Vectored IRQ.
• Enabling the slot here means that it can generate the address
of the ‘dedicated Interrupt handling function (ISR)’
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
• Disabling it will generate the address of the ‘common/default
Interrupt handling function (ISR)’ which is for Non-Vectored
ISR.
• In simple words if the slot is enabled it points to ‘specific and
dedicated interrupt handling function’ and if its disable it will
point to the ‘default function’.
19. VICVectAddr0 to VICVectAddr15
(16 registers in all)
• For Vectored IRQs these register store the
address of the function that must be called
when an interrupt occurs.
• Note – If you assign slot 3 for TIMER0 IRQ then
care must be taken that you assign the
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
care must be taken that you assign the
address of the interrupt function to
corresponding address register .. i.e
VICVectAddr3 in this example.
20. VICVectAddr
• This must not be confused with the above set of
16 VICVecAddrX registers.
• When an interrupt is Triggered this register holds
the address of the associated ISR i.e the one
which is currently active.
• Writing a value i.e dummy write to this register
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
• Writing a value i.e dummy write to this register
indicates to the VIC that current Interrupt has
finished execution.
• The only place we’ll use this register .. is at
the end of the ISR to signal end of ISR execution.
21. VICDefVectAddr
• This register stores the address of the
“default/common” ISR that must be called
when a Non-Vectored IRQ occurs.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
22. How to Write an ISR
Method – 1
__irq void myISR (void)
{
...
}
Method – 2
void myISR (void) __irq
{
...
}
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
For 8051
void myISR (void) interrupt 1
{
...
}
23. A Simple 3 Step Process to Enable a
Vectored IRQ
• Step – 1 : Enable the IRQ by setting the
appropriate bit of VICIntEnable to ’1′.
• Step-2 : Identify the interrupt source number
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
• Step-2 : Identify the interrupt source number
and assign it to VICVectCntlX.
• Step-3 : Assign the address of the related ISR
to VICVectAddrX.
24. Example – Enabling Timer0 Interrupt
• First we need to enable the TIMER0 IRQ itself!
Hence , from Table we get the bit number
to Enable TIMER0 Interrupt which is Bit number
4. Hence we must make bit 4 in VICIntEnable to
’1′.
• Next , from Table we get the interrupt source
number for TIMER0 which is decimal 4 and OR it
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
number for TIMER0 which is decimal 4 and OR it
with (15) [i.e 5th bit=1 which enables the slot]
and assign it to VICVectCntlX.
• Next assign the address of the related ISR to
VICVectAddrX.
25. Template Code
VICIntEnable |= (1Y) ;
VICVectCntlX = (15) | Y ;
VICVectAddrX = (unsigned) myISR;
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
26. Assigning TIMER0 Interrupt to Slot0
VICIntEnable |= (14) ; // Enable TIMER0 IRQ
VICVectCntl0 = (15) | 4 ; //5th bit must 1 to
enable the slot
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
enable the slot
VICVectAddr0 = (unsigned) myISR;
//Vectored-IRQ for TIMER0 has been configured
27. ISR
• First when we have only one ‘internal’ source of interrupt in
TIMER0 i.e an MR0 match event which raises an IRQ.
__irq void myISR(void)
{
long int regVal;
regVal = T0IR; // read the current value in T0's Interrupt
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
regVal = T0IR; // read the current value in T0's Interrupt
Register
//... MR0 match event has occured .. do something here
T0IR = regval; // write back to clear the interrupt flag
VICVectAddr = 0x0; // The ISR has finished!
}
28. Important Note
• Each Peripheral in lpc2148 has only 1 IRQ associated with it.
• But inside each device there may be different sources which can raise
an interrupt
• Like the TIMER0 peripheral has 4 match + 4 capture registers and any
one or more of them can be configured to trigger an interrupt.
• Hence such devices have a dedicated interrupt register which contains
a flag bit for each of these source(For Timer block its ‘T0IR‘).
• So , when the ISR is called first we need to identify the actual source of
the interrupt using the Interrupt Register and then proceed
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
the interrupt using the Interrupt Register and then proceed
accordingly.
• Also just before , when the main ISR code is finished we also need to
acknowledge or signal that the ISR has finished executing for the
current IRQ which triggered it.
• This is done by clearing the flag(i.e the particular bit) in the device’s
interrupt register and then by writing a zero to VICVectAddr register
which signifies that interrupt has ISR has finished execution
successfully.
35. Problem-1
• Design a LPC2148 based system to perform
the following tasks.
• Task1 Blink an LED at P1.31 using software
delay.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
delay.
• Task2 Generate a square wave at 1KHz @
P1.25 using Timer0 Match Interrupt
37. void initInterrupt(void)
{
VICVectCntl0 = (0x01 5) | 4 ;
//(i.e bit5 = 1) - to enable Vectored
IRQ slot
VICVectAddr0 = (unsigned) T0ISR;
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
VICVectAddr0 = (unsigned) T0ISR;
//Pointer Interrupt Function (ISR)
VICIntEnable = 0x01 4; //Enable
timer0 int
}
38. void initTimer0(void)
{
T0PR = 60-1;
// Pclk = 60MHz, ft = 1MHz ,
// Div = 60x10^6/1x10^6 = 60
T0CTCR = 0x00; // Configure as Timer
T0TCR = 0x02; // Clear TC and PC
T0MR0 = 500-1; // 500us
Method -1
Reset Timer after Match
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
T0MR0 = 500-1; // 500us
T0MCR |= 0x03 ; // Reset after Match
//Set bit0 Bit1 to High which is
//Interrupt on MR0, RESET ON MR0
T0TCR = 0x01; // Enable TC
}
39. void T0ISR(void) __irq
{
long int temp;
temp = T0IR ;
if (temp 0x01) // MR0 Interrupt
{
Squarewave = ~( Squarewave);
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
Squarewave = ~( Squarewave);
writepin(25, Squarewave);
}
T0IR = temp; // Clear T0IR
VICVectAddr = 0x00; // Dummy Write
}
40. void initTimer0(void)
{
T0PR = 60-1;
// Pclk = 60MHz, ft = 1MHz ,
// Div = 60x10^6/1x10^6 = 60
T0CTCR = 0x00; // Configure as Timer
T0TCR = 0x02; // Clear TC and PC
T0MR0 = 500-1; // 500us
Method -2
Don’t Reset Timer after Match
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
T0MR0 = 500-1; // 500us
T0MCR |= 0x01 ; //No Reset.
//Set bit0 Bit1 to High which is
//Interrupt on MR0,
T0TCR = 0x01; // Enable TC
}
41. void T0ISR(void) __irq
{
long int temp;
temp = T0IR ;
if (temp 0x01) // MR0 Interrupt
{
T0MR0 = T0MR0 + 500;// Increment!
Squarewave = ~( Squarewave);
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
Squarewave = ~( Squarewave);
writepin(25, Squarewave);
}
T0IR = temp; // Clear T0IR
VICVectAddr = 0x00; // Dummy Write
}
42. #includelpc214x.h
#include GPIO.h
#include timer.h
#include UART.h
#include timerinterrupt.h
int main(void)
{
initPLL(); // 60 MHz Pclk
initInterrupt();
initTimer0();
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
while(1)
{
writepin(31,1); // Background Task
delay();
writepin(31,0);
delay();
}
}
43. Problem-2
• Design a LPC2148 based system to perform the
following tasks.
• Task1 Blink an LED at P1.31 using software delay.
• Task2 Generate a square wave at 500Hz @ P1.25
using Timer0 Match Interrupt
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
using Timer0 Match Interrupt
• Task3 Generate a square wave at 1KHz @ P1.26
using Timer0 Match Interrupt
• Task4 Generate a square wave at 2KHz @ P1.27
using Timer0 Match Interrupt
• Task5 Generate a square wave at 4KHz @ P1.28
using Timer0 Match Interrupt
49. External Interrupt Mode register
(EXTMODE)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
50. External Interrupt Polarity register
(EXTPOLAR)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
51. External Interrupt Flag register
(EXTINT)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
52. Pin function Select register 1
(PINSEL1)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
53. Problem-3
• Design a LPC2148 based system to perform
the following tasks.
• Task1 Blink an LED at P1.31 using software
delay.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
delay.
• Task2 Read a switch @ EINT0 and Turn a
Load @ P1.25 when count exceeds 10.
56. void INT0ISR(void) __irq
{
long int temp;
temp = EXTINT ;
if (temp 0x01) // EINT0
{
if (mycount 10)
writepin(25,1); // Load ON
else
{
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
{
mycount ++;
writepin(25,0); // Load OFF
}
}
EXTINT = temp; // clear interrupt
VICVectAddr = 0x00;
}
57. #includelpc214x.h
#include GPIO.h
#include timer.h
#include UART.h
#include extinterrupt.h
int main(void)
{
initPLL(); // 60 MHz Pclk
initInterrupt();
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
while(1)
{
writepin(31,1);
delay();
writepin(31,0);
delay();
}
}
58. Problem-4
• Design a LPC2148 based system to perform
the following tasks.
• Task1 Blink an LED at P1.31 using software
delay.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
delay.
• Task2 Read a switch @ EINT0 and Turn a
Load @ P1.25 when count exceeds 10.
• Task3 Read a switch @ EINT1 and Toggle a
Load @ P1.26
65. Problem-5
• Design a LPC2148 based system to perform the
following tasks.
• Task1 Blink an LED at P1.31 using software delay.
• Task2 Receive commands from PC at 9600 Baud and
control 2 Loads for the following commands.
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
control 2 Loads for the following commands.
• A Load1 ON
• B Load 1 Off
• C Load 2 ON
• D Load 2 OFF
66. Pin Function Select Register 0
(PINSEL0)
01
01
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
67. UART0 Line Control Register
(U0LCR)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
68. UART0 Line Status Register (U0LSR)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
69. UART0 Line Status Register (U0LSR)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC
70. UART0 Interrupt Enable Register(U0IER)
Dr.R.Sundaramurthy.,M.E.,Ph.D., sundar@pec.edu
Department of EIE / PEC