Mci 8
Mci 8
Microcontroller &
Interfacing
Experiment No.
08
Title:
8051 Micro controller Timers and counters
DOC. CODE
AITS/ECD/EXPT
DATE:
8.6
Rev. No.
1.00
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.
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
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
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)
TLX 5 Bits
THX 8-Bits
TFX
Interrupt
TLX 8 Bits
THX 8-Bits
TFX
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
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
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.
8.6
Rev. No.
1.00