CEG2400 chapter 8 - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

CEG2400 chapter 8

Description:

chapter 8: Timer and external interrupts v93b. 3. A) To demonstrate timer ... chapter 8: Timer and external interrupts v93b. 4. Learn to use timer and interrupt ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 74
Provided by: khw9
Category:
Tags: ceg2400 | chapter

less

Transcript and Presenter's Notes

Title: CEG2400 chapter 8


1
CEG2400chapter 8
  • Timer and external interrupts--
  • EintDemo.c
  • (Also see slide notes)
  • 1 http//www.nxp.com/acrobat_download/usermanual
    s/UM10120_1.pdf

2
Overview
  • Part A) To demonstrate timer interrupt EintDemo.c
  • A1) The main program
  • A2) Initialize timer and interrupt
  • A3) The interrupt service routine
  • Part B) To demonstrate multiple
    interruptsEintDemo.c

3
A) To demonstrate timer interrupt
EintDemo.cLED blinking
  • Important parts
  • main(),
  • init_timer_Eint () //init timer
  • __IRQ exception() //timer interrupt, blink
    red-led

4
Learn to use timer and interrupt
  • A timer sends out interrupt requests regularly

Timer0 Interrupt the CPU

LPC213x
5
What is a timer?
  • Like an alarm clock
  • After programmed, it sends out regular signals to
    interrupt the Central Processing Unit (CPU).
  • How to program the system?
  • Set frequency of timer
  • Set the MCU to receive interrupt from timer.

6
What is a binary counter?
  • Time Q0 Q1 Q2 Q3
  • 0 0000
  • 1 0001
  • 2 0010
  • 3 0011
  • 4 0100
  • 5 0101
  • 6 0110
  • 7 0111
  • 8 1000
  • 9 1001
  • 1010
  • 11 1011
  • 12 1100
  • 13 1101
  • 14 1110
  • 15 1111
  • 0000
  • 0010
  • Example
  • a 4-bit counter, output changes at each rise
    edge of clock
  • A 32-bit counter has Q0-Q31 (32 outputs)

clock
4 output Q0 Q1 Q2 Q3
Time 1 2 3
7
Example 4-bit Asyn. Clock CounterPlot count,
and check delayFFD-type flip flop

Count(0)
Count(1)
Count(2)
Count(3)
D(3)
D(0)
D(1)
D(2)
FF
FF
FF
FF
clock
ck
ck
ck
Q(0)
Q(2)
Q(3)
ck
Q(1)
reset
clock
Q(0) Q(1) Q(2) Q(3)
8
Concept of a timer/counter How does a timer send
out periodic signals?
  • The timer/counter increments at a rate of
    13.834MHz,
  • When the output matches T0MR0 , an output pulse
    is generated , the timer/counter is reset to 0
    and start counting again.
  • What is the frequency of the output?
  • ANSWER 1KHZ, because
  • TC input clock is 13.824MHz, it counts 1,2,3, to
    13824 in 1ms
  • So TC_outputT0MR013824 , then an output pulse
    is sent, TC resets and counts again

9
How to use the timer?
  • Like an alarm clock
  • After programmed, it sends out regular signals to
    interrupt the Central Processing Unit (CPU).
  • How to program the system?
  • Set frequency of timer
  • Set the MCU to receive interrupt from timer.

10
Where is the timer?
  • The timer
  • Is inside
  • LPC213x

11
We learned before, the ARM_LPC213x has an
PCLK13.842MHz for peripheral devices

ARM-LPC213x
FOSCx5CCLK for MCU 55.296MHz
FOSC 11.0592MHz
CCLK/4 PCLK for peripherals 13.824MHz
The Clock for timer/counter
12
Part 1a of void init_timer_Eint() of EintDemo.c
( for init timer , use VICVectAddr0
------------------------------RECALL--------------
-----------------------
cclkMFosc, M5 pclkcclk/4 Pclk110592005/4 13.
824MHz
  • 144)/ Setup the Timer Counter 0 Interrupt /
  • 145)void init_timer_Eint (void)
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400 //set interrupt rate
    10Hz,
  • 148) // Pclk/10Hz (13.824MHz)/
    10
  • 149) T0MCR 3 // Interrupt and
    Reset on MR0
  • 150) T0TCR 1 // Timer0 Enable
  • 151) VICVectAddr0 (unsigned
    long)IRQ_Exception
  • 152) VICVectCntl0 0x20 4 // use it for
    Timer 0 Interrupt
  • 153) VICIntEnable 0x00000010 // Enable
    Timer0 Interrupt
  • 154)
  • Setup intyerrupt vetcor 0
  • (This becomes the highest
  • priory interrupt)

Usually You only need to change lines in
the Boxes for your application
13
Part 1b of void init_timer_Eint() of EINT.c (
for init timer , use VICVectAddr0
------------------------------RECALL--------------
-----------------------
cclkMFosc, M5 pclkcclk/4 Pclk110592005/4 13
.824MHz
  • 144)/ Setup the Timer Counter 0 Interrupt /
  • 145)void init_timer_Eint (void)
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400 //set interrupt rate
    10Hz,
  • 148) // Pclk/10Hz (13.824MHz)/
    10
  • 149) T0MCR 3 // Interrupt and
    Reset on MR0
  • 150) T0TCR 1 // Timer0 Enable
  • 151) VICVectAddr0 (unsigned
    long)IRQ_Exception
  • 152) VICVectCntl0 0x20 4 // use it for
    Timer 0 Interrupt
  • 153) VICIntEnable 0x00000010 // Enable
    Timer0 Interrupt
  • 154)
  • This stes which the
  • interrupt service routine
  • for timer0, is
  • IRQ Exception()

This sets the frequency of the interrupt timer
counter 10Hz
14
How about you need another frequency, say 1KHz
interrupt rate?Example of a 1KHzfreq_out
interrupt generator
  • PCLK /freq_out PCLK/1K(11059200 x 5)/(4
    )13.824 MHz/100013824
  • When timer counter (TC)match reg0 (T0MR0), an
    interrupt is generated

Match reg0 (MR0) T0MR0 13824
Timer Counter TC
Divided by (pre-scale1) Since pre-scale T0PR
0 So divided by 1

PCLK Or an input pin CAPx.y (See pin assignment
of lpc2131)
Freq_out PCLK/T0MR0 Interrupt request or
output pin (MATx.y) (1KHz, every 1ms)
15
Little summary
  • T0MCR Frequency of timer interrupt
  • 1382400 10Hz
  • 13824 1000Hz

In our lab 10Hz is used
16
Why do we use timer interrupt?Example of
blinking an LED
  • //Software delay loop method
  • Main()
  • For ()
  • On_LED
  • delay_100ms_loop()
  • Off_LED
  • delay_100ms_loop()
  • Problems
  • Delay not accurate, because different interrupts
    (UART, TIMER..etc) may occur in between
    statements
  • Exact delay is difficult o calculate, because
  • delay_100ms_loop()
  • for (i0 iltx i)
  • for (j0 jlt 1000 j)
  • some instructions depends on how you
    write them
  • //difficult to estimate x to make delay 100ms
  • Solution muse timer interrupt

17
What is timer interrupt? _isr( )Interrupt
Service Routine
Inside LPC213x
Match reg0 (MR0) T0MR0 1382400
Timer CounterTC
  • Main ()
  • Doing something


PCLK 13.824MHz
reset
TIMER_OUTPUT An output pulse will be generated
when TCT0MR0 match
10Hz
At each rising edge of TIMER_OUTPUT ISR( )
executes once. So _ISR( ) executes 10 times per
second _isr( )//Interrupt service routine ..
some tasks //when finished, //goes back to main
18
The main ideas
  • The timer is hardware inside ARM_LPC213x
  • The timer generates a 10Hz clock at TIMER_OUTPUT
  • At each rising edge of TIMER_OUTPUT
  • , the interrupt service routine _ISR() is
    executed once
  • So the _ISR is being executed 10 times per second

19
The CPU runs instructions of main() and ISR()
sequentially.--M1,M2.. running times are
different, so it is not predictable when in the
main(), interrupt will occur.-- But ISR
(I1,I2,I3) will be executed once in every 100ms
  • ISR() //interrupt service routine 10Hz
  • I1
  • I2
  • I3
  • Main()
  • setup()
  • M1
  • M2
  • M3
  • M4
  • M5
  • M6
  • M7
  • M8
  • M9
  • M10
  • M11
  • M12
  • M13
  • CPU runs instructions sequentially
  • Setup()
  • M1
  • M2
  • M3
  • I1
  • I2
  • I3
  • M4
  • M5
  • I1
  • I2
  • I3
  • M6
  • M7
  • M8
  • M9
  • I1

Executes ISR in every 100ms
100ms
100ms
100ms
20
SOFTWARE HARDWARETime delay vs timer
interrupt method
  • Hardware timer interrupt method
  • Blink Frequency actuate
  • Software Delay method
  • Blink Frequency not accurate

Main( ) Setup( ) while(1) do Something
  • //Delay loop method
  • Main()
  • For ()
  • On_LED
  • delay_100ms_loop()
  • Off_LED
  • delay_100ms_loop()

//Timer interrupt rate 10Hz ISR( )
IRQ_Exception() blink LED
Timer0 Interrupt the MCU
Accurate delay loops are difficult to implement
using software delay
LPC2131
21
Explanation
  • You may use delay loop to implement an LED
    blinking program, but how do you write the
    delay_10ms_loop in the following code?
  • delay_100ms_loop()
  • for (i0 iltx i)
  • for (j0 jlt 1000 j)
  • some instructions
  • //difficult to estimate x
  • Some instructions are used to consume time, they
    may include instructions like add, load, store
    etc. But it is very difficult to calculate the
    exact delay time. Because
  • some instructions may run faster or slower than
    others.
  • If you are using a new CPU that runs faster (e.g.
    the clock is not 11MHz but 22 MHz) the delay
    will be shorter, then you have to recalculate x
    again to make sure the delay loop occupies 100ms.
  • The solution is you use the timer, after
    programmed, the timer interrupts the CPU at a 10
    Hz frequency and the time is always correct. Even
    when you change the system clock the timer will
    still be interrupting the CPU at 10Hz and the
    blink frequency will not be changed.

22
Timer application Scheduler
  • In time sharing operating systems e.g. Windows,
    Unix
  • A simplified model
  • 1KHz interrupt rate

process1
A scheduler
process3
process2
Proces1 Process2 Process3 Process1
Time (ms)
1 2 3
4
Interrupts and runs the interrupt service routine

http//en.wikipedia.org/wiki/Scheduling_(computing
)
23
Explanation
  • Another application is the scheduler of the
    operating system.
  • A scheduler is a program that feeds the CPU with
    a process at one time using a scheme so that
    processes are run in a fair and balanced manner.
  • The scheduler is a essential part of all
    time-sharing operating systems, such as Windows,
    Unix, MacOS.
  • For example you may see how the CPU is scheduled
    to run different processes using the
    XP-windows-task-manager.
  • In our example, a timer is programmed to make
    interrupt requests to the CPU at 1KHZ, so the
    first process will be run for 1 ms, and then the
    second for the next 1ms etc.
  • Nearly all operating systems use the timer to
    implement the scheduler in the core of the
    operating system called the kernel.

24
The experiment of blinking a red LED
Video

switch
Arm board
green led
red led
--We will show how to blink the red-led (Turn on
and off the red-led at a regular pattern)
25
Our testing board connector

26
2) The RED LED

Each LPC2131 can drive current IOH-4mA,
IOL4mA Use transistor to enhance output to drive
an LED.
27
Blink LED Interrupt Service Routine ISR( )the
concept
Main( ) Setup( )
  • A timer sends out interrupt requests regularly
    at 10Hz
  • ISR() runs once every 1/10 seconds
  • At each Interrupt Service Routine ISR
  • __irq IRQ_Exception()
  • change state of LED
  • so the LED blinks

ISR( ) IRQ_Exception() blink LED
Timer0 Interrupt the MCU
LPC2131
28
Blink LED Interrupt Service Routine ISRwith
program details
Timer0 set
Main() Init_IO_pins() init_serial_port()
void init_timer_Eint() Do something
while(1)

//Timer0 interrupt __irq IRQ_Exception()
Blink red-led
timer0
29
Explanation
  • The software has two parts
  • The main() program initializes the timer and
    interrupt
  • The interrupt service routine __irq() blinks the
    LED at 10HZ
  • After initialization the code in main() can do
    something else, like some calculations or idling
    by running an endless loop , such as while(1)
    .
  • The main() program is being interrupted at a
    regular basis, 10 Hz,
  • The __irq() toggles the state of the LED, turning
    it on or off at 10/2HZ
  • void __irq IRQ_Exception()
  • timeval
  • //Blink the Red LED
  • if((timeval2)0) IO0SETRED_LED //turn on
    LED if timeval is even
  • else IO0CLRRED_LED
    //turn on LED if timeval is odd
  • T0IR 1 //
    Clear interrupt flag
  • VICVectAddr 0 //
    Acknowledge Interrupt

30
Now let us study the programsA1) The main program
  • Main()

31
main(), init and do nothingserial port is
initialized but not used.
  • int main(void)
  • PINSEL0 0x00000005 // set p0.0 to TXD0, p0.1
    to RXD0 and the rest to GPIO
  • PINSEL0 0x000000C0 // set p0.3 as EINT1
    external interrupt input
  • Init_Serial_A() // Init COM port
  • init_timer_Eint() // Init TIMER 0 EINT1
  • NEWLINE
  • print("
    ") NEWLINE
  • print("

    ") NEWLINE
  • print(" CUHK Computer Science and
    Engineering Department ") NEWLINE
  • print(" LPC2138 ARM Board
    (2009)
    ") NEWLINE
  • print("
    ") NEWLINE
  • print(" External Interrupt
    Demo (2009) ") NEWLINE
  • print("
    ") NEWLINE
  • print("
    ") NEWLINE
  • NEWLINE
  • //set p0.8 and p0.9 as output
  • IO0DIRRED_LED
  • IO0DIRGREEN_LED
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

32
Pin assignment in main()
  • 89) main ()
  • 90) PINSEL0 0x00000005// set p0.0 to TXD0,
    p0.1 to RXD0 and rest to GPIO, so p0.8 , p0.9 are
    outputs, see the circuit diagram
  • 91) PINSEL0 0x000000C0// set p0.3 as EINT1
    external interrupt input(pin26)
  • ..

0xc01100 0000, so bit 7,611b
33
Explanation
  • Line 90,91 setup the hardware pin , so the MCU
    knows that they are used for certain functions as
    specified, such as
  • Line 90) PINSEL0 0x00000005 // set p0.0 to
    TXD0, p0.1 to RXD0 and the rest to GPIO so p0.8 ,
    p0.9 are outputs, see the circuit diagram
  • Line 91) PINSEL0 0x000000C0 // set p0.3 as
    EINT1 external interrupt input(pin26)
  • Look at table 58 in the previous slide, for the
    address 0xE002 C000 (PINSEL0 for setting up GPIO
    port .
  • If bit0,1 is 01 , pin p0.0 (this is the
    symbolic name the physical pin number is pin19)
    will be assigned as TXD0 (of UART0) the transmit
    signal output of serial port 0 (UART0). Note that
    there are two serial ports (UART0,UART1)
    available in this MCU.
  • If bit2,3 is 01 , pin p0.1 (this is the
    symbolic name the physical pin number is pin21)
    will be assigned as RXD0 (of UART0) the receive
    signal input of serial port 0 (UART0). Note that
    there are two serial ports (UART0,UART1)
    available in this MCU.
  • Exercise search for pin p0.3 , what is the pin
    number and the other functions? Explain the
    purpose of line 3.
  • Exercise If fact the two statements (line 2,3)
    can be combined to become PINSEL00x000000C5
    Explain why and discuss why line 3 is written as
    PINSEL0 0x000000C0.
  • Exercise Where is this Table58 come from and
    discuss the use of P0.?

34
A2) Initialize timer and interrupt
  • init_timer_Eint()

35
/ Setup the Timer Counter 0 Interrupt
/145)void init_timer_Eint (void)
  • 144)/ Setup the Timer Counter 0 Interrupt /
  • 145)void init_timer_Eint (void)
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400 // set interrupt rate
    10Hz, (interval100mS)
  • 148) // Pclk/10Hz (11059200 x 5/4)/ 10
    (13.824MHz)/ 10
  • 149) T0MCR 3 // Interrupt
    and Reset on MR0
  • 150) T0TCR 1 // Timer0
    Enable
  • 151) VICVectAddr0 (unsigned
    long)IRQ_Exception // set interrupt vector in 0
  • 152) VICVectCntl0 0x20 4
    // use it for Timer 0 Interrupt
  • 153) VICIntEnable 0x00000010 //
    Enable Timer0 Interrupt
  • 154)
  • 155) EXTMODE0x02 // set EINT1 as edge
    trigger
  • 156) VICVectAddr1 (unsigned long)IRQ_Eint1
    // set interrupt vector in 1
  • 157) VICVectCntl1 0x20 15
    // use it for EINT1 Interrupt
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 // Clear EINT1 flag
  • 160)

36
/ Setup the Timer0 /http//www.nxp.com/acrobat
_download/usermanuals/UM10120_1.pdfstudy lines
144-150
  • 144)/ Setup the Timer Counter 0 Interrupt /
  • 145)void init_timer_Eint (void)
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400 // set interrupt rate
    10Hz, (interval100mS)
  • 148) // Pclk/10Hz (11059200 x 5/4)/ 10
    (13.824MHz)/ 10
  • 149) T0MCR 3 // Interrupt
    and Reset on MR0
  • 150) T0TCR 1 // Timer0
    Enable
  • 151) VICVectAddr0 (unsigned
    long)IRQ_Exception // set interrupt vector in 0
  • 152) VICVectCntl0 0x20 4
    // use it for Timer 0 Interrupt
  • 153) VICIntEnable 0x00000010 //
    Enable Timer0 Interrupt
  • 154)
  • 155) EXTMODE0x02 // set EINT1 as edge
    trigger
  • 156) VICVectAddr1 (unsigned long)IRQ_Eint1
    // set interrupt vector in 1
  • 157) VICVectCntl1 0x20 15
    // use it for EINT1 Interrupt
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 // Clear EINT1 flag
  • 160)

37
Explanation
  • See chapter14 (from page171) of
    http//www.nxp.com/acrobat_download/usermanuals/UM
    10120_1.pdf
  • The manual (UM10120_1.pdf) is huge, if you are
    interested you may read the whole chapters5,14 to
    see how timer0 can be programmed.
  • For you , the most important step is to set the
    timer to interrupt the CPU at freq_out Hz , you
    can set T0MRO (address 0xE000 4018 ) to be
    pclk/freq_out. So do you know what is PCLK in
    your system? ANS(13.824MHz)
  • Line 147 setup the timer to run at the required
    certain frequency.
  • 147) T0MR0 1382400// set interrupt rate
    10Hz, (interval100mS)
  • 148) // Pclk/10Hz (11059200 x 5/4)/ 10
    (13.824MHz)/ 10

38
Recall Summary of ClocksOne oscillator generates
two outputs CCLK, PCLK

ARM-LPC213x
FOSCx5CCLK for MCU 55.296MHz
FOSC 11.0592MHz
CCLK/4 PCLK for peripherals 13.824MHz
PCLK For timer0
39
Concept of the timer Operation
  • PCLK /freq_out(11059200 x 5/4)/freq_out
  • 13.824MHz /freq_out
  • When timer/counter (TC)match reg0 (T0MR0), an
    pulse is generated, Then TC is rest and count
    again.

Match reg0 (MR0) T0MR0
Timer Counter TC
Interrupt output
When TCT0MR0 a pulse is sent The frequency
generated freq_outPCLK/T0MR0 If
T0MR01384200 This output is 10Hz

PCLK 13.824MHz
reset
40
Line 146-148 set frequency, freq_outPCLK/T0MR0
  • To generate freq_out10Hz, you need
  • T0MR0plk/ freq_out
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400// set interrupt rate
    10Hz, //(interval100mS)
  • 148) // Pclk/10Hz (11059200 x 5/4)/ 10
  • 28) T0MCR 3 // Interrupt
    and Reset on MR0
  • 29) T0TCR 1 // Timer0
    Enable

41
Line 149 Set interrupt
Match reg0 (MR0) T0MR0 1382400
Timer Counter TC
Interrupt output
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400 // set interrupt rate
    10Hz, (interval100mS)
  • 148) // Pclk/10Hz (11059200 x 5/4)/ 10
  • 149) T0MCR 3 // Interrupt
    and Reset on MR0
  • 150) T0TCR 1 // Timer0
    Enable
  • Line 149 Match Control Register (MCR, TIMER0
    T0MCR - address 0xE000 4014)
  • Bit 0,1 of T0MCR (MR0R, MR0I)

PCLK 13.824MHz
reset
42
Line 150) Setting T0TCR Count Control Register
  • 150) T0TCR 1 // Timer0
    Enable
  • Line 150
  • Timer Control Register (TCR, TIMER0 T0TCR -
    0xE000 4004)

43
A2i) / time0 interrupt , setup the Vector
Control registers/lines151,152,153,
  • 144)/ Setup the Timer Counter 0 Interrupt /
  • 145)void init_timer_Eint (void)
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400 // set interrupt rate
    10Hz, (interval100mS)
  • 148) // Pclk/10Hz (11059200 x 5/4)/ 10
  • 149) T0MCR 3 // Interrupt
    and Reset on MR0
  • 150) T0TCR 1 // Timer0
    Enable
  • 151) VICVectAddr0 (unsigned
    long)IRQ_Exception // set interrupt vector in 0
  • 152) VICVectCntl0 0x20 4
    // use it for Timer 0 Interrupt
  • 153) VICIntEnable 0x00000010 //
    Enable Timer0 Interrupt
  • 154)
  • 155) EXTMODE0x02 // set EINT1 as edge
    trigger
  • 156) VICVectAddr1 (unsigned long)IRQ_Eint1
    // set interrupt vector in 1
  • 157) VICVectCntl1 0x20 15
    // use it for EINT1 Interrupt
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 // Clear EINT1 flag
  • 160)

44
Line 152VICVectCntl0(bit 04)(0x020 4),
because0x20gtbit 51 , is the IRQslot_en4 is
the source mask for timer0 (see next slide)
0x020 ? bit51
45
Source mask
  • E.g.
  • timer04

46
Line 153VICIntEnable 0x00000010enable timer
0
Bit4 is set
47
Timer0 hex_mask 4

48
A3) The interrupt service routine
  • void __irq IRQ_Exception()

49
A3) ISR program Timer counter 0 Interrupt blink
red-ledwhen timeval is even, red-led is on
otherwise, off
  • 118) void __irq IRQ_Exception()
  • 119)
  • 120) timeval
  • 121)
  • 122) //Blink the Red LED
  • 123) if((timeval2)0) IO0SETRED_LED
  • 124) else IO0CLRRED_LED
  • 125)
  • 126) T0IR 1
    // Clear interrupt flag
  • 127) VICVectAddr 0
    // Acknowledge Interrupt
  • 128)

Note Details see appendix and ch5 of
http//www.nxp.com/acrobat_download/usermanuals/UM
10120_1.pdf --VICVectAddr0 ( 0xFFFF F100 )
holding the starting address of ISR
IRQ_Exception() -- When interrupt occurs and
after the priority is resolved, VIC places
the content of VICVectAddr0 into --VICVectAddr (
0xFFFF F030 ) -- After ISR IRQ_Exception() is
executed, VICVectAddr0 is set by the ISR
program.
50
Limitation of interruptUsually stack is used in
the interrupt service routines isr()Maximum
Interrupt rate allowed Stack will overflow if
interrupt rate is too high.
  • main( )

51
Experiment
  • In our experiment, you may change the code of
    EintDemo.c to increase the interrupt rate ( much
    bigger than 10Hz) until the system crashes to see
    the limitation of interrupt.
  • The tutor will tell you the details

52
Part B
  • To demonstrate multiple interruptsEintDemo.c
  • Control the on/off of the green LED by a switch

53
To demonstrate multiple interruptsEintDemo.c
  • Important parts
  • main(),
  • init_timer_Eint () //init timer
  • __irq IRQ_Eint1() //external interrupt, blink
    green-led

54
In part B, we will show how to change the state
of the Green LED by pressing SW1-- after
pressing the switch SW1, the LED will change
state once ( from on-to-off, or from off-to-on)

Switch SW1
Arm board
green led
red led
Testing board
55
Our testing board connector

56
For 3.3V driving LEDs from a 3.3V system

57
Advanced topic
  • Nested interrupt using EintDemo.c
  • Multiple interrupt occurrences
  • Timer
  • External (a switch)
  • Further references
  • http//www.nxp.com/acrobat_download/applicationnot
    es/AN10254_2.pdf
  • http//www.nxp.com/acrobat_download/applicationnot
    es/AN10381_1.pdf

58
Multiple interrupt exampleNested interrupts can
occur
Timer1 set
Main() PINSEL0 0x000000C0//pin26Eint1 void
init_timer_Eint() Do something

//Timer0 interrupt __irq IRQ_Exception()
Blinks red-LED
timer0
//external interrupt __irq IRQ_Eint1()
blinks green-LED
Eint1 (pin26)
Occurs when Eint1 is pulled down
59
The theory for External interrupt1 (EINT1)ISR
Interrupt service routine for /EINT1 is _irq
IRQ_Eint1()
  • Not only the timer can generate interrupts, an
    external signal through EINT1 can also initiates
    an interrupt.
  • An falling edge at EINT1 will trigger the
    execution of ISR void __irq IRQ_Eint1()

External signal
  • /EINT1 (p0.3, pin26)
  • When /ENT1 is pulled down
  • __irq IRQ_Eint1()
  • Will be executed

LPC2213x
60
2ii) / Setup external interrupt
EINT1/Important lines
  • 144)/ Setup the Timer Counter 0 Interrupt /
  • 145)void init_timer_Eint (void)
  • 146) T0PR 0 // set prescaler to 0
  • 147) T0MR0 1382400 // set interrupt rate
    10Hz, (interval100mS)
  • 148) // Pclk/10Hz (11059200 x 5/4)/ 10
  • 149) T0MCR 3 // Interrupt
    and Reset on MR0
  • 150) T0TCR 1 // Timer0
    Enable
  • 151) VICVectAddr0 (unsigned
    long)IRQ_Exception // set interrupt vector in 0
  • 152) VICVectCntl0 0x20 4
    // use it for Timer 0 Interrupt
  • 153) VICIntEnable 0x00000010 //
    Enable Timer0 Interrupt
  • 154)
  • 155) EXTMODE0x02 // set EINT1 as edge
    trigger
  • 156) VICVectAddr1 (unsigned long)IRQ_Eint1
    // set interrupt vector in 1
  • 157) VICVectCntl1 0x20 15
    // use it for EINT1 Interrupt
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 // Clear EINT1 flag
  • 160)

61
setup external interruptline 155
  • 155) EXTMODE0x02 // set EINT1 as edge trigger
  • 156) VICVectAddr1 (unsigned long)IRQ_Eint1
    // set interrupt vector in 1
  • 157 VICVectCntl1 0x20 15 //
    use it for EINT1 Interrupt
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 //

62
setup external interruptline 156
  • 155) EXTMODE0x02 // set EINT1 as edge trigger
  • 156) VICVectAddr1 (unsigned long)IRQ_Eint1
    // set interrupt vector in 1
  • 157 VICVectCntl1 0x20 15 //
    use it for EINT1 Interrupt
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 //

Point to which interrupt service program will
run when EINT1 is pulled low
63
line 157) VICVectCntl1 0x20 15 // use it
for EINT1 Interrupt, because0x20gtbit 51 , is
the IRQslot_en15 is the source mask of
external interrupt1 (EINT1),(see next slide)
(see next slide)
0x020 ? bit51
64
Source mask
  • E.g.
  • timer04
  • external interrupt15

65
Examples of other interrupt sources
  • If you want to use Eint1(source mask15)
  • VICVectCntl1 0x20 15
  • If you want to use Eint0(source mask14)
  • VICVectCntl1 0x20 14
  • If you want to use Uart0(source mask6)
  • VICVectCntl1 0x20 6

66
setup external interrupt1 (EINT1)line 158
Bit15 is set
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 //
  • Enable external interrupt 1 (EINT1)

67
setup external interruptline 159
  • 155) EXTMODE0x02 // set EINT1 as edge trigger
  • 156) VICVectAddr1 (unsigned long)IRQ_Eint1
    // set interrupt vector in 1
  • 157 VICVectCntl1 0x20 15 //
    use it for EINT1 Interrupt
  • 158) VICIntEnable 0x00008000 // Enable
    EINT1 interrupt
  • 159) EXTINT 0x02 //
  • External Interrupt Flag register (EXTINT -
    address 0xE01F C140)

bit
68
4) External interrupt1 (EINT1) Green-led changes
state every time you press the interrupt switch
SW1
  • 130) //external interrupt
  • 131) void __irq IRQ_Eint1() //runs when key
    depressed. 132
  • 133) exint
  • 134) //turn on /off the Green LED
  • 135) if((exint2)0)
  • 136) IO0SETGREEN_LED
  • 137) else IO0CLRGREEN_LED
  • 138)
  • 139) EXTINT 0x02 // Clear EINT1 flag
  • 140) VICVectAddr 0 // Acknowledge
    Interrupt
  • 141)

69
Exercise
  • GPIO.c is a polling software program and a switch
    (SW1) to change the state of the Green LED
  • EintDemo.c is a program uses hardware external
    interrupt1 (EINT1) and a switch (SW1) to change
    the state of the Green LED.
  • Compare the difference between EintDemo.c and
    GPIO.c in terms of technology and performance

70
Summary
  • Learned how to initialize an ARM system
  • Learned how to use timer interrupt
  • EintDemo.c can be used as a template for building
    interrupt driven programs.

71
Appendix
72
Interrupt details chapter5 of 1 from
http//www.nxp.com/acrobat_download/usermanuals/UM
10120_1.pdf Example UART generates an interrupt
request and has the highest priory
  • Interrupt service routine ISR_UART (software to
    handle UART) starting address is at VICVectAddr
    address reg 0xFFFF F030
  • At 0x18,the instruction is LDR pc, pc,-0xFF0
    which will redirect Arm to executed ISR_UART()
    when UART interrupt request is received

VIC places the address there automatically
IRQ_vector0x18 ARM7TDMI Processor
VIC
IRQ
Or function
UART Serial interface
End of transmission
Logic_or all requests
73
IRQ execution vector
  • After initialization, any IRQ on UART0, SPI0,
    UART1 or I2C will cause jump to IRQ vector (0x18)
  • Could put LDR pc, pc,-0xFF0 instruction there
  • This instruction loads PC with the address that
    is present in VICVectAddr (0xFFFFF030) register!
    (meaning goto the address in VICVectAddr0xFFFF
    F030)
  • LDR pc, addr ? goes to PC8addr
  • Since -0x0000 0ff00xFFFFF00F10xFFFFF010
  • PC0x188-0x0ff00x20 0xFFFFF010 0xFFFFF030
  • so LDR pc, pc,-0xFF0 will branch to 0xFFFFF030
  • This instruction handles all 32 interrupt sources

- is 2s compliment
Write a Comment
User Comments (0)
About PowerShow.com