Home >Documents >8051 Chap4 Timer

8051 Chap4 Timer

Date post:14-Apr-2018
Category:
View:221 times
Download:0 times
Share this document with a friend
Transcript:
  • 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

    [email protected]

    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 TH0, #-50 ; reload value

    SETB TR0 ; start timer

    WAIT: JNB TF0, WAIT ; wait for overflow

    CLR TF0 ; clear timer overflow flag

    CPL P1.0 ; toggle port bit

    SJMP WAIT ; repeat

    29L Ch Thng

    10-kHz Square Wave using Mode 2

    Mode 2: 8-bit auto-reload timer

    A buzzer is connected to P1.7 and a debounced switch is connected

    to P1.6. Write a program that reads the logic level provided by the

    switch and sounds the buzzer for 1 second for each 1-to-0 transition

    detected.

    30L Ch Thng

    Buzzer Interface

  • 7/30/2019 8051 Chap4 Timer

    16/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    HUNDRED EQU 100

    COUNT EQU -10000

    ORG 0000H

    MOV TMOD, #01H

    LOOP: JNB P1.6, LOOP ;wait for high level

    WAIT: JB P1.6, WAIT ;wait for low level

    SETB P1.7

    CALL DELAY

    CLR P1.7

    SJMP LOOP

    31L Ch Thng

    Buzzer Interface

    DELAY: MOV R7,#HUNDRED

    AGAIN: MOV TH0,#HIGH COUNTMOV TL0,#LOW COUNT

    SETB TR0

    JNB TF0,$

    CLR TF0

    CLR TR0

    DJNZ R7,AGAIN

    RET

    END

    When reading the content of counter, is the data correct?

    16-bit timer/counter, but 8-bit reading (MOV instruction) 2 read operations

    A phase error may occur.

    Solution

    AGAIN:MOV A, TH1

    MOV R6, TL1

    CJNE A, TH1, AGAIN

    MOV R7, A

    32L Ch Thng

    Reading a timer On the Fly

  • 7/30/2019 8051 Chap4 Timer

    17/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    12 MHz operation

    33L Ch Thng

    Techniques for Programming Timed Intervals

    Maximum

    interval [s]Technique

    10 Software tuning

    256 8-bit timer with auto-reload

    65536 16-bit timer

    No limit 16-bit timer plus software loops

    Very short intervals (i.e. high frequencies) can beprogrammed without using timers.

    LOOP: SETB P1.0

    CLR P1.0

    SJMP LOOP

    34L Ch Thng

    Very Short Intervals

  • 7/30/2019 8051 Chap4 Timer

    18/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    1 s delay subroutine: 1 s = 20 x 50 ms

    MOV TMOD, #00010000B ; Timer 1, mode1

    DELAY1S:

    PUSH 07 ; Push R7 to stack

    MOV R7,#20 ; 20 loops

    LOOP: MOV TL1, #LOW(-50000) ; Initial count = -50000

    MOV TH1,#HIGH(-50000) ;

    SETB TR1 ; start Timer 1

    JNB TF1, $ ; wait for overflow

    CLR TF1 ; clear overflow flag

    CLR TR1 ; stop Timer 1DJNZ R7,LOOP

    POP 07 ; Pop R7 from stack

    RET35L Ch Thng

    Delay 1 s using Timer 1 (12 MHz crystal)

    to Timer

    Internal clock fCLK = fCrystal / 12

    External clock

    36L Ch Thng

    Counter Operation

    C/T Clock Function

    0 Internal Timer (interval timing, delay)

    1 External Counter (event counting)

  • 7/30/2019 8051 Chap4 Timer

    19/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong

    37L Ch Thng

    Counter 0 Mode 1

    C/T = 1

    16-bit counter (TH0 and TL0)

    TH0-TL0 is incremented when TR0 is set to 1 and an externalpulse (in T0) occurs.

    Timer 0external clockinput(P3.4/T0)

    TR0

    TH0 TL0 TF0

    TF0 goes high

    when FFFF 0

    Overflow

    flag

    C/T = 1

    38L Ch Thng

    Counter_BarLED

    A push button is connected to P3.4 (T0). Assume that there is nocontact bounce. Write a program that counts the pulses created bypush button and display on the LED-Bargraph connected to Port 1.(schematic)

  • 7/30/2019 8051 Chap4 Timer

    20/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong 2

    39L Ch Thng

    Counter_BarLED

    ORG 0000H

    MAIN:MOV TMOD,#00000101B;Timer 0, 16 bit, external clock

    ;(counter operation);Gate=0, C/T=1, M1 M0 = 01

    MOV TH0,#0MOV TL0,#0SETB TR0 ;Start Timer

    LOOP: MOV A,TL0 ;Read TimerMOV P1,A ;Display on Bar-LEDSJMP LOOPEND

    (source)

    40L Ch Thng

    Counter_7segLED

    A push button is connected to P3.4 (T0). Assume that there is nocontact bounce. Write a program that counts the pulses created bypush button and display on the common-anode 7-segment LEDconnected to Port 1. (schematic)

  • 7/30/2019 8051 Chap4 Timer

    21/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong 2

    41L Ch Thng

    Counter_7segLED

    ORG 0000H

    MAIN: MOV TMOD,#00000101BMOV TH0,#0MOV TL0,#0SETB TR0

    LOOP: MOV A,TL0CJNE A,#10,NEXTCLR AMOV TL0,#0

    NEXT: ACALL DISPLAYSJMP LOOP

    DISPLAY:ACALL BCDTO7SEGMOV P1,A

    RET

    (source)

    BCDTO7SEG:MOV DPTR,#TABLEMOVC A,@A+DPTRRET

    TABLE: DB 40h,79h,24h,30h,19hDB 12h,02h,78h,00h,10h

    DONE: NOPEND

    42L Ch Thng

    Frequency Meter_7segLED

    A clock source is connected to P3.4 (T0). Write a program thatdisplays the frequency in KHz on the common-anode 7-segmentLED connected to Port 1. (schematic)

  • 7/30/2019 8051 Chap4 Timer

    22/24

  • 7/30/2019 8051 Chap4 Timer

    23/24

    H Bch Khoa TP.HCM L Ch Thn

    sites.google.com/site/chithong 2

    45L Ch Thng

    Pulse_width_7segLED

    A pulse source is connected to P3.2 (/INT0). Write a program that

    displays the pulse width in ms (approximate) on the common-anode 7-segment LED connected to Port 1. (schematic)

    46L Ch Thng

    Pulse_width_7segLED

    ORG 0000HMOV TMOD,#00001001B

    ;Timer 0,16 bit,internal clock,GATE=1MOV TH0,#0MOV TL0,#0SETB TR0

    AGAIN: MOV A,TH0CJNE A,TH0,AGAINMOV B,#4DIV AB

    ;A=Pulse width in us/256/4;approximate /1000

    ACALL DISPLAY

    SJMP AGAIN

    (source)

    DISPLAY:ACALL BCDTO7SEGMOV P1,ARET

    BCDTO7SEG:MOV DPTR,#TABLEMOVC A,@A+DPTRRET

    TABLE: DB 40h,79h,24h,30h,19hDB 12h,02h,78h,00h,10h

    DONE: NOPEND

  • 7/30/2019 8051 Chap4 Timer

    24/24

    H Bch Khoa TP.HCM L Ch Thn

    47

    References

    L Ch Thng

    I. Scott MacKenzie , The 8051 Microcontroller, 2ndEdition, Prentice-Hall, 1995

    Kenneth J. Ayala, The 8051 Microcontroller:Architecture, Programming, and Applications, West

    Publishing Company

    [email protected] , Lecture notes

Embed Size (px)
Recommended