Anda di halaman 1dari 7

Subject:

Microcontroller &
Interfacing
Experiment No.
08

Title:
8051 Micro controller Timers and counters

DOC. CODE
AITS/ECD/EXPT

DATE:

8.6

Rev. No.

1.00

Aim: - To study timers and counters


Theory: - Many microcontroller applications require counting of external events, such as the frequency
of a pulse train, or the generation of precise internal time delays between computer
actions. Both of these tasks can be accomplished using software techniques, but software
loops for counting or timing keep the processor occupies so that other, perhaps more
important, functions are not done.
To relieve the processor of this burden, two 16-bit up counters, named T0 and T1, are provided
for the general use of the programmer. Each counter may be programmed to count internal clock pulses,
acting as a timer, or programmed to count external pulses as a counter.
The counters are divided into two 8-bit registers called the timer low (TL0, TL1) and high (TH0,
TH1) bytes. All counters action is controlled by bit states in the timer mode control register(TMOD), the
timer/counter control register (TCON), and certain program instructions.
1. TCON: Timer Control SFR (Address = 88H, bit addressable):
TF1
Bit
7

Symbol
TF1

TR1

TF0

TR0

IE1

IT1

IE0

IT0

TR1

TF0

TR0

IE1

IT1

IE0

IT0

Function
Timer1 overflow flag. Set when timer rolls from 1s to 0.cleared when processor
vectors to execute interrupt service routine located at program address 001BH.
Timer1 run control bit. Set to1 by program to enable timer to count. Cleared to 0 by
program to halt timer.
Timer0 overflow flag. Set when timer rolls from 1s to 0.Cleared when processor
vectors to execute interrupt service routine located at program address 000BH.
Timer0 run control bit. Set to1 by program to enable timer to count. Cleared to 0 by
program to halt timer.
External interrupt 1 Edge flag. Set to 1 by when a high-to-low edge signal is
received on port 3 pin 3.3 (INT1). Cleared when processor vectors to interrupt
service routine located at program address 0013H. Not related to timer operations
External interrupt 1 signal type control bit. Set to 1 by program to enable external
interrupt 1 to be triggered by a falling edge signal. Set to 0 by program to enable a
low-level signal on external interrupt 1 to generate an interrupt.
External interrupt 0 Edge flag. Set to 1 by when a high-to-low edge signal is
received on port 3 pin 3.2 (INT0). Cleared when processor vectors to interrupt
service routine located at program address 0003H. Not related to timer operations
External interrupt 0 signal type control bit. Set to 1 by program to enable external
interrupt 0 to be triggered by a falling edge signal. Set to 0 by program to enable a
low-level signal on external interrupt 0 to generate an interrupt.

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

Subject:
Microcontroller &
Interfacing
Experiment No.
08

Title:
8051 Micro controller Timers and counters

DOC. CODE
AITS/ECD/EXPT

DATE:

8.6

Rev. No.

1.00

2. TMOD: Timer Mode Control SFR (Address = 89h):


Timer 1
Gate C/T M1
Bit
7/3

Symbol
Gate

6/2

C/ T

5/1

M1

4/0

M0

M0

Timer 0
Gate C/T

M1 M0

Function
OR gate enable bit, which controls RUN/Stop of time 1/0. Set to 1 by program to
enable timer to run if bit TR1/0 in TCON is set and signal on external interrupt
INT1/0 pin is high. Cleared to 0 by program to enable timer to run if bit TR1/0 in
TCON is set.
Set to 1 by program to make timer 1/0 act as a counter by counting pulses from
external input pins 3.5(T1) or 3.4(T0). Cleared to 0 by program to make timer act as
a timer by counting internal frequency.
Timer/counter operating mode select bit 1. Set/cleared by program to select mode.
Timer/counter operating mode select bit 1. Set/cleared by program to select mode.
M1 M0 Mode
0
0
0
0
1
1
1
0
2
1
1
3

Timer Counter Interrupts:


When a program wishes to count a certain number of internal pulses or external events, a number
is placed in one of the counters. The number represents the maximum count less the desired count, plus
1. The counter increments from the initial number to the maximum and then rolls over to 0 on the final
pulse and an also sets a timer flag. The flag condition may be tested by an instruction to tell the program
that the count has been accomplished, or the flag may be used to interrupt the program.
Timing:
If a counter is programmed to be a timer, it will count the internal clock frequency of the 8051
oscillator divided by 12d. As an example, if the crystal frequency is 12 MHz, then the timer clock will
have a frequency of 1MHz.
The resultant timer clock is gated to the timer by means of the circuit shown in figure a. In order
for oscillator clock pulses to reach the timer, the C/ T bit in the TMOD register must be set to 0(timer
operation). Bit TRX in the TCON register must be set to 1(timer run), and the gate bit in the TMOD
register must be 0, or
external pin INTX must be a 1. In other words, the counter is configured as a timer, and then the timer
pulses are gated to the counter by the run bit and the gate bit or the external input bits INTX.

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

Subject:
Microcontroller &
Interfacing
Experiment No.
08

Title:
8051 Micro controller Timers and counters

DOC. CODE
AITS/ECD/EXPT

DATE:

Oscillator
Frequency

Divide
by 12

8.6

Rev. No.

1.00

Timer
C/ T = 0(Timer)
To timer stages
C/ T = 1(Counter)

T1/0 Input Pin


TR1/0 Bit In TCON
Gate Bit in TMOD
____
INTX Pin

Timer Modes of Operation


1. Timer Mode 0:
Setting timer X mode bit to 00B in the TMOD register results in suing the THX register as an 8bit counter and TLX as a 5-bit counter.
Pulse
Input

TLX 5 Bits

THX 8-Bits

TFX

Interrupt

Timer Mode 0 13 Bit Timer/Counter


2. Timer Mode 1:
In mode 1 TLX is configured as a full 8-bit and THX is also configured as a full 8-bit counter.
Here we can get maximum time delay using as timer.
Pulse
Input

TLX 8 Bits

THX 8-Bits

TFX

Timer Mode 1 16 Bit Timer/Counter

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

Interrupt

Subject:
Microcontroller &
Interfacing
Experiment No.
08

Title:
8051 Micro controller Timers and counters

DOC. CODE
AITS/ECD/EXPT

DATE:

8.6

Rev. No.

1.00

3. Timer Mode 2:
TLX is 8-bit counter. THX is used to hold as value that is loaded into TLX every time TLX
overflows from 0ff to 00h. The timer flag is also set when TLX overflows.
This mode exhibits an auto-reload feature: TLX will count up from the number in THX,
overflow, and be initialized again with the contents of THX. For example, placing 9ch in THX will
result in a delay of exactly .0002 seconds before the overflow flag is set if a 6 Megahertz crystal is
used.
Pulse
Input

TFX

TLX 8 Bits

Interrupt

Reload TLX

THX 8 Bits

Timer Mode 2 Auto-Reload of TL from TH


4. Timer Mode 1:
Timers 0 and 1 may be programmed to be in mode 0,1, or 2 independently of a similar mode for
the other timer. This is not true for mode 3; the timers do not operate independently if mode 3 is chosen
for timer 0. Placing timer 1 in mode 3 causes it to stop counting; the control bit TR1 and timer 1 flag
TF1 are then used by timer 0.
Pulse
Input

TLX 8 Bits

F/12

Interrupt

TFX

TLX 8 Bits

TR1 Bit
in TCON
Timer Mode 3 Two 8-Bit Timers Using Timer 0
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

TFX

Interrupt

Subject:
Microcontroller &
Interfacing
Experiment No.
08

Title:
8051 Micro controller Timers and counters

DATE:

DOC. CODE
AITS/ECD/EXPT

8.6

Rev. No.

1.00

Timer 0 in mode 3 becomes two completely separate 8-bit counters. TL0 is controlled by the gate
arrangement of fig. Below. And sets timer flag TF0 when ever it overflows from 0FFh to 00h. TH0
receives the timer clock (the oscillator divided by 12)under the control of TR1 only and sets the TF1 flag
when it overflows.
Timer 1 may still be used in modes 0,1 and 2 while timer 0 is in mode 3 with one
important exception: timer 1 will generate No interrupts while timer 0 is using the TF1 overflow flag.
Switching timer 1 to mode 3 will stop it. Timer 1 can be used for baud rate generation for the serial port,
or any other mode 0, 1 and 2 function that does not depend on an interrupt for proper operation.
Counting:
The only difference between counting and timing is the source of the clock pulses to the counters.
When used as a timer the clock pulses are sourced from the oscillator through the divide-by-12d circuit.
When used as a counter, pin T0(P3.4) supplies pulses to counter 0, and pin T1(P3.5) to counter 1. The C/
T bit in TMOD must be set to 1.
The input pulse on TX is sampled during P2 of state 5 every machine cycle. A change on the
input from high to low between samples will increment the counter. Each high and low state of the input
pulse must thus be held constant for at least one machine cycle to ensure reliable counting. Since this
takes 24 pulses, the maximum input frequency that can be accurately counted is the oscillator frequency
divide by 24. For 12 MHz crystal, the maximum external frequency is 500KHz.

Grade

Lab-In-Charge

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

H.O.D.

Subject:
Microcontroller &
Interfacing
Experiment No.
08

Title:
8051 Micro controller Timers and counters

DOC. CODE
AITS/ECD/EXPT

DATE:

8.6

Rev. No.

1.00

EXERCISE
Programming in Assembly
1. Find the delay generated by Timer 0 in the following code. Do not include the overhead due to
instructions.
Org 0000h
Clr p2.3
Mov tmod,#01h
Here:
mov tl0,#3ch
Mov th0,#0b8h
Setb p2.3
Setb tr0
Again: jnb tf0,again
Clr tr0
Clr tf0
Clr p2.3
2. Write ALP to generate 25 ms delay using timer 1 & having crystal frequency 12 MHz using mode.
(polling and interrupt method)
3. Calculate the largest possible delay in mode 1 if crystal frequency is 11.0592 MHz.
4. The following program generates a square wave on pin P1.5 continuously using Timer 1 for a time
delay. Find the frequency of the square wave if XTAL = 11.0592 MHz. Do not include the overhead
due to instructions.
Org 0000h
Again: mov tl1,#34h
Mov th1,#76h
Setb tr1
Back:
jnb tf1,back
Clr tr1
Cpl p1.5
Clr tf1
Sjmp again
5. Write a program to generate square wave of 50% duty cycle of 10KHz
frequency on pin P1.5. Use XTAL = 12MHz. use timer 0 in mode1 (polling and
interrupt method)
6. Write a program to generate square wave of 70% duty cycle of 2KHz frequency
ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

Subject:
Microcontroller &
Interfacing
Experiment No.
08

Title:
8051 Micro controller Timers and counters

DATE:

DOC. CODE
AITS/ECD/EXPT

on pin P1.0. Use XTAL = 12MHz. use timer 1 in mode 1 (polling only)
7. Write a program to generate 100us of delay using timer 0 in mode 2. use XTAL
= 12MHz. (interrupt method only)
8. Write a program to count frequency of square wave being fed to T1 pin. Use
timer 1 as a counter in mode 1.
Programming in C (XTAL = 12MHz)
1. Write a 8051 C program to toggle all the bits of port P1 continuously with 10
msec of delay in between. Use timer 0, in mode 1.
2. Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use
timer 1 in mode 1.
3. Write an 8051 C program to create a frequency of 2.5KHz on pin P2.5. Use
timer 1 in mode 2.

ATMIYA INSTITUTE OF TECHNOLOGY & SCIENCE, RAJKOT

8.6

Rev. No.

1.00

Anda mungkin juga menyukai