+ All Categories
Home > Engineering > 8051 timer counter

8051 timer counter

Date post: 19-Jan-2017
Category:
Upload: vishalgohel12195
View: 80 times
Download: 0 times
Share this document with a friend
24
8051 Timer / Counter
Transcript
Page 1: 8051 timer counter

8051 Timer / Counter

Page 2: 8051 timer counter

Introduction

TMOD Register

TCON Register

Modes of Operation

Counters

TOPICS

Page 3: 8051 timer counter

Introduction The microcontroller 8051 has two 16 bit Timer/

Counter registers namely Timer 0 (T0) and Timer 1 (T1) .When used as a “Timer” the microcontroller is programmed

to count the internal clock pulse.When used as a “Counter” the microcontroller is programmed

to count external pulses.

o Maximum count rate is 1/24 of the oscillator frequency.

Page 4: 8051 timer counter

The 8051 has 2 timers/counters:◦Timer/Counter 0 ◦ Timer/Counter 1

Registers Used in the Timer :◦ Timer 0 registers:

TH0, TL0

Exclusive◦ Timer 1 registers:

TH1, TL1

◦ TMOD (Timer mode register) Shared by both◦ TCON (Timer control register)

Page 5: 8051 timer counter

Basic Registers Registers THx & TLx

They are 16 bit wide.

These registers store:The time delay as a timer.The number of events as a counter.

Timer 0: TH0 & TL0Timer 0 high byte , timer 0 low byte

Timer 1: TH1 & TL1Timer 1 high byte, timer 1 low byte

Page 6: 8051 timer counter

Timer Registers

D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6

TH0 TL0

D15 D8D9D10D11D12D13D14 D7 D0D1D2D3D4D5D6

TH1 TL1

Timer 0

Timer 1

Page 7: 8051 timer counter

Timer mode register = TMOD

◦ An 8-bit register lower 4 bits : Timer 0 Mode setting (0000 : not used) upper 4 bits : Timer 1 Mode setting (0000 : not used)

◦ Not bit-addressable

TMOD Register

Page 8: 8051 timer counter

Functions of individual

bits of TMOD

GATE C/T M1 M0 GATE C/T M1 M0Timer 1 Timer 0

(MSB) (LSB)

BIT NAME EXPLANATION OF THE FUNCTION TIMER

7 GATE1When this bit is set the timer will only run when INT1 (P3.3) is high. When this bit is clear the timer will run regardless of the state of INT1.

1

6 C/T1When this bit is set the timer will count events on T1 (P3.5). When this bit is clear the timer will be incremented every machine cycle.

1

5 T1M1 Timer mode bit 1

4 T1M0 Timer mode bit 1

3 GATE0When this bit is set the timer will only run when INT0 (P3.2) is high. When this bit is clear the timer will run regardless of the state of INT0.

0

2 C/T0When this bit is set the timer will count events on T0 (P3.4). When this bit is clear the timer will be incremented every machine cycle.

0

1 T0M1 Timer mode bit 0

0 T0M0 Timer mode bit 0

Page 9: 8051 timer counter

0 : Timer operation (clock : Machine cycle)

1 : Counter operation (clock : Tx input pin)

C / T

GATE C/T M1 M0 GATE C/T M1 M0Timer 1 Timer 0

(MSB) (LSB)

Page 10: 8051 timer counter

M0 and M1 select the timer mode for timers 0 & 1.

M1 M0 Mode Operating Mode 0 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

M0,M1

Page 11: 8051 timer counter

Modes of operation

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

4 Operating Modes

Page 12: 8051 timer counter

Timer mode – 0 (The 13 bit Timer)

This is a relic mode.◦ Included in 8051 to maintain compatibility with its

predecessor 8048. The counters are counting up:

◦ TLx will count from 0 to 31.◦ When TLx is incremented from 31, it will “reset”

(overflow) to 0.◦ Now THx will be incremented.

Hence effectively only 13 bits are used.◦ Bits 0-4 of TLx.◦ Bits 0-7 of THx..

Page 13: 8051 timer counter

Timer mode – 1 (The 16 bit Timer)

This is the most commonly used mode.

This mode operates in a fashion almost like the Mode 0, only this time all 16 bits are used.

The counting:◦ TLx is incremented from 0(00h) to 255(FFh). ◦ When TLx is incremented from 255, it resets to 0 and

causes THx to be incremented by 1.◦ Hence we have a maximum count of ‘65,025’

(255*255) machine cycles.

Page 14: 8051 timer counter

Mode 1- Programming

÷ 12

TR

TH TL TF

Timeroverflow

flag

C/T = 0

TF goes high when FFFF 0

XTALoscillator

Page 15: 8051 timer counter

Timer Mode – 2 (8 bit Auto Reload)

When a timer is in mode 2, THx holds the "reload value" and TLx is the timer itself.

Thus the counting proceeds as:◦ TLx starts counting up. ◦ TLx reaches 255 and is subsequently incremented.◦ Now instead of resetting to 0 (as in the case of modes

0 and 1), it will be reset to the value stored in THx.

Page 16: 8051 timer counter

Mode 2With Internal Input

XTAL oscillator ÷ 12

TR1

TL1

TH1

TF1 overflow flag

reload

C/T = 0

Page 17: 8051 timer counter

When Timer 0 is placed in mode 3, it essentially becomes two separate 8-bit timers.

That is to say, Timer 0 is TL0 and Timer 1 is TH0.Both timers count from 0 to 255 and overflow back to 0 independently.

What happens to timer1?◦ All the bits that are related to Timer 1 will now be tied to TH0.

◦ While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2 normally.

◦ However, you may not start or stop the real timer 1 since the bits that do that are now linked to TH0.

◦ The real timer 1, in this case, will be incremented every machine cycle no matter what.

Timer Mode – 3 (Split Timer)

Page 18: 8051 timer counter

Mode 3With Internal Input

Page 19: 8051 timer counter

Finally, there is one more SFR that controls the two timers and provides valuable information about them.

Timer control register: TCON

◦ Upper nibble : TIMER◦ Lower nibble : INTERRUPTS

TCON

Page 20: 8051 timer counter

The Register bits represent the following values : BIT NAME BIT ADDRESS EXPLANATION OF THE

FUNCTIONTIMER

7 TF1 8FhTimer 1 Overflow. This

bit is set by the microcontroller when

Timer 1 overflows.1

6 TR1 8Eh

Timer 1 Run. When this bit is set Timer 1 is turned on. When this bit is clear

Timer 1 is off.

1

5 TF0 8DhTimer 0 Overflow.

This bit is set by the microcontroller when

Timer 0 overflows.0

4 TR0 8Ch

Timer 0 Run. When this bit is set Timer 0 is turned on. When this bit is clear

Timer 0 is off.

0

Page 21: 8051 timer counter

TCON Register

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0Timer 1 Timer0 for Interrupt

(MSB) (LSB)

Page 22: 8051 timer counter

Counters

As far as the use of a timer/counter as an event counter is concerned ,everything that we have talked about in the last section also applies to programming it as a counter ,except the source of the frequency.

When used as a timer ,the 8051’s crystal is used as the source of the frequency.

However ,when used as a counter ,it is a pulse outside of the 8051 that increments the TH,TL registers.

These timers can also be used as counters counting events happening outside the 8051.

Page 23: 8051 timer counter

Port 3 Pins Used for Timers 0 and 1

Pin Port Pin Function Description

14 P3.4 T0 Timer/Counter 0 external input

15 P3.5 T1 Timer/Counter 1 external input

GATE C/T=1 M1 M0 GATE C/T=1 M1 M0

Timer 1 Timer 0

(MSB) (LSB)

Page 24: 8051 timer counter

THANK YOU


Recommended