0% found this document useful (0 votes)
5 views12 pages

Module3 timers notes

This document covers the programming of timers, counters, and serial communication in the 8051 microcontroller using C and assembly language. It details the configuration and operation of Timer 0 and Timer 1, including their modes of operation, registers, and applications. Additionally, it provides example assembly language programs for generating square waves and calculating delays based on the microcontroller's clock frequency.

Uploaded by

legend636369
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)
5 views12 pages

Module3 timers notes

This document covers the programming of timers, counters, and serial communication in the 8051 microcontroller using C and assembly language. It details the configuration and operation of Timer 0 and Timer 1, including their modes of operation, registers, and applications. Additionally, it provides example assembly language programs for generating square waves and calculating delays based on the microcontroller's clock frequency.

Uploaded by

legend636369
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/ 12

EVEN2025 / MICROCONTROLLERS / BEC405A / 4TH SEM / A SECTION / Dr GSY

MODULE3
Timers/Counters & Serial port programming:
Basics of Timers & Counters, Data types & Time delay in the 8051 using C, Programming 8051 Timers,
Mode 1 & Mode 2 Programming, Counter Programming (Assembly Language only). (Text book 2- 3.4,
Text book 1- 7.1, 9.1,9.2)
Basics of Serial Communication, 8051 Connection to RS232, Programming the 8051 to transfer data
serially & to receive data serially using C. (Text book 2- 3.5, Text book 1- 10.1,10.2,10.3 except assembly
language programs, 10.5)
TEXT BOOKS
1. The “8051 Microcontroller and Embedded Systems – Using Assembly and C”, Muhammad Ali Mazidi and
Janice Gillespie Mazidi and Rollind. Mckinlay; Phi, 2006 / Pearson, 2006.
2. “The 8051 Microcontroller”, Kenneth j. Ayala, 3rd edition, Thomson/Cengage Learning.
3. “Programming And Customizing The 8051 Microcontroller”.,Myke Predko Tata Mc Graw-Hill Edition
1999 (reprint 2003).

Timers & Counters:


The 8051 microcontroller features two 16-bit timers/counters, Timer 0 (T0) and Timer 1 (T1), which can be
used for timing events, generating delays, or counting external events, depending on their configuration.
Purpose:
 Timers: Measure time intervals by incrementing a register based on the microcontroller's clock
frequency.
 Counters: Count external events or pulses by incrementing a register based on an external signal.
Registers:
 TMOD (Timer Mode Register): Configures the operating mode of Timer 0 and Timer 1 (e.g., timer mode,
counter mode, 8-bit or 16-bit mode).
 TCON (Timer Control Register): Controls the start/stop of timers, overflow flags, and interrupt enable
bits.
 TH0, TL0 (Timer 0 High and Low Bytes): Store the 16-bit value for Timer 0.
 TH1, TL1 (Timer 1 High and Low Bytes): Store the 16-bit value for Timer 1.
Modes of Operation:
 Mode 0 (8-bit Timer/Counter): Uses 8 bits for counting/timing.
 Mode 1 (16-bit Timer/Counter): Uses 16 bits for counting/timing.
 Mode 2 (8-bit Auto-Reload Timer): Automatically reloads the counter value when it overflows.
 Mode 3 (Two 8-bit Timers/Counters): Timer 0 is split into two independent 8-bit timers.
Interrupts:
 Timers can generate interrupts when they overflow (reach their maximum value and roll over to zero).
 The TCON register contains interrupt enable bits for Timer 0 and Timer 1.
Applications:
 Generating precise time delays.
 Counting external events (e.g., pulses from a sensor).
 Implementing pulse-width modulation (PWM).
 Creating baud rate generators for serial communication.
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 6.0MHz, then the timer clock will have a frequency
of 500khz.

̅=0
C/T

̅=1
C/T

Timer/Counter Control logic


̅̅̅̅̅
INT

1|P a ge
If the 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), & the gate bit in the TMOD register
must be 0, or external pin ̅̅̅̅̅̅̅
INTX must be a 1.
Timer modes of operation:
In Intel 8051, there are two 16-bit timer registers. These registers are known as Timer0 and Timer1. The timer
registers can be used in two modes. These modes are Timer mode and the Counter mode. The only difference
between these two modes is the source for incrementing the timer registers.
Timer Mode
In the timer mode, the internal machine cycles are counted. So this register is incremented in each machine
cycle. So when the clock frequency is 12MHz, then the timer register is incremented in each millisecond. In
this mode it ignores the external timer input pin.
Counter Mode
In the counter mode, the external events are counted. In this mode, the timer register is incremented for each
1 to 0 transition of the external input pin (P3.4 & P3.5). This type of transitions is treated as events. The
external input pins are sampled once in each machine cycle, and to determine the 1or 0 transitions, another
machine cycle will be needed. So in this mode, at least two machine cycles are needed. When the frequency
is 12MHz, then the maximum count frequency will be 12MHz / (12d x 2m/c cycles) = 500KHz. So for event
counting the time duration is 2 µs.
There are four different modes of the Timer or Counter. The Mode 0 to Mode 2 are for both of the
Timer/Counter. Mode 3 has a different meaning for each timer register. There is a register called TMOD. This
register can be programmed to configure these timers or counters.
The Serial port is used for serial communication in mode 1 and 3. Timer1 is used for generating the baud rate.
So only Timer0 is available for timer or counter operations.
TMOD Register (8-bit register)
TMOD (Timer Mode) is an SFR. The address of this register is 89H. This is not bit-addressable.

In the following table, we will see the bit details and their different operations for high or low value.

Example of programming:
To configure the Timer0 as 16-bit event counter and Timer1 as 8-bit auto reload counter, we can use the bit
pattern 0 0 1 0 0 1 0 1. It is equivalent to 25H. If we want to program the TMOD register with this bit pattern,
we can use this instruction:
MOV TMOD, #25H
The above instruction is executed, then the timer/counter will be controlled by the software.
To configure the system as hardware controlled mode, then the gate bits will be 1. So the bit patterns will be
1 0 1 0 1 1 0 1 = ADH
we can use this instruction:
MOV TMOD, #0ADH
2|P a ge
TCON Register:

3|P a ge
Mode 0 of Timer/Counter
The Mode 0 operation is the 8-bit timer or counter with a 5-bit pre-scaler. So it is a 13-bit timer/counter. It
uses 5 bits of TL0 or TL1 and all of the 8-bits of TH0 or TH1.
Here Timer0 is shown in mode 0.
Timer1 can also be used in the
same manner.
In this example the Timer1is selected, in this case, every 32(2^5) event for counter operations or 32 machine
cycles for timer operation, the TH1 register will be incremented by 1. When the TH1overflows from FFH to
00H, then the TF1 of TCON register will be high, and it stops the timer/counter. So for an example, we can
say that if the TH1 is holding F0H, and it is in timer mode, then TF1will be high after 10H * 32 = 512 machine
cycles.
MOV TMOD, #00H
MOV TH1, #0F0H
MOV IE, #88H
SETB TR1
In the above program, the Timer1 is configured as timer mode 0. In this case Gate = 0. Then the TH1 will be
loaded with F0H, then enable the Timer1 interrupt. At last set the TR1 of TCON register, and start the timer.
Mode 1 of Timer/Counter
The Mode 1 operation is the 16-bit timer or counter. In the following diagram, we are using Mode 1 for
Timer0.
Here Timer0 is shown in mode 1.
Timer1 can also be used in the same
manner.

In this case every event for counter operations or machine cycles for timer operation, the TH0– TL0 register-
pair will be incremented by 1. When the register pair overflows from FFFFH to 0000H, then the TF0 of TCON
register will be high, and it stops the timer/counter. So for an example, we can say that if the TH0 – TL0
register pair is holding FFF0H, and it is in timer mode, then TF0 will be high after 10H = 16 machine cycles.
When the clock frequency is 12MHz, then the following instructions generate an interrupt 16 µs after Timer0
starts running.
MOV TMOD, #01H 12MHz/12 = 1MHz.
MOV TL0, #0F0H 1MHz 1μseconds
MOV TH0, #0FFH 16 counts * 1μseconds = 16 μseconds
MOV IE, #82H
SETB TR0
In the above program, the Timer0 is configured as timer mode 1. In this case Gate = 0. Then the TL0 will be
loaded with F0H and TH0 is loaded with FFH, then enable the Timer0 interrupt. At last set the TR0 of TCON
register, and start the timer.
Mode 2 of Timer/Counter
The Mode 2 operation is the 8-bit auto reload timer or counter. In the following diagram, we are using Mode
2 for Timer1.

Here Timer1 is shown in mode 2.


Timer0 can also be used in the same
manner.

In this case every event for counter operations or machine cycles for timer operation, the TL1register will be
incremented by 1. When the register pair overflows from FFH to 00H, then the TF1 of TCON register will be
high, also theTL1 will be reloaded with the content of TH1 and starts the operation again.

4|P a ge
So for an example, we can say that if the TH1 and TL1 register both are holding F0H and it is in timer mode,
then TF1 will be high after 10H= 16 machine cycles. When the clock frequency is 12MHz this happens after
16 µs, then the following instructions generate an interrupt once every 16 µs after Timer1 starts running.
MOV TMOD, #20H
MOV TL1, #0F0H
MOV TH1, #0F0H
MOV IE, #88H
SETB TR1
In the above program, the Timer1 is configured as timer mode 2. In this case Gate = 0. Then the TL1 and TH1
are loaded with F0H. then enable the Timer1 interrupt. At last set the TR1 of TCON register, and start the
timer.
Timer1 in mode 2 generates the desired baud rate when the serial port is working on Mode 1 or 3.
Mode 3 of Timer/Counter
Mode 3 is different for Timer0 and Timer1. When the Timer0 is working in mode 3, the TL0 will be used as
an 8-bit timer/counter. It will be controlled by the standard Timer0 control bits, T0 and INT0 inputs. The TH0
is used as an 8-bit timer but not the counter. This is controlled by Timer1 Control bit TR1. When the TH0
overflows from FFH to 00H, then TF1 is set to 1. In the following diagram, we can Timer0 in Mode 3.

When the Timer1 is working in Mode 3, it simply holds the count but does not run. When Timer0 is in mode
3, the Timer1 is configured in one of the mode 0, 1 and 2. In this case, the Timer1 cannot interrupt the
microcontroller. When the TF1 is used by TH0 timer, the Timer1 is used as Baud Rate Generator.
The meaning of gate bit in Timer0 and Timer1 for mode 3 is as follows
It controls the running of 8-bit timer/counter TL0 as like Mode 0, 1, or 2. The running of TH0 is controlled
by TR1 bit only. So the gate bit in this mode for Timer0 has no specific role.
The mode 3 is present for applications requiring an extra 8-bit timer/counter. In Mode 3 of Timer0, the 8051
has three timers. One 8-bit timer by TH0, another8-bit timer/counter by TL0, and one 16-bit timer/counter by
Timer1.
If the Timer0 is in mode3, and Timer1 is working on either 0, 1 or 2, then the gun control of the Timer1 is
activated when the gate bit is low or INT1 is high. The run control is deactivated when the gate is high
and INT1 is low.
Timer/Counter Programming (Assembly language only)
(Programming 8051 Timers, Counter programming):
First be perfect in TMOD & TCON registers. They are SFRs.
TMOD Register – SFR with address 89h

5|P a ge
TCON Register – SFR with address 88h

Problem statement:
1) Write an assembly language program for 8051 (ALP from now onwards) to generate a square wave
with 50% duty cycle on P1.5 bit. XTAL frequency is 11.0592MHz.

Steps of programming:
1) Load the TMOD with a bit pattern to select which timer we are using, which mode & other bit
settings. Here we have selected Timer0 with mode1.
2) Load TH0 & TL0 with a value based on the calculation.
3) Change the bit P1.5 from 1 to 0 or 0 to1.
4) Call a delay program to generate a delay depending on the ON time or OFF time of the square wave
to be generated
Delay:
a) Start the Timer with setting the respective TR bit. SETB TR0.
b) Keep monitoring the respective TF flag to check the completion of the count to check overflow.
c) If no overflow (TF = 0), then repeat (b)
d) If overflow (TF = 1), stop the timer (TR = 0) & clear TF flag
e) Go back to step (3).
Calculation:
XTAL frequency = 11.0592 MHz
8051 clock frequency = 11.0592MHz ÷ 12 = 921.6KHz.
Clock period = 1 ÷ 921.6KHz = 1.085μsecs.
Count = FFFFh – FFF2h + 1 = 14.
Delay generated = counts * clock period = 14 * 1.085μsecs = 15.19μsecs.
Square wave generated will be with ON period 15.19μsecs and OFF period 15.19μsecs.
Total period of the square wave = (ON + OFF) periods = 30.38μsecs.

6|P a ge
2) For the assembly language program for 8051 given above, to generate a square wave with 50% duty
cycle on P1.5 bit. XTAL frequency is 11.0592MHz. Here consider also the overheads involved in
the execution of the instructions in the loop & make the calculations for T generated.
ALP Clock
cycles

2
2
1
2
2

1
14

1
1
2
28

XTAL frequency = 11.0592 MHz


8051 clock frequency = 11.0592MHz ÷ 12 = 921.6KHz.
Clock period = 1 ÷ 921.6KHz = 1.085μsecs.
Count = FFFFh – FFF2h + 1 = 14.
T= 2 x 28 x 1.085μsecs = 60.76μsecs and F = 16458.2Hz.
3) Without including overheads, explain the ALP given below & calculate the delay generated.

Configuration: T0 & mode 1, Initial count = B83Eh


XTAL frequency = 11.0592 MHz, 8051 clock frequency = 11.0592MHz ÷ 12 = 921.6KHz.
Clock period = 1 ÷ 921.6KHz = 1.085μsecs. Count = FFFFh – B83Eh + 1 = 47C2h = 18370decimal.
Delay generated = counts * clock period = 18370 * 1.085μsecs = 19.93145msecs.
i.e., a pulse of the above width is generated.
4) With XTAL freq of 22MHz, timer 1 in mode 0 is used below. Analyse the ALP given below to
generate a square wave. Calculate the delay generated.
MOV TMOD, #00H ; T1, MODE0
AGAIN: MOV TL1, #00H ;
MOV TH1, #0FH ;
SETB TR1 ; Run T1
BACK: JNB TF1, BACK ; check for the overflow
CLR TR1 ; if overflow, stop the timer
CPL P0.6 ; complement P0.6
CLR TF1 ; clear T1 overflow
SJMP AGAIN ; reload the counter
7|P a ge
A square wave is output on P0.6.
XTAL freq = 22MHz, internal clock = 22MHz ÷ 12 = 1.833MHz, period 1 ÷ 1.833MHz = 0.546μsecs
Mode 0 is a 13-bit timer. TH with 8 bits & TL with 5-bits. With TL-TH combination & with a maximum
count of 11111-11111111 i.e., 1FFFh as maximum count.
Initial count TH(8-bits)-TL(5-bits) 0|0001|111-0|0000 taking only 13 bits we have 01D0h
Delay count is 1FFFh – 01D0h = 1E2Fh = 7727 + 1 = 7728.
Delay is 7728 X 0.546μsecs = 4.219msecs. (ON time & OFF time of the square wave)
Therefore, period T of the square wave = 4.219msecs X 2 = 8.4 msecs.
5) Assume XTAL frequency = 11.0592MHz. Write an ALP to generate a pulse of 5msecs on P2.3 using
T0.
Calculations: -
XTAL freq = 11.0592MHz.
Internal freq = 11.0592MHz ÷ 12 = 921.6KHz.
Internal clock period = 1 ÷ 921.6KHz = 1.085μsecs.
How many clocks are required to make pulse with delay 5ms = 5ms ÷ 1.085μsecs = 4608 clocks.
÷ Initial count to be loaded in TH-TL in mode1 is 65536 – 4608 = 60928 = EE00H.
Pgm: -
CLR P2.3
MOV TMOD, #01H ; 00000001b-Timer0 in mode1
HERE: MOV TL0, #00H
MOV TH0, #EEH
SETB P2.3
SETB TR0
AGAIN: JNB TF0, AGAIN
CLR P2.3
CLR TR0
CLR TF0
6) Write an ALP to generate a square wave of 2khz on p1.5.
Calculations: -
XTAL freq = 11.0592MHz.
Internal freq = 11.0592MHz ÷ 12 = 921.6KHz.
Internal clock period = 1 ÷ 921.6KHz = 1.085μsecs.
Given square wave frequency = 2khz.
T=1 ÷ 2khz = 500 μsecs.
Ton = Toff = 500 μsecs ÷ 2 = 250 μsecs.
Clock cycles to be counted for 250 μsecs = 250 μsecs ÷ 1.085μsecs = 230.
Initial count to be loaded in TH-TL combination = 65536 – 230 = 65306 = FF1AH
Pgm:-
MOV TMOD, #10H ; T1 in mode1
AGAIN: MOV TL1, #1AH
MOV TH1, #0FFH
SETB TR1
BACK: JNB TF1, BACK
CLR TR1
CPL P1.5
CLR TF1
SJMP AGAIN
Generating larger delays: -
Need of larger delays:
XTAL freq = 22MHz, internal clock = 22MHz ÷ 12 = 1.833MHz, period 1 ÷ 1.833MHz = 0.546μsecs
Final counts: mode0-1FFFH, mode1-FFFFH, mode2-FFH
Maximum delay in mode1 is (Final count – Initial count) x clock period
= (FFFFH – 0000H) x 0.546μsecs = 35.75 msecs.
8|P a ge
Hence mode1 can be used only for generating a max delay of 35.75 msecs.
(make the similar calculations for mode0 & mode2).
Problem: -
Write an ALP for generating a delay of 1 second. (using mode1 & T0)
In mode1 as we see above, the max delay is 35.75 msecs. Hence to generate a delay of 1 second we need to
repeat this delay of 35.75 msecs 28 times. (1 second ÷ 35.75 msecs = 28 = 1CH).
Pgm:-( to generate a delay of 1second & also a square waveform of 0.5Hz on P2.3)
MOV TMOD, #01H ; T0 in mode1
REPEAT: MOV R0, #1CH ; R0 = 28
AGAIN: MOV TL0, #00H
MOV TH0, #00H
SETB TR0
BACK: JNB TF0, BACK to generate a the delay of 35.75msecs is repeated
CLR TR0 delay of 35.75msecs. 28 times to generate a delay of
CLR TF0 35.75msecs x 28 1second.
DJNZ R0, AGAIN
CPL P2.3
SJMP REPEAT
Problem: -
Write an ALP to generate a square waveform with Ton = 3msecs & Toff = 10msecs.
Assume XTAL freq = 22MHz & waveform on P0.0.
Calculations: -
XTAL freq = 22MHz, internal clock = 22MHz ÷ 12 = 1.833MHz, period 1 ÷ 1.833MHz = 0.546μsecs
Delay calculations:
Ton for 3msecs: - let us use T0 in mode1. Max delay in mode1 is 35.75msecs. Hence larger delay is not
required.
Calculation of initial count to be loaded in TH0-TL0 combination for mode1: -
Total clock cycles to count 3msecs = 3msecs ÷ 0.546μsecs = 5494 = 1576H
Initial count = FFFFH – 1576H = EB89H
Toff for 10 msecs: -
Calculation of initial count to be loaded in TH0-TL0 combination for mode1: -
Total clock cycles to count 10msecs = 10msecs ÷ 0.546μsecs = 18315 = 478BH
Initial count = FFFFH – 478BH = B874H.
Pgm: -
MOV TMOD, #01H ; T0 in mode1
BACK: MOV TL0, #74H
MOV TH0, 0B8H ; delay count for 10msecs
CLR P0.0
ACALL DELAY
MOV TL0, 89H
MOV TH0, 0EBH ; delay count for 3msecs
SETB PO.O
ACALL DELAY
SJMP BACK
DELAY: SETB TRO
AGAIN: JNB TF0, AGAIN
CLR TR0
CLR TF0
RET
END

9|P a ge
Mode2 programming: -

Features for programming 8051 in mode2.


 It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer’s register
TH.
 After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL. Then the timer must be
started by using SETB TRx instruction.
 After the timer is started, it starts to count up by incrementing the TL register. It counts up until it
reaches its limit of FFH. When it rolls over from FFH to 00, it sets high the TF (timer flag).
 When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded automatically with the
original value kept by the TH register. To repeat the process, we must simply clear TF and let it go
without any need by the programmer to reload the original value. This makes mode 2 an auto-reload,
in contrast with mode 1 in which the programmer has to reload TH and TL.
 It must be emphasized that mode 2 is an 8-bit timer. However, it has an auto reloading capability. In
auto-reload, TH is loaded with the initial count and a copy of it is given to TL. This reloading leaves
TH unchanged, still holding a copy of the original value.
Steps to program in mode2.
 Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is to be used, and select
the timer mode (mode 2).
 Load the TH registers with the initial count value.
 Start the timer.
 Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction to see whether it is raised.
Get out of the loop when TF goes high.
 Clear the TF flag.
 Go back to Step 4, since mode 2 is auto-reload.
Problem: -
With XTAL freq = 22MHz, write an ALP to generate a square wave of f=1KHz on p1.2 using mode2
Delay calculations: -
XTAL freq = 22MHz, internal clock = 22MHz ÷ 12 = 1.833MHz, period 1 ÷ 1.833MHz = 0.546μsecs.
F = 1KHz means T=1msecs. Ton = 0.5msecs = Toff
Cycles required to count 0.5msecs = 0.5msecs ÷ 0.546μsecs = 915 clocks.
Maximum clocks which can be counted in mode2 is 256. This counting of 256 clocks has to be repeated 915
÷ 256 = 3.57 times. To get the rounded value of this number let us divide 915 by 5 = 183. i.e., 183 clock cycles
will be counted 5 times which gives a total count of 915. 183 = B7H, the count value which is repeated 5
times.
Pgm: -
MOV TMOD, #02H BACK: JNB TF0, BACK
REPT: CPL P1.2 CLR TR0
MOV R0, #05H CLR TF0
AGAIN: MOV TH0, #48H DJNZ R0, AGAIN
SETB TR0 SJMP REPT
END

10 | P a g e
Counter Programming.
 Recall what is counter mode of Timer/Counter of 8051.

When C/T ̅ = 1, then the Timer 1/0 will be selected for counter operation, during which the Timer/counter
will count the external clock pulses from pin 14(p3.4) & 15(p3.5) by the T1 & T0 respectively.
 p3.4 & p3.5 have to be set (to configure them as input pins to allow the clock pulses to be counted).
Bcos when 805 is powered-up, all ports will be set as output ports.
 All operations of the counter/s is same as timers except the clock pulses which are counted.

T0 in counter operation in mode1 T1 in counter operation in mode1

T0 in counter operation in mode2 T1 in counter operation in mode2

11 | P a g e
Sample pgm to illustrate programming Timer/Counter in counter0 mode2 to count the external
events(external clock pulses) & display the count status in TL0 in port2.
MOV TMOD, 06H ; timer0 in counter mode1(8-bit auto-reload operation),
MOV TH0, #00H
SETB P3.4 ; make p3.4 as input pin to allow in the external clock pulses.
REPEAT; SETB TR0 ; start counting
BACK; MOV A, TL0 ;
MOV P2, A ; Display the count value in TL0 on port2
JNB TF0, BACK ; continue counting if TLO is not overflown
CLR TR0
CLR TF0
SJMP REPEAT

P2 connected to 7 segment display assembly and


the input is connected to p3.4
P3.4

Pgm to count the pulses of an input signal using Timer0. Also calculate the number of pulses received
in 1 second (Frequency of the received pulse waveform. Use Timer1 to count 1 second.(XTAL frequency
is 22MHz).

RPT: MOV TMOD, #00010101B ; T1 in mode1 to count 1 second & T0 in mode1 to count
external pulses on p3.4
SETB P3.4 ; make p3.4 pin as input pin
MOV TL0, #00H
MOV TH0, #00H
SETB TR0
MOV R0, #1CH ; 1CH = 28d repetition count to generate 1 second delay as
AGAIN: MOV TL1, #00H illustrated in the previous sections.
MOV TH1, #00H
SETB TR1 to generate a delay of 1 second, to count the number
BACK: JNB TF1, BACK of clock pulses in 1 second to get frequency of the
CLR TF1 pulse waveform. Timer1 is used here.
CLR TR1
DJNZ R0, AGAIN
MOV A, TL0 at the end of 1 second, the values in TH0-TL0 combination gives
MOV P2, A the number of pulses that were received in 1 second. This also gives the
MOV A, TH0 frequency of the signal, i.e., pulses in 1 second.
MOV P1, A
SJMP RPT
END

12 | P a g e

You might also like