8051 Chap4 Timer

  • View
    220

  • Download
    0

Embed Size (px)

Text of 8051 Chap4 Timer

  • 7/30/2019 8051 Chap4 Timer

    1/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    Chapter 4

    Timer Operation

    The 8051 Microcontroller

    L Ch Thng

    chithong@hcmut.edu.vn

    sites.google.com/site/chithong

    Ho Chi Minh City University of Technology

    1L Ch Thng

    2L Ch Thng

    Review of 3-bit Up Counter

    000001111000

    Overflow

  • 7/30/2019 8051 Chap4 Timer

    2/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    Timer/Counter

    8051 has 2 timers Timer 0

    Timer 1

    Each Timer is a 16-bit up counter Counts from 0000H to FFFFH

    FFFFH-to-0000H overflow: overflow flag is set

    L Ch Thng 3

    Timer Clock

    Timer/Counter

    3 Functions

    1. Timer is used as time delay generator (intervaltiming)

    Internal clock source

    2. An event counter (event counting)

    External clock source

    For example : number of people passing through an entrance

    number of wheel rotations

    any other event that can be converted to pulses

    3. Baud rate generation for serial port

    L Ch Thng 4

  • 7/30/2019 8051 Chap4 Timer

    3/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    to Timer

    Internal clock fCLK = fCrystal / 12

    External clock

    5L Ch Thng

    Clock Source

    C/T Clock Function

    0 Internal Timer (interval timing, delay)1 External Counter (event counting)

    6L Ch Thng

    Timer 1 mode 1 (16 bit)

  • 7/30/2019 8051 Chap4 Timer

    4/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    7L Ch Thng

    Clock Enable/Disable

    Gate TRx INTx Function

    00 X Clock Disable Timer Stops

    1 X Clock Enable Timer Runs

    1

    0 X Clock Disable Timer Stops

    1 1 Clock Enable Timer Runs

    Clock Enable/Disable

    TRx: Bit TR0 or TR1; INTx: pin INT0 or pin INT1

    8L Ch Thng

    Timer Run/Stop

    Gate TRx INTx Function

    00 X Clock Disable Timer Stops

    1 X Clock Enable Timer Runs

    10 X Clock Disable Timer Stops

    1 1 Clock Enable Timer Runs

    When Gate is cleared (0)

    Timer runs when TRx = 1

    Timer stops when TRx = 0

    When Gate is set (1)

    Timer runs when TRx = 1 and signal at pin INTx goes high

    Timer stops when TRx = 0 or signal at pin INTx goes low

    TRx: Bit TR0 or TR1; INTx: pin INT0 or pin INT1

  • 7/30/2019 8051 Chap4 Timer

    5/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    9L Ch Thng

    GATE=0

    Internal control The start and stop of the timer are controlled by the software

    Ex: SETB TR1 ; Run Timer 1

    CLR TR1 ; Stop Timer 1

    GATE=1

    External control

    The hardware way ofstarting and stoppingthe timer by software

    and an external source. When GATE is set and TRx is set

    (SETB TRx), Timer runs only while the INTx pin is high.

    GATE bit: More details

    Timer

    runs

    Timer

    stops

    10L Ch Thng

    Applications of Timer

    Gate C/T Application

    00 Delay

    1 Event counting; Frequency meter

    1 0 Pulse width meter

  • 7/30/2019 8051 Chap4 Timer

    6/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    11L Ch Thng

    Timer Registers

    TH0, TL0 registers

    TH1, TL1 registers

    TMOD register

    TCON register

    12L Ch Thng

    Timer 0 and Timer 1 Registers

    D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6

    TH0 TL0

    D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6

    TH1 TL1

    Timer 0

    Timer 1

  • 7/30/2019 8051 Chap4 Timer

    7/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    13L Ch Thng

    TMOD Register

    TMOD: Timer Mode RegisterGATE C/T M1 M0 GATE C/T M1 M0

    Timer 1 Timer 0

    (MSB) (LSB)

    GATE 0 : Timer/counter counts only while TRx bit is set.

    1 : Timer/counter counts only while TRx bit is set andINTx pin is high

    C/T 0 : Timer operation (clock : Machine cycle)

    1 : Counter operation (clock : Tx input pin)

    14L Ch Thng

    M1, M0: mode setting bits

    M1 M0 Mode Operating mode

    0 0 0 13-bit timer mode

    8-bit THx + 5-bit TLx (x= 0 or 1)

    0 1 1 16-bit timer mode

    8-bit THx + 8-bit TLx

    1 0 2 8-bit auto-reload mode

    8-bit auto reload timer/counter;

    THx holds a value which is to be reloaded intoTLx each time it overflows.

    1 1 3 Split timer mode

  • 7/30/2019 8051 Chap4 Timer

    8/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    15L Ch Thng

    An Example

    TMOD: Timer Mode RegisterGATE C/T M1 M0 GATE C/T M1 M0

    Timer 1 Timer 0

    (MSB) (LSB)

    Find the value for TMOD if we want to program timer 0 in mode 2,

    use 8051 XTAL for the clock source, and use instructions to start

    and stop the timer.

    Solution:

    TMOD= 0000 0010 Timer 1 is not used.

    Timer 0, mode 2

    C/T = 0 to use internal clock source (timer)

    GATE = 0 to use internal (software) start and

    stop method.

    16L Ch Thng

    More Examples

    TMOD: Timer Mode Register

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

    Timer 1 Timer 0

    (MSB) (LSB)

    Ex:

    MOV TMOD,#00000001B ; Timer 0 mode 1, timer operation

    MOV TMOD,#20H ; Timer 1 mode 2, timer operation

    MOV TMOD,#12H ; Timer 1 mode 1, Timer 0 mode 2, both timer; operation

    MOV TMOD,#00000101B ; Timer 0 mode 1, counter operation

    MOV TMOD,#00001001B ; Timer 0 mode 1, external control (GATE=1)

  • 7/30/2019 8051 Chap4 Timer

    9/24

  • 7/30/2019 8051 Chap4 Timer

    10/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    TF (timer overflow flag)

    TF0 : for Timer/counter 0

    TF1 : for Timer/counter 1.

    Originally, TF=0. When TH-TL roll over to 0000 fromFFFFH (overflow), the TF is set to 1.

    If we enable interrupt, TF=1 will trigger ISR.

    TCON Register (2)

    TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

    Timer 1 Timer0 for Interrupt

    (MSB) (LSB)

    19L Ch Thng

    12 MHz Crystal fCLK = 12MHz/12 = 1MHz

    1 MC = 1/1MHz = 1 s

    tDelay = 100 s = 100 MC

    Use Timer 1 to count from -100 to 0

    Timer 1 mode 1, timer operationTMOD = 00010000B or 10H

    Initial count TH1:TL1 = -100

    -100 = FF9CH

    TH1 = FFH and TL1 = 9CH

    20L Ch Thng

    Delay 100 s using Timer 1 (12 MHz crystal)

    TMOD 10H

    TH1:TL1 -100

    Run Timer 1

    Overflow?

    (TF1=1?)

    N

    Y

    Clear overflow flag

    Stop Timer 1

  • 7/30/2019 8051 Chap4 Timer

    11/24

  • 7/30/2019 8051 Chap4 Timer

    12/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    MOV TMOD, #01H ; Timer 0, mode 1: 16 bit

    MOV TH0,#HIGH -50 ; Load high byte

    MOV TL0, #LOW -50 ; Load low byte

    SETB TR0 ; start Timer 0

    JNB TF0, $ ; wait for overflow

    CLR TF0 ; clear Timer 0 overflow flag

    CLR TR0 ; stop Timer 0

    23L Ch Thng

    Delay 25 s using Timer 0 (24 MHz crystal)

    Write a program using Timer 0to create a 10 Hz square wave on P1.0

    24L Ch Thng

    10-Hz Square Wave

    Delay 50,000 s

    P1.0 NOT (P1.0)

    Timer 0 mode 1

    Initial count = -50,000

    Start Timer 1

    Overflow?

    (TF1=1?)

    N

    Y

    Clear overflow flag

    Stop Timer 1

    P1.0 NOT (P1.0)

    tH tL

    T

    f = 10 Hz

    T = 100,000 s

    tH = tL = 50,000 s

    (schematic)

  • 7/30/2019 8051 Chap4 Timer

    13/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    Write a program using Timer 0 to create a 10 Hz square wave on P1.0

    MOV TMOD, #01H ; Timer 0, mode 1 (16-bit timer mode)

    LOOP: MOV TH0, #HIGH(-50000); high byte of -50,000

    MOV TL0, #LOW(-50000) ; low byte of -50,000

    SETB TR0 ; start timer

    WAIT: JNB TF0, WAIT ; wait for overflow

    CLR TR0 ; stop timer

    CLR TF0 ; clear timer overflow flag

    CPL P1.0 ; toggle port bit

    SJMP LOOP ; repeat

    25L Ch Thng

    10-Hz Square Wave

    Delay 50,000 s

    P1.0 NOT (P1.0)(source)

    Write a program using Timer 0 to create a 10 kHz square wave on P1.0

    26L Ch Thng

    10-kHz Square Wave

  • 7/30/2019 8051 Chap4 Timer

    14/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    Write a program using Timer 0 to create a 10 kHz square wave on P1.0

    MOV TMOD, #01H ; Timer 0, mode 1 (16-bit timer mode)

    LOOP: MOV TH0, #HIGH(-50); high byte of -50

    MOV TL0, #LOW(-50); low byte of -50

    SETB TR0 ; start timer

    WAIT: JNB TF0, WAIT ; wait for overflow

    CLR TR0 ; stop timer

    CLR TF0 ; clear timer overflow flag

    CPL P1.0 ; toggle port bit

    SJMP LOOP ; repeat

    27L Ch Thng

    10-kHz Square Wave

    Mode 1: 16-bit timerDelay 50 s

    P1.0 NOT (P1.0)

    Write a program using Timer 0

    to create a 10 kHz square wave on P1.0

    28L Ch Thng

    10-kHz Square Wave using Mode 2

    Delay 50 s

    P1.0 NOT (P1.0)

    Timer 0 mode 2

    Reload value = -50

    Start Timer 1

    Overflow?

    (TF1=1?)

    N

    Y

    Clear overflow flag

    P1.0 NOT (P1.0)

    Mode 2: 8-bit auto-reload timer

  • 7/30/2019 8051 Chap4 Timer

    15/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    Write a program using Timer 0 to create a 10 kHz square wave on P1.0

    MOV TMOD, #02H ; Timer 0, mode 2 (8-bit auto-reload)

    MOV TH