Timer/Counter Programming

Download Report

Transcript Timer/Counter Programming

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
0
M0
Mode
Operation
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
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.
2.
3.
4.
Set the timer & mode in TMOD register
Load 16 bit register with the initial count value
Start the timer
SETB TRX
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
 Machine cycle period = 12MHz / 12
= 500 µS
= 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
ORG
0
;Reset entry point
ljmp
MAIN
;Jump above interrupt
cont..
ORG 0030H ;Main Program entry point after vector table space
MAIN:
mov TMOD,#02H
;Timer 0, mode 1
Again:
mov TH0, #0FAh
; TH0 = FEh
mov TL0, #024h
; TL0 = 0Ch
setb TR0
;Start timer
JNB TF0, Here
;keep monitoring TF0
clr tr0
;Stop timer
cpl p2.0
;toggle bit 0 of port2
clr TF0
;clear timer 0 flag bit
sjmp Again
;go for next round
Here:
END
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
ORG
0
;Reset entry point
sjmp
MAIN
;Jump above interrupt
ORG 0030H
MAIN:
mov TMOD,#02H
;Timer 0, mode 2
mov TH0, #0CEh
; TH0 = CEh
mov TL0, #0CEh
; TL0 = CEh
Again:
SETB TR0
;Start timer
Here:
JNB TF0, Here
;keep monitoring TF0
clr tr0
;stop the process
cpl p1.0
;toggle bit 0 of port1
clr TF0
;clear timer 0 flag bit
sjmp Again
;go for next round
END
Cont…
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:
back:
mov TMOD,#10H
mov R3, #200
mov TL1,#08
mov TH1,#01
setb TR1
jnb TF1,back
clr TR1
clr TF1
djnz R3, again
end
;2
;2
;1
;65272
;1
;1
;2
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.
8051
TH0
P1
TL0
P3.4
a switch
T0
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
Sjmp start
Org 030h
Start:
mov TMOD, #60h
mov th1,#00h
; Counter 1 mode 2,
;clear th1
setb P3.5
;make T1 as input
Count_again:
setb tr1
;start the counter
Back:
mov a, tl1
;get copy of TL1
mov p2, a
;display it on port 2
jnb tf1, back
;keep monitoring timer1 flag bit
clr tr1
;stop timer
clr tf1
;make tf1 =0 (polling mode)
sjmp Count_again
end