Timer/Counter Programming

advertisement

Timer/Counter Programming

Timers/Counters Programming

• The 8051 has 2 timers/counters:

• timer/counter 0

• timer/counter 1

They can be used as

1.

The timer to generate time delay.

– The clock source is the internal crystal frequency of the 8051.

2.

An event counter .

– External pulse input from input pin to count the number of events on registers.

– These clock pulses could represent the number of people passing through an entrance, or the number of wheel rotations, or any other event that can be converted to pulses .

Registers

• TH0, TL0 : timer/counter register of timer 0

• TH1, TL1 : timer/counter register of timer 1

• TMOD : Mode Select register

• TCON : Control Register

Timer0 and Timer1 Registers

• Accessed as lower byte and higher byte

 The lower byte register is TL0 / TL1

 The higher byte register is TH0 / TH1

 Accessed like any other register

• mov TL0, #4Fh

• mov R5, TH0

TCON register

Timer control and Flag bits

• TR (Timer run control bit)

– TR0 for Timer/counter 0; TR1 for Timer/counter 1.

– TR is set by programmer to turn timer/counter on/off.

• CLR TR0 : off (stop)

• SETB TR0 : on (start)

• TF (timer flag bit)

– TF0 for timer/counter 0; TF1 for timer/counter 1.

– TF is like a carry. Originally TF=0. When TH-TL rolls over from FFFFh to

0000, the 8051 sets TF to 1. TF should be cleared by software in polling method.

• TF=0 : not reach

• TF=1: reach

TMOD Register

timer clock

Gate

• Every timer has a mean of starting and stopping.

– GATE=0

• Internal control

• The start and stop of the timer are controlled by way of software.

• Set/clear the TR for start/stop timer.

SETB TR0

CLR TR0

– GATE=1

• External control

• The hardware way of starting and stopping the timer by software and an external source .

• Timer/counter is enabled only while the INT pin is high and the TR control pin is set (TR).

Mode selection Bits in the TMOD

M1 M0 Mode Operation

0 0 0

0 1 1

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

16-bit timer mode 8-bit THx + 8-bit TLx

1 0 2 8-bit auto reload 8-bit auto reload timer/counter;

THx holds a value which is to be reloaded into

TLx each time it overflows.

1 1 3 Split timer mode

Find Timer Register values

Assume XTAL frequency = 12MHz

 Clock frequency of the timer = 12MHz/12 = 1MHz

 Clock period = 1/1MHz = 1uS

 Devide the desired time delay by 1uS = decimal value n

 Perform 65536 n (for 16bit mode)

 convert the result to hex , where yyxx is the initial value to be loaded into the timer’s register

 Set TH = yy and TL = xx.

Timer Register Values

Example

• Generate 500us time delay. Let crystal frequency = 12MHz

 Timer clock period ( 12 / Xtal freq. ) = 1us

 The required Delay time = 500 us

 No. of counts to be counted by timer = 500us/1us = 500

 The value in TH0 & TL0 = 65536-500 = 65036 = FE0Ch

 TH0 = FEh, TL0 = 0Ch

Timer mode 1 programming

1.

Set the timer & mode in TMOD register

2.

Load 16 bit register with the initial count value

3.

Start the timer SETB TRX

4.

Keep monitoring TFx JNB TFx, step4

TFX = 0 until timer reaches FFFFh

TFX = 1 when timer rolls over from FFFFh to 0000h

5.

Stop the process CLR TRX

6.

Clear the TFx flag for the next round

7.

Go back to step 2 to load TH and TL

Programming timer in mode1

Example

• Assuming XTAL = 12MHz write a program to generate a square wave of 1khz at P2.0 with 50% duty cycle

 The required ON/OFF time or Delay time = ( 1 /1KHz)/2 = 500 µS

 Machine cycle period = 12MHz / 12 = 1 µS

 No. of counts to be counted by timer = 500us/1 µS = 500

 The value in TH0 & TL0 = 64536 - 500 = 64036 = FE0Ch

Programming timer in mode1

Example cont..

MAIN:

Again:

Here:

ORG 0 ljmp MAIN

;Reset entry point

;Jump above interrupt

ORG 0030H ;Main Program entry point after vector table space mov TMOD,#02H ;Timer 0, mode 1 mov TH0, #0FAh mov TL0, #024h setb TR0

JNB TF0, Here clr tr0

; TH0 = FEh

; TL0 = 0Ch

;Start timer

;keep monitoring TF0

;Stop timer cpl p2.0

clr TF0 sjmp Again

END

;toggle bit 0 of port2

;clear timer 0 flag bit

;go for next round

Programming timer in mode2

Example

• Assuming XTAL = 12MHz write a program to generate a square wave of 10khz at P1.0 with 50% duty cycle

 The required ON/OFF time or Delay time = ( 1 /10KHz)/2 = 50 µS

 Machine cycle period = 12MHz / 12 = 1 µS

 No. of counts to be counted by timer = 50us/1 µS = 50

 The value in TH0 & TL0 = 256 - 50 = 206 = CEh

Programming timer in mode2

Example Cont…

MAIN:

Again:

Here:

ORG 0 sjmp MAIN

ORG 0030H mov TMOD,#02H

;Reset entry point

;Jump above interrupt

;Timer 0, mode 2 mov TH0, #0CEh mov TL0, #0CEh

SETB TR0

; TH0 = CEh

; TL0 = CEh

;Start timer

JNB TF0, Here clr tr0 cpl p1.0

clr TF0 sjmp Again

END

;keep monitoring TF0

;stop the process

;toggle bit 0 of port1

;clear timer 0 flag bit

;go for next round

Generate a Large Time Delay

• The size of the time delay depends on two factors:

– The crystal frequency

– The timer’s 16-bit register, TH & TL

• The largest time delay is achieved by making TH=TL=0.

• What if that is not enough?

Large Time Delay

Example

Examine the following program and find the time delay in seconds.

Exclude the overhead due to the instructions in the loop.

org 0 again: mov TMOD,#10H mov R3, #200 mov TL1,#08 mov TH1,#01 setb TR1

;2

;2

;1 back: jnb TF1,back clr TR1 clr TF1

;65272

;1

;1 djnz R3, again ;2 end

Solution:

TH – TL = 0108H = 264 in decimal; 65536 – 264 = 65272.

One of the timer delay = 65272 X 1

 s = 65.272 ms

Total delay = 200 X 65.272 ms = 13.054400 seconds

Counter Programming

• Programming is as same as timer

• Only the difference is the source frequency

 Timer = internal frequency

 Counter = pulses from external input pins (T0/1)

 C/T bit in the TMOD decides the source frequency

Counter

• Count the number of events

– Show the number of events on registers

– External input from T0 input pin (P3.4) for Counter 0

– External input from T1 input pin (P3.5) for Counter 1

– External input from Tx input pin. a switch

8051

TH0

P1

T0

TL0

P3.4

to

LCD

Counter Programming

Example

Assume that clock pulses are fed into pin T1, write a program for counter

1 in mode 2 to count and display the state of the TL1 count on P2.

Org 00h

Start:

Sjmp start

Org 030h mov TMOD, #60h mov th1,#00h setb P3.5

Count_again: setb tr1

Back: mov a, tl1 mov p2, a jnb tf1, back clr tr1 clr tf1 sjmp Count_again end

; Counter 1 mode 2,

;clear th1

;make T1 as input

;start the counter

;get copy of TL1

;display it on port 2

;keep monitoring timer1 flag bit

;stop timer

;make tf1 =0 (polling mode)

Download