Вы находитесь на странице: 1из 46

成功大學工程科學系

Chapter 9

Counter/Timer programming in the


8051

Spring'17 TWHou 8051


1
outline 成功大學工程科學系

• 9.1 Programming 8051 timers


• 9.2 Counter programming

Spring'17 TWHou 8051


2
成功大學工程科學系

9.1 Programming 8051 timers


• Basic registers of the timer
• Timer 0 registers
• Timer 1 registers
• TMOD (timer mode) register
• Mode 1 programming
• Steps to program in mode 1
• Finding values to be loaded into the timer
• Generating a large time delay
• Using windows calculator to find TH, TL
• Mode 0

Spring'17 TWHou 8051


3
成功大學工程科學系

• Mode 2 programming
• Steps to program in mode 2
• Assemblers and negative values

Spring'17 TWHou 8051


4
Basic registers of the timer 成功大學工程科學系

Spring'17 TWHou 8051


5
成功大學工程科學系

Spring'17 TWHou 8051


6
M1, M0 成功大學工程科學系

• Three modes: 0, 1, 2
• Mode 0 – 13-bit timer
• Mode 1 –16 bit timer
• Mode 2 – 8-bit timer
• Mode 1 and 2 are used most frequently

Spring'17 TWHou 8051


7
成功大學工程科學系

Spring'17 TWHou 8051


8
C/T (clock /timer) 成功大學工程科學系

• 選擇當clock或是timer
• C/T = 0
– timer
– Clock source – crystal frequency
– Frequency of the timer is always 1/12 of
crystal frequency
– Use XTAL = 11.0592 most frequently
• Reason: baud rate for serial communication of 8051
• C/T = 1
– (event) Counter
– Input from Tx input pin

Spring'17 TWHou 8051


9
成功大學工程科學系

Spring'17 TWHou 8051


10
Gate 成功大學工程科學系

• Why does 8051 has the ‘Gate’ in TMOD?


– The SETB TR0 (TR1), CLR TR0(TR0)
instructions are only valid when GATE=0.
• How to start/stop the timer?
– Software start/stop by TR (timer start) bits
TR0, TR1 (in TCON timer/counter register)
separately
• TR0=1, Set timer/counter 0(on), SETB TR0, starts
timer
• TR1=1, Set timer/counter 1(on)
• TR0=0, clear timer/counter 0(off) CLR TR0, stops
timer
• TR1=0, clear timer/counter 1(off)
– Hardware start/stop the timer
GATE
Spring'17•TWHou = 1 (discussed
8051 in chapter 11)
11
成功大學工程科學系

Spring'17 TWHou 8051


12
成功大學工程科學系
Mode 1 programming
• 16-bit timer; (0000H~FFFFH)
• 1.select the counter one would use. Load the
initial value (time to be measured) to TL and TH
• 2. Start the timer (SETB TRx)
• 3. It starts to count up until it reaches FFFFH.
When it rolls over from FFFFH to 0000H, it sets a
high flag bit (TFx, timer flag). (keep monitoring
the TF, JNB TFx, target)
• When the TF is raised, one option would be to
stop the timer (CLR TRx)
• 4. After the timer reaches its limit and rolls over,
in order to repeat the process, the registers TH
and TL must be reloaded with the original value
and TF must be reset to 0.
NOTTWHou
• Spring'17 AUTORELOADED
8051
13
成功大學工程科學系

Spring'17 TWHou 8051


14
成功大學工程科學系

Spring'17 TWHou 8051


15
成功大學工程科學系

Spring'17 TWHou 8051


16
成功大學工程科學系

Spring'17 TWHou 8051


17
成功大學工程科學系

Spring'17 TWHou 8051


18
Timer delay calculation for 成功大學工程科學系
XTAL=11.0592

Spring'17 TWHou 8051


19
成功大學工程科學系

Spring'17 TWHou 8051


20
成功大學工程科學系

Spring'17 TWHou 8051


21
An Reload example 成功大學工程科學系

Spring'17 TWHou 8051


22
如何算該放到timer之值? 成功大學工程科學系

• Assume XTAL = 11.0592 MHZ


• 1. Divide the desired time delay by 1.085
us
• 2. Perform 65536-n, where n is the
decimal value we got in step 1.
• 3. Convert the result of Step 2 to hex,
where yyxx is the initial hex value to be
loaded into the timer’s registers
• 4. Set TL=xx, TH=yy.
• Example 9-10,9-11

Spring'17 TWHou 8051


23
成功大學工程科學系

Spring'17 TWHou 8051


24
成功大學工程科學系

Spring'17 TWHou 8051


25
成功大學工程科學系

Spring'17 TWHou 8051


26
Large Time Delay 成功大學工程科學系

Spring'17 TWHou 8051


27
Mode 0 成功大學工程科學系

• Like mode 1 except it is a 13-bit timer


• 0000H~1FFFHin TH-TL.
• (over 1FFFH 0000H)

Spring'17 TWHou 8051


28
Mode 2 programming 成功大學工程科學系

• 8-bit timer (00H~FFH) in TH


• The TH loaded value is copied to TL by
8051 automatically.
• Start timer (SETB TRx)
• 8051 increments TL. When it rolls over
from FFH to 00H, it sets the TFx.
• TL is reloaded automatically with the
original value kept by the TH register.
• AUTORELOADED
• Very useful, such as baud rate setting

Spring'17 TWHou 8051


29
成功大學工程科學系

Spring'17 TWHou 8051


30
成功大學工程科學系

Spring'17 TWHou 8051


31
成功大學工程科學系

Spring'17 TWHou 8051


32
成功大學工程科學系

Spring'17 TWHou 8051


33
成功大學工程科學系

Spring'17 TWHou 8051


34
成功大學工程科學系

正確 time delay

• 必須加入instruction execution overhead.

Spring'17 TWHou 8051


35
成功大學工程科學系

Section 9-2 Counter Programming


• C/T bit in TMOD register
• TCON register
• The case of GATE =1 in TMOD

Spring'17 TWHou 8051


36
C/T bit in TMOD reg. 成功大學工程科學系

• If C/T = 0, timer gets pulse from crystal


• If C/T =1, counter gets its pulse from P3.4
or P3.5 (T0 – timer 0 input, T1- timer 1
input)
– Counter counts up

Spring'17 TWHou 8051


37
成功大學工程科學系

Spring'17 TWHou 8051


38
成功大學工程科學系

• Q: 使用 JNB TFx, target 在loop中每個


iteration要浪費一個指令測試的時間(polling),
能不能再快一點?
•  use interrupt (in Chapter 11)

Spring'17 TWHou 8051


39
TCON register 成功大學工程科學系

Spring'17 TWHou 8051


40
Gate =1 in TMOD 成功大學工程科學系

• Gate = 0, software controlled


timer/counter.
• Gate =1, controlled by P3.2 and P3.3 for
timers 0 and 1, respectively.
• 用一個簡單switch 來externally控制
start/stop timer

Spring'17 TWHou 8051


41
成功大學工程科學系

Spring'17 TWHou 8051


42
成功大學工程科學系

Spring'17 TWHou 8051


43
成功大學工程科學系

End of Chapter 9

Spring'17 TWHou 8051


44
成功大學工程科學系

Spring'17 TWHou 8051


45
成功大學工程科學系

Spring'17 TWHou 8051


46

Вам также может понравиться