0% found this document useful (0 votes)
11 views47 pages

Week 05 Timers

it is using timers in 8051

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views47 pages

Week 05 Timers

it is using timers in 8051

Uploaded by

tanveer1111110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Timers and Counters

7
Organization

 Introduction

 Timers
 Delay Generation
 Measuring Time

 Counters

8
Timers and Counters
 2 Timers in 8051
 T0
 T1

 Timers can be used to


1. Measure time between events
2. Generate required amount of time
3. Count events
4. Generate baud rate for Serial communication

9
Timers and Counters
Modes of operation are

2 Timers

Serial
Timers Counters
Port

Measure Generate
Time Time

10
Timers and Counters
 Always counts up in any operational mode
 Timer
 Counter

 Timer
 Timer increments by 1 every machine cycle
 Counter
 Increments at every input pulse

Time of 1 Machine Cycle, for 12 MHZ crystal?


1 micro-second for crystal of 12 MHz
11
Timers and Counters
Time of 1 Machine Cycle for 11.0592 MHz crystal?
 Oscillator (Crystal) : 11.0592 MHz

 12 Clock cycles make 1 Machine cycle

1 Machine Cycle = 12 Clock cycles

 Time of 1 clock cycle = 1 / 11.0592M

 Time of 1 Machine Cycle = 12* (1 / 11.0592M )

1 Machine Cycle = 1.0851 Micro-seconds


12
Timers and Counters
Timer SFRs

13
Timers and Counters
Timer SFRs

SFR Timer Description

TMOD T0 and T1 Set Timer Mode

TCON T0 and T1 Control Timer Operation

TL0 T0 Timer 0 Low Byte

TH0 T0 Timer 0 High Byte

TL1 T1 Timer 1 Low Byte

TH1 T1 Timer 1 High Byte

14
Timers and Counters
 Separate Registers – TH and TL
 High and Low byte of Timer
 Timer 0:
TH0 TL0
(1 Byte) (1 Byte)
TIMER 0

 Timer 1:
TH1 TL1
(1 Byte) (1 Byte)
TIMER 1

15
Timers and Counters
Bit Name Explanation of Function Timer
Configuring = 1 Timer will only run when INT1 is
Timers high. (Calculate Time between
7 GATE1 1
events)
= 0 for Timer1

TMOD =1 Input on P3.5 drives time. Counter


6 C/T1 Mode 1
=0 Crystal drives Timer. Timer Mode
5 T1M1 Timer mode bit 1
4 T1M0 Timer mode bit 1
= 1 Timer will only run when INT0 is
high. (Calculate Time between
3 GATE0 0
events)
= 0 for Timer0
=1 Counter
2 C/T0 0
= 0 Timer
1 T0M1 Timer mode bit 0
16
0 T0M0 Timer mode bit 0
Timers and Counters

Not gate

17
Timers

18
Timers and Counters
Modes of operation are

2 Timers

Serial
Timers Counters
Port

Measure Generate
Time Time

19
Timers

 Timer Operation
1. Configure timers (TMOD)
2. Initialize values (TH and TL)
3. Start and Stop Timer (TCON)

20
Timers
Using 8051 Timers TH0 TL0
(1 Byte) (1 Byte)
 TH and TL contain the TIMER 0
‘value’ of timer
TH1 TL1
 Timers count UP. This value (1 Byte) (1 Byte)
will be incremented by 1 every TIMER 1
machine cycle

 When TH and TL have maximum value, next machine cycle would set
them both to ZERO.

 Practice
 What is the maximum value TH and TL (1 Byte each) can have?

21
Timers
Using 8051 Timers
TH0 TL0
(1 Byte) (1 Byte)
 What is the maximum value TIMER 0
TH and TL (1 Byte each) can have?

 Maximum value = 1111 1111 1111 1111

TH TL
 = FF FF = 65,535

22
Timers
Using 8051 / 8052 Timers
 Maximum value of Timer TH0 TL0
(1 Byte) (1 Byte)
Registers= FF FF = 65,535 TIMER 0

Timer
Operation Timer
Value
(TH , TL)

Time
23
Timers
Timer Modes

 Mode 0 – 13 Bit Timer


 Mode 1 – 16 Bit Timer
 Mode 2 – 8 Bit Timer
 Mode 3 – Split Timer

24
Timers
Timer Modes

 Mode 0 – 13 Bit Timer


 To keep compatibility with 8048 (old)
 TL counts 0 to 31
 From 31, TL resets to ZERO and increments TH by 1

 Not used generally

25
Timers
Timer Modes

65,535d
 Mode 1 – 16 Bit Timer FF FFh
 Commonly used
 Counts up to FF FF and resets to zero

 Working: as discussed previously


Timer
Value
(TH , TL)

26
Timers
Timer Modes
 Mode 2 – 8 Bit Timer
 8 Bit Auto-Reload Mode
 TH = Reload value
 TL = Time Value Reset
Reload
Timer
Value
Value

Time

27
Timers
Timer Modes

 Mode 3 – Split Timer


 When Timer 0 is placed in mode 3, it becomes 2 x 8 bit Timers
 Timer 0 is TL0
 Timer 1 is TH0
 Bits of Timer1 will be tied to TH0

 Timer 1 can be in mode 0,1 or 2.


 Timer 1 will be incremented every machine cycle in any case – no
control

28
Timers
Bit Name Explanation of Function Timer
Configuring = 1 Timer will only run when INT1 is
high. (Calculate Time between
Timers 7 GATE1
events)
1

TMOD = 0 for Timer1


=1 Input on P3.5 drives time. Counter
6 C/T1 Mode 1
= 0 Crystal drives Timer. Timer Mode
5 T1M1 Timer mode bit 1
4 T1M0 Timer mode bit 1
= 1 Timer will only run when INT0 is
high. (Calculate Time between
3 GATE0 0
events)
= 0 for Timer0
=1 Counter
2 C/T0 0
= 0 Timer
1 T0M1 Timer mode bit 0
29 0 T0M0 M. Usman
Timer mode bit Rafique Air University, Spring02015
Timers
Configuring Timers
TMOD

Timer Mode

TxM1 TxM0 Timer Mode Description of Mode


0 0 0 13-bit Timer.
0 1 1 16-bit Timer
1 0 2 8-bit auto-reload
1 1 3 Split timer mode

30
Bit Name Explanation of Function Timer
Timers = 1 for External Time
7 GATE1 Measurement 1
Configuring Timers = 0 for Timer 1
TMOD =1 Counter
6 C/T1 1
= 0 Timer
5 T1M1 Timer mode bit 1
Practice 4 T1M0 Timer mode bit 1
 Timer 0 in Mode 1 = 1 for External Time
3 GATE0 Measurement 0
 Timer 1 in Mode 2 = 0 for Timer 0
Timer Description =1 Counter
TxM1 TxM0 2 C/T0 0
Mode of Mode = 0 Timer
0 0 0 13-bit Timer. 1 T0M1 Timer mode bit 0
0 1 1 16-bit Timer 0 T0M0 Timer mode bit 0
8-bit auto-
1 0 2
reload
Split timer
1 1 3
31 mode
Timers
Configuring Timers
TMOD
 Timer0 in Mode1
 Timer1 in Mode3

MOV tmod,#00100001b

OR

MOV tmod,#21h

32
Timers
Controlling Timers
TCON
Bit Name Explanation of Function Timer
Timer 1 Overflow.
7 TF1 1
= 1 when T1 Overflows
Timer 1 Run.
6 TR1 = 1 Timer1 Runs 1
= 0 Timer 1 Stops
Timer 0 Overflow.
5 TF0 0
= 1 when T0 Overflows
Timer 0 Run.
4 TR0 = 1 Timer0 Runs 0
= 0 Timer 0 Stops

Lower Bits are related to Interrupts and will be covered later on


33
Timers
Controlling Timers
TCON

setb tr0 ;running the timer

Label: JNB tf0, Label ; waiting till overflow flag is set

34
Timers

Using 8051 / 8052 Timers

 How to run timers for a given time?

 How to measure time between 2 events?

35
Timers
Using 8051 / 8052 Timers
 How to run timers for a given time?

2 Timers

Serial
Timers Counters
Port

Measure Generate
Time Time

36
Timers
Using 8051 / 8052 Timers
 How to run timers for a given time?
1. Select Timer Mode – TMOD
2. Store a Value in Timer Register – TH and TL
3. Wait for Timer to Over-flow or Reset – TCON

37
Timers
Using 8051 / 8052 Timers
 How to run timers for a given time?
1. Select Timer Mode
2. Set an appropriate Value in Timer Register

Timer Value = 65536 – (Desired


Number of Machine Cycle)
Value Reset
Timer
Value

38 Time
Timers
Practice
 Set P3.0 high for 0.05 seconds using Timer 0

39
Timers
 Set P3.0 high for 0.05 seconds using Timer 0

 Finding Value of Timer


 0.05 sec = 50,000 micro seconds

 1.0851 micro sec = 1 Machine Cycle


 1 micro sec = (1 / 1.0851) Machine Cycles
 50,000 micro sec = 50,000 * (1/1.0851)
= 46079 Machine Cycles

Note: 1.0851 micro sec for 11.0592 MHz Crystal

40
Timers
Using 8051 / 8052 Timers
 Set P3.0 high for 0.05 seconds using Timer 0

Value to load in TH and TL?

 Timer Value = 65536 – (Desired Number of Pulses)


= 65536 – 46079
= 19456 = 4C 01h

 TH = 4Ch ; TL = 01h

41
Timers
Using 8051 / 8052 Timers
 Set P3.0 high for 0.05 seconds using Timer 0

Timer Mode
 Mode 1 = 16 Bit timer Mode can be used
 TMOD = 0000 0001b
T1 T0

42
Timers
Using 8051 / 8052 Timers
 Set P3.0 high for 0.05 seconds using Timer 0

MOV TMOD,#00000001b;
MOV TH0,#4Ch;
MOV TL0,#01h;

setb P3^0
setb TR0

Here: JNB TF0, Here


CLR P3^0

43
Timers

 How to measure time between 2 events?

44
Timers
Using 8051 / 8052 Timers
 How to measure time between 2 events?

2 Timers

Serial
Timers Counters
Port

Measure Generate
Time Time

45
Timers
 How to measure time between 2 events?

 Set GATE Bit in TMOD

46
Timers
 How to measure time between 2 events?

0 1

0/1

47
Timers
 How to measure time between 2 events?

0/1

48
Timers
 How to measure time between 2 events?

 Set GATE Bit in TMOD


 C / T = 0 to ensure that crystal is connected to timers

 When TR = 1, timer will run ONLY WHEN P3.2or P3.3 is set

 So, timer value will be corresponding to the time P3.2 or P3.3


remains high

49
Counters

50
Counters

51
Counter
Counter

 If TCON – Bit “C/T” = 1


 Timer runs as counter

 Timer 0 – Pin 3.4 (T0)


 Timer 1 – Pin 3.5 (T1)

 Events (pulses) on these pins will be counted

 Frequency consideration

52
Counter
Counter

 Value of Timer is incremented by 1 when there is 1-to-0


(High to Low) transition on the pin

 For counter to count, there must be 1 during one


machine cycle and 0 during next machine cycle

 Maximum frequency: 2 machine cycles = 2 micro sec (for


12 MHz crystal)
Frequency = 1/2μs = 500 kHz

53

You might also like