timer and interrupts

Upload: baijayantisweety

Post on 09-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Timer and Interrupts

    1/40

    Timer Programming and InterruptsTimer Programming and Interrupts

    EE-342 Embedded System

  • 8/8/2019 Timer and Interrupts

    2/40

    Timer ProgrammingContrast and compare interrupt versus polling

    Interrupt Handling

    Interrupts of the 8051

    Purpose of the interrupt vector table

    Enable or disable interrupts

    Timers using interrupts

    Interrupt priority

    In this Lecture .In this Lecture .

  • 8/8/2019 Timer and Interrupts

    3/40

    Timers Programming (1/10)

    The 8051 has two timers: Timer 0 and Timer 1

    They can be used either as timers to generate a time delay or

    as counter to count events happening outside themicrocontroller

    Both Timer 0 and Timer 1 are 16 bits wide

    They are accessed as two separate registers, low byte and

    high byte. (TL0 & TH0 for Time 0 and TL1 & TH1 for

    timer 1)

  • 8/8/2019 Timer and Interrupts

    4/40

    Timers Programming (2/10)

    TH0 TL0

    D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

    There are three Special Function Registers for timer settings

    1. Timer Registers (Timer 0 & Timer 1) store the starting values

    of the Timer 0 & Timer 1. Each timer is 16-bit register which is

    split into two bytes (THx & TLx).

    Timer 0 register (16 Bit)

    TH1 TL1

    D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

    Timer 1 register (16 Bit)

  • 8/8/2019 Timer and Interrupts

    5/40

    Ti r r r i (3/ )

    (MSB) (LSB)

    TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

    2. Timer Control Register (TCON) use to turn ON/OFF of the timers

    and timer interrupt control. It is a 8-bit register and bit-addressable,

    and only the upper 4-bit refers to timer control.

    Timer ON/OFF control bits

    TR1 Timer 1 run control bit. Set = Timer ON and Clear = Timer OFF

    TF1 Timer flag which is set when the Timer 1 rolls over from FFFFH to 0000H

    Cleared the bit in software !.

    TR0 Timer 0 run control bit. Set = Timer ON and Clear = Timer OFF

    TF0 Timer flag which is set when Timer 0 rolls over from FFFFH to 0000H.

    Cleared the bit in software !

    IE1 and IT1 Set the trigger mode of external interrupt 1

    IE0 and IT0 Set the trigger mode of external interrupt 0

    Trigger mode of external interrupt control bits

  • 8/8/2019 Timer and Interrupts

    6/40

    Ti r r r i (4/ )

    (MSB) (LSB)

    GATE C/T M1 M0 GATE C/T M1 M0

    3. Timer Mode Register (TMOD) use to set the various timer operation

    modes. It is a 8-bit register and bit-addressable.

    Timer 1

    GATE Gating control when set. The timer/counter is enable only while the INTx

    pin is high and the TRx control pin is set. When cleared, the timer is enabledwhenever the TRx control bit is set.

    C/T Timer or counter selected. Cleared for timer operation (input from internal

    system clock) and Set for counter operation (input from Tx input pin)

    M1 & M0 Mode bits

    Timer 0

  • 8/8/2019 Timer and Interrupts

    7/40

  • 8/8/2019 Timer and Interrupts

    8/40

    Ti r / C u t r M d : 3 Bit C u t r u i Ti r

  • 8/8/2019 Timer and Interrupts

    9/40

    Ti r / C u t r M d : 6 Bit C u t r u i Ti r

    TL1

    (8 Bit )

    Timer value range from 0000H to FFFFH in TL1 TH1

  • 8/8/2019 Timer and Interrupts

    10/40

    Ti r / C u t r M d : 8 Bit Aut r l d

  • 8/8/2019 Timer and Interrupts

    11/40

    Example 8Example 8--11

    Solution:

    Convert the values from hex to binary:

    (a) TMOD = 0000 0001, mode 1 of Timer 0 is selected

    (b)TMOD = 0010 0000, mode 2 of Timer 1 is selected

    (c) TMOD = 0001 0010, mode 2 of Timer 0, and mode 1 of Timer 1are

    selected

    Indicate which mode and which timer are selected for each of the following.

    (a) MOV TMOD, #01H

    (b) MOV TMOD, #20H

    (c) MOV TMOD, #12H

    Ti r r r i (6/10)

  • 8/8/2019 Timer and Interrupts

    12/40

    Example 8Example 8--22

    Solution:

    (1)

    MOV TMOD, #01H ; Timer 0 and mode 1 set, C/T = 0 to use XTAL

    ; clock source, Gate = 0 to use software ON/OFF

    MOV TL0, #0ABH ; TL0 =ABH

    MOV TH0, #0F0H ; TH0 =F0H

    SETB TR0 ; Start Timer 0

    1. Write instructions to do the followings:

    a. Set Timer 0 in mode 1, use 8051 XTAL for the clock source,

    instructions to start and stop the timer,

    b. Set value F0ABH to Timer 0.

    c. Start Timer 02. Determine the time forTimer 0 rolling over if XTAL = 12 MHz.

    Ti r r r i (7/10)

  • 8/8/2019 Timer and Interrupts

    13/40

    Ti r r r i (8/10)

    Solution:

    (2)

    Counts for Timer rolling over = Counts from F0ABH to FFFFH

    plus rolling over to 0

    Timer clock cycles = (FFFFH F0ABH + 1) = 0F55H

    = 3925 in decimal

    @ Time for Timer 0 rolls over = 3925 x 1 Qs = 3925 Qs #

    Time for 1 Timer clock = 1 machine cycle = s1s12x

    10x12

    16

    Q!

    Assume 12 MHz clock :

    THx TLx # of count to set TFx

    FFH FFH 1FFH FEH 2

    65536 10 F0ABH

  • 8/8/2019 Timer and Interrupts

    14/40

    Example 8Example 8--33

    Solution:The period of the square wave, T = 1/50 Hz = 20 ms

    of it for the high and low portions of the pulse = 10 ms

    10 ms/ 1Qs = 10,000 timer cycles are needed for each pulse.

    Timer 1 value to be set = 65536 10000 = 55536 in decimal = D8F0H

    i.e. TH1 = D8H and TL1 = F0H ( need 16 bit Counter, so take mode 1)

    MOV TMOD, #10H ; Timer 1, mode 1

    AGAIN: MOV TL1, #0F0H ; TL1 =F0H

    MOV TH1, #0D8H ; TH1 =D8H

    SETB TR1 ; Start Timer 1

    BACK: JNB TF1, BACK ; Stay until timer rolls over

    CLR TR1 ; Stop Timer 1

    CPL P1.3 ; Complement P1.3 to set Hi, Low

    CLR TF1 ; Clear Timer flagby software

    SJMP AGAIN

    Assume XTAL = 12 MHz, write a program to generate a square wave of 50 Hz

    frequency on pin P1.3 by using timer 1 as time control.

    Ti r r r i (9/10)

    20ms

    65535 10000 + 1

    Ie. Toggle P1.3 every 10ms

  • 8/8/2019 Timer and Interrupts

    15/40

    Example 8Example 8--44

    ; Time Delay subroutine

    MOV TMOD, #10H ; Timer 1, mode 1

    MOV R3, #200

    AGAIN: MOV TL1, #08H ; TL1 =08H

    MOV TH1, #01H ; TH1 =01H

    SETB TR1 ; Start Timer 1

    BACK: JNB TF1, BACK ; Stay until timer rolls over

    CLR TR1 ; Stop Timer 1

    CLR TF1 ; Clear Timer flag

    DJNZ R3, AGAIN ; Loop until R 3 = 0

    RET

    Examine the following subroutine and find the time delay for it. (12 MHz clock)

    Ti r r r i (10/10)

    Solution :

    Timer value = 0108H = 264 in decimal, counts to rolls over = 65536 264 = 65272 in decimal

    Timer cycle of Timer = 65272

    Total machine cycles of the subroutine = [2 + 1 + (5 + 65272 + 4) x 200 + 2] = 13056205

    Total delay time 13056205 x 1Qs = 13056.2 ms

    Machine cycles

    2

    1

    2

    2

    1

    2

    1

    1

    2

    2

  • 8/8/2019 Timer and Interrupts

    16/40

  • 8/8/2019 Timer and Interrupts

    17/40

    A single microcontroller always connects to serve

    several peripheral devices through its I/O ports

    There are two ways for the peripheral devices to

    request service from microcontroller

    Polling

    Interrupt

    Interrup

    t & PollingInterru

    pt & Po

    lling

  • 8/8/2019 Timer and Interrupts

    18/40

    Microcontroller continuously monitors the status of a

    certainnumber of devices in sequence

    Services to a device if preset condition metAfter the service, the microcontroller will move on to

    monitor the status of another device until all devices

    are serviced

    The operation described above is called polling

    Programmed I/O (Polling)Programmed I/O (Polling)

  • 8/8/2019 Timer and Interrupts

    19/40

    Whenever any device needs the service, it notifies the

    microcontroller by sending it aninterrupt request (IR)

    signal while the microcontroller is doing other work.

    The microcontroller suspends its work to service the

    device at once.

    Note that each IR is associated with aninterrupt

    service routine (ISR)

    Interrupt I/O (Interrupt)Interrupt I/O (Interrupt)

  • 8/8/2019 Timer and Interrupts

    20/40

    Comparison between Interrupt and PollingComparison between Interrupt and Polling

    Interrupt Polling

    Method Devices notify MCU

    by sending it an

    interrupt signalswhile the MCU is

    doing another work

    MCU continuously

    monitors devices to

    determine whetherthey need service

    Response time Faster Slower

    Need of MCU

    time

    Less More

    Priority setting Yes No

  • 8/8/2019 Timer and Interrupts

    21/40

    St p i H dli I t rrupt R qu t

    The current instruction will be finished

    The PC is saved on the stack

    The current interrupt status is saved internally

    The PC is loaded with the vector address of the ISR

    from the interrupt vector table (Jump to execute ISR)

    The ISR is executed and will be finished with a RETIinstruction (return from interrupt)

    Return to main program by popping the PC from the

    stack

    When an interrupt activates and is accepted by the MCU, the main

    program is interrupted. The following actions occurs:

  • 8/8/2019 Timer and Interrupts

    22/40

    Pr r Ex cuti with I t rrupt

    Main Main Main

    ISR ISR

    stack

    PC saved

    on stack

    PC popped

    from stack

    The program that deals with an interrupt is called an interrupt

    service routine (ISR)

    ISR executes in response to the interrupt and generally performs I/O

    operation to a device

  • 8/8/2019 Timer and Interrupts

    23/40

    There are 6 interrupts in the 8051

    Types of Interrupt in the 8051Types of Interrupt in the 8051

    Reset when the reset pin is activated, the 8051 will reset allregisters and ports and jumps to address location 0000H starting upexecution.

    2 external interrupts Hardware external interrupts (INT0 andINT1) at pins 12 & 13 are used to receive interrupt signals fromexternal devices.

    2 timer interrupts They are Timer 0 and Timer 1 which will giveout interrupt signal when the timers count to zero.

    1 serial port interrupt - It is used for data reception andtransmission during serial communication.

    Apart from the Reset, only the 5 interrupts are available to the user

  • 8/8/2019 Timer and Interrupts

    24/40

    I t rrupt V ct r Tabl f r th 8051

    Priority Interrupt Flag ROM location Pin

    1 Reset RST 0000H 9

    2External 0(INT0)

    IE0 0003H P3.2 (12)

    3 Timer 0 TF0 000BH ---

    4

    External 1

    (INT1) IE1 0013H P3.3 (13)

    5 Timer 1 TF1 001BH ---

    6 Serial port RI or TI 0023H ---

    interrupt vector table holds the addresses of ISR

  • 8/8/2019 Timer and Interrupts

    25/40

    Enabling an Disabling an Interr t

    Upon reset, all interrupts are disabledThe interrupts must be enabled by software

    A register called IE (interrupt enable) register, which is bit-

    addressable, is responsible for enabling and disabling the interrupts

    Bit IE. 7 must be set high to allow the rest of register to take effect

    EA = 1 ; Global enable interrupt

    EA = 0 ; Global disable interrupt

    IE.7 IE.6 IE.5 IE.4 IE.3 IE.2 IE.1 IE.0

    EA -- ET2 ES ET1 EX1 ET0 EX0

    IE (interrupt enable) register

  • 8/8/2019 Timer and Interrupts

    26/40

    E abli and i ablingan Int rrupt

    Bit Symbol Description (1=Enable, 0=Disable)

    IE.7 EA Global Enable/disable

    IE.6 -- Not implemented, reserved for future use

    IE.5 ET2 Not use for 8051 (8052 only)

    IE.4 ES Enable/disable serial port interrupt

    IE.3 ET1 Enable/disable timer 1 interrupt

    IE.2 EX1 Enable/disable external interrupt 1

    IE.1 ET0 Enable/disable timer 0 interrupt

    IE.0 EX0 Enable/disable external interrupt 0

  • 8/8/2019 Timer and Interrupts

    27/40

    Example 8Example 8--55

    Solution:

    (a) MOV IE, #10010110B ; enable serial, Timer 0, EX1 interrupts

    or

    SETB IE.7 ; EA=1, Global enable

    SETB IE.4 ; enable serial interrupt

    SETB IE.1 ; enable Timer 0 interruptSETB IE.2 ; enable EX1 interrupt

    (b) CLR IE.1 ; disable Timer 0 interrupt

    (c) CLR IE.7 ; disable all interrupts

    Write instructions to

    (a) Enable serial interrupt, Timer 0 interrupt and external interrupt 1, and

    (b) Disable Timer 0 interrupt only, then

    (c) Disable all the interrupt with a single instruction

  • 8/8/2019 Timer and Interrupts

    28/40

    Ti r Int rrupt

    TF0 Timer 0 Interrupt Vector

    1 Jumps to 000BH

    When timer rolls over, its timer flag (TF) is set.

    If the timer interrupt in the IE register is enable, whenever the TF isset, the microcontroller is interrupted and jumps to the interruptvector table to service the ISR.

    With timer interrupt is enabled, microcontroller can do other thingsand no need to monitor the TF for rolling over.

    TF1 Timer 1 Interrupt Vector

    1 Jumps to 001BH

  • 8/8/2019 Timer and Interrupts

    29/40

    Write a program to generate a squarewave of 50 Hz on pin P1.3. This is similarto Example 8.3 except that it uses aninterrupt for Timer 1.

    Exampl 8Exampl 8--66

    Solution:ORG 0000HLJMP MAIN ;by-pass interrupt vector table

    ; ISR for Timer 1 to generate square waveORG 001BHLJMP ISR_T1 ; jump to ISR_T1

    ; Main Program for initializationORG 30H

    MAIN: MOV TMOD, #10H ; Timer 1, mode 1MOV TL1, #0F0H ; TL1 =F0HMOV TH1, #0D8H ; TH1 =D8HMOV IE, #88H ; enable Timer 1 interruptSETB TR1 ; Start Timer 1

    HERE: SJMP HERE; Timer 1 ISRISR_T1: CPL P1.3 ; Complement P1.3 to set Hi, Low

    MOV TL1, #0F0H ; reload timer valueMOV TH1, #0D8H ;SETB TR1 ; Start Timer 1RETIEND

    Timer 1 value to be set = 65536 10000 = 55536 in decimal = D8F0H

    Write a program to generate a square wave of 50 Hz on pin P1.3. This is similar

    to Example 8.3 except that it uses an interrupt for Timer 1.

    Example 8.3 (use TIMER)

  • 8/8/2019 Timer and Interrupts

    30/40

    Interr t Priority

    Table 8.1: 8051 Interrupt Priority Upon Reset

    External Interrupt 0 (INT0)

    Timer Interrupt 0 (TF0)

    External Interrupt 1 (INT1)Timer Interrupt 1 (TF1)

    Serial Communication (RI+TI)

    Upon reset, the priorities of interrupt source are assignedfrom top to bottom as in the following Table 8 , i.e. ifINT0 and INT1 are activated at the same time, INT0 isfirst responded.

    Highest

    Priority

    Lowest

    Priority

    INT0 > TF0 > INT1 > TF1 > SERIAL(RI+TI)

  • 8/8/2019 Timer and Interrupts

    31/40

    Setting Interr t Priority it t e IP register

    IP.7 IP.6 IP.5 IP.4 IP.3 IP.2 IP.1 IP.0

    -- -- PT2 PS PT1 PX1 PT0 PX0

    The sequence of Table 8 can be changed by assigning ahigher priority to any one of the interrupts.

    It is done by setting high at the corresponding bit in theIP (interrupt priority) register.

    IE (interrupt priority) register (Bit-addressable)

    Priority bit = 1 (assign high priority) Priority bit = 0 (assign low priority)

    PT2, Timer 2 (8052 only), PT1 & PT0 -- Timer 1 & Timer 0 interrupts

    PX1 & PX0 External interrupts 1 & 0, PS Serial port interrupt

  • 8/8/2019 Timer and Interrupts

    32/40

    Setting Interrupt Pri rit with the IP regi ter

    Example 8.7 :

    (a) Program the IP register to assign the highest priority to INT1,

    (b) Discuss what happens if INT0, INT1 and TF0 are activated at the

    same time.

    Solution :

    (a) MOV IP, #00000100B ; set IP.2=1 INT1 has the highest priority

    or SETB IP.2

    (b) Priority of interrupt will be changed to INT1 > INT0 > TF0The 8051 will services INT1 first and then INT0 and TF0.

    (As the INT0 and TF0 bits in IP register are 0, their priorities follow thesequence in Table 8.1)

  • 8/8/2019 Timer and Interrupts

    33/40

    Setting Interrupt Pri rit with the IP regi ter

    Example 8.8 :

    The IP register is set by the instruction MOV IP, #00001100B after

    reset. Discuss the sequence in which the interrupts are serviced.

    Solution :

    MOV IP, #00001100B instruction sets INT1 & TF1 to a higher

    priority level compared with the rest of the interrupts.

    Priority of interrupt will then beINT1 > TF1 > INT0 > TF0 > Serial

    POWER UP DEFAULT PRIORITY: INT0 > TF0 > INT1 > TF1 > SERIAL(RI+TI)

  • 8/8/2019 Timer and Interrupts

    34/40

    Interrupt insidean interrupt

    Higher-priority interrupt can interrupt a low-priority interrupt.

    An interrupt cannot be interrupted by a low-priority interrupt

    No low priority interrupt can get the immediate attention of the CPU

    until the 8051 has finished servicing the high-priority interrupts

    Main Main Main

    ISR ISR

    ISR

    ISR

    Low ISR is

    interrupted by a

    higher priority ISR

    higher ISRlower Lower ISR cannotinterrupt a higher

    priority ISR

  • 8/8/2019 Timer and Interrupts

    35/40

    Example 8.9

    Assume that the INT1 pinis connected to a switchthat is normally high.Whenever it goes low, itshould turn on an LED. The LED is connected toP1.3 and is normally off.Whenit is turned onit

    should stay on for a fraction of a second. As longas the switch is pressed, the LED should stay on.

    to LED

    80515v

    0v

    P1.3INT1 1=ON

    0=OFF

  • 8/8/2019 Timer and Interrupts

    36/40

    Example 8.9 - Solution

    ORG 0000HLJMP MAIN ;by-pass interrupt vector table

    ; ISR for INT1ORG 0013HLJMP ISR_INT1 ; jump to ISR_INT1

    ; Main ProgramORG 30H

    MAIN: MOV IE,#10000100B ;enable external INT 1 + GLOBALHERE: SJMP HERE ;stay here until get interrupted

    ; INT1 ISRISR_INT1: SETB P1.3 ;turn on LED

    MOV R3,#255BACK: DJNZ R3, BACK ;keep LED on for a while

    CLR P1.3 ;turn off LEDRETI

    END

  • 8/8/2019 Timer and Interrupts

    37/40

    1. How many timers do we have in the 8051_____ ?(8052 _____?)

    2. The timers of the 8051 are ___-bit and are designated as ____ and_______.

    3. The registers ofTimer 0 are accessed as ______ and ______.

    4. The registers ofTimer 1 are accessed as ______ and ______.

    5. The TMOD register is ____-bit register.

    6. Find the TMOD value for both Timer 0 and Timer 1, mode 2, softwarestart/stop (gate=0), with driving by internal clock.

    7. Find the frequency and period used by the timer if the crystal attachedto the 8051 has the following values.

    (a) XTAL = 11.0592MHz (b) XTAL = 12MHz

    (c) XTAL = 16MHz (d) XTAL = 20MHz

    (e) XTAL = 24MHz (f) XTAL = 30MHz

    Review QuestionsReview Questions

  • 8/8/2019 Timer and Interrupts

    38/40

    8. Find the value (in hex) loaded into TH in each of the following.

    (a) MOV TH1, #-120 (b)MOV TH1, #-67

    (c) MOV TH1, #-222 (d)MOV TH0, #-104

    9. What address in the interrupt vector table is assigned to Time 0?

    10.T

    o put the en

    ti

    rein

    terrupt servi

    ce routin

    ein

    thein

    terrupt vector table,it must be no more than _____ bytes in size.

    11.Why do we put an LJMP instruction at address 0?

    12.What register keeps track ofinterrupt priority in the 8051? Is it a bit-addressable register?

    13.If the value of IP register

    is 09H, arra

    nge the pr

    ior

    ity of the 5

    interrupts(exclude Reset) of 8051 in descending order.

    14.Assume that the IP register contains all 0s. Explain what happens if bothINT0 and INT1 are activated at the same time.

    Review QuestionsReview Questions

  • 8/8/2019 Timer and Interrupts

    39/40

    The 8051Microcontroller and Embedded

    Systems - Using Assembly and C,Mazidi

    Chapter 9 P.239

    P.255Chapter 11 P.317 P.339

    Read referenceRead reference

  • 8/8/2019 Timer and Interrupts

    40/40

    EE341 Embedded System

    Dept. of EE, NITR

    Timer Programming and InterruptsTimer Programming and Interrupts