Download Timer and Counter - Microprocessor System Design - Slides | ECE 473 and more Assignments Microprocessors in PDF only on Docsity!
Microprocessor System Design, Dr. ShiueECE473/
Timer and Counter
Microprocessor System Design, Dr. ShiueECE473/
Timer and Counter
- •^ Timer and Counter
- (^) Can be accessed thru interrupt and software.– (^) Can be controlled with software.– (^) Generate outputs with variable frequency and pulse width.Measure the time/frequency of input signal.
- (^) 4 bit counterflop:)• (^) Structure of Timer/Counter (a series of divide-by-two flip
- Microprocessor System Design, Dr. ShiueECE473/ - Timer 0 and Timer - • Timer 1 (16-bit)• Timer 0 (16-bit)• Two 16-bit timers: Timer 0 and Timer - TH - TL
Microprocessor System Design, Dr. ShiueECE473/
Setup the Timer and Counter
- (^) TMOD: Timer Mode Register• (^) 2 registers: TMOD and TCON
Set the various timer
MSB timer 0 and the upper 4 bits are set aside for timer 1.– TMOD is an 8-bit register where the lower 4 bits are set aside foroperation mode.
LSB
GATE C/!T M M GATE C/!T M M
Timer 1
Timer 0
Microprocessor System Design, Dr. ShiueECE473/
TMOD Register
- •^ C/!T: Timer or counter selection C/!T = 0 the Osc. Crystal (1/12) is used to trigger the timer.^ ^ Timer (input from internal system clock)
– C/!T = 1
(^) (^) Counter (input from Tx input pin)
Mode•^ M1 and M0: Mode selection for timer and counter
M
M
(^) 13-bit timer/counter mode
(^) 16-bit timer/counter mode
(^) 8-bit auto reload timer/counter mode
(^) split timer/counter mode
T0 T P3.4P3.
Microprocessor System Design, Dr. ShiueECE473/
TCON Register
MSB
LSB
TF TR TF TR IE IT IE IT
Timer 1
Timer 1
Timer 0
Timer 0
- •^ TF1: Timer 1 overflow flag
- (^) TF1=0: processor vectors to the interrupt services.TF1=1: Timer/counter 1 overflows.
- •^ TR1: Timer 1 run control bit
- (^) TR1=0: turn Timer 1 OFFTR1=1: turn Timer 1 ON
Microprocessor System Design, Dr. ShiueECE473/
Run Timer (SW Control)
MSB
LSB
TR TR
Timer 1
Timer 1
Timer 0
Timer 0
SETB TR
Run Timer 1
SETB TR
Run Timer 0
CLR TR
OFF Timer 1
CLR TR
OFF Timer 0
MSB
LSB
GATE=
GATE=
Timer 1
Timer 0
TCON TMOD
Microprocessor System Design, Dr. ShiueECE473/
Timer and Counter
and use instructions to start and stop the timer.Timer 0 in mode 2, use 8051 XTAL for the clock source,•^ Q4: Find the value for TMOD if we want to programfrequencies. (a) 12MHz, (b) 16MHz, and (c) 11.0592MHz.various 8051-based systems with the following crystal•^ Q3: Find the timers’ clock frequency and its period forTMOD, #20H, and (c) MOV TMOD, #12H.each of the following (a) MOV TMOD, #01H, (b) MOV•^ Q2: Indicate which timer and which mode are selected for
Microprocessor System Design, Dr. ShiueECE473/
Timer 0, Mode 1 ExampleSteps to Program for
2. Load TH0 and TL0 (eg. MOV TL0, #9CH & MOV1. Load the TMOD value (eg. MOV TMOD, #21H)•^ Steps:
target: JNB TF0, target 4.^ Keep monitor the timer flag (TF0)3. Start the timer (SETB TR0)TH0, 0FFH)
Jump out only if TF0=
7. Repeat Step 2.6. CLR TF05. Stop the timer 0 (CLR TR0)
Microprocessor System Design, Dr. ShiueECE473/
Timer Delay Calculation
(FFFF-YYXX+1) *1.085•^ For XTAL=11.0592MHz
μs
(65535-NNNNN) *1.
μs
TH TL
Hexdecimal
OR
NNNNN is a decimal value derived from YYXXH
Microprocessor System Design, Dr. ShiueECE473/
Examples
include the overhead due to instructions).•^ Q7: Find the delay generated by timer 0 in the following code (Do not=11.0592MHz.DELAY subroutine generated by the timer. Assume XTAL•^ Q6: See the example in Q5, calculate the amount of time delay in the MOV TMOD, #01CLR P2.
Here: SETB TR0SETB P2.3MOV TH0, #0B8HMOV TL0, #3EH
Again: CLR P2.3CLR TF0CLR TR0^ JNB TF0, Again
Microprocessor System Design, Dr. ShiueECE473/
Examples
to instructions)wave if XTAL=11.0592MHz. (Not include overhead dueTimer 1 for a time delay. Find the frequency of the squaregenerates a square wave on P1.5 continuously usingsince it was a single pulse. The following program•^ Q9: examples in Q7 and Q8 did not reload TH and TL,overhead due to instructions)possible. Find the delay in ms. (Do not include the•^ Q8: Modify TL and TH in Q7 to get the largest time delay
Microprocessor System Design, Dr. ShiueECE473/
Timer Mode 0
- (^) 0000 ~ 1FFFH• (^) Mode 0: 13-bit Timer/counter mode
Microprocessor System Design, Dr. ShiueECE473/
Timer Mode 2
baud rate in serial communication.•^ This mode has many applications, including setting theunchanged still holding a copy of original values.copy of it is given to TL. This reloading leaves TH•^ In auto reload, TH is loaded with the initial count and a•^ Mode 2: 8-bit auto reload Timer/counter mode (00 ~ FFH)