BEC405A Module3
BEC405A Module3
Module 3:
8051 TIMERS & SERIAL
PORT
• Dr. VEENA S K
VISION OF THE DEPARTMENT
Module 3:
8051 TIMERS & SERIAL
PORT
COs
RBT
Module – 3 Level
address
ed
Figure 9–2
Timer 1 Registers
Timer1 Timer0
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 10
PROGRAMMING 8051 TIMERS
TMOD Register
Figure 9–3
Microcontroller BEC405A-Dr. VEENATMOD
S K. Register 01/25/2025 11
PROGRAMMING 8051 TIMERS
TMOD Register
If C/T = 0, it is used
as a timer for time Example 9-2
delay generation. Find the timer’s clock frequency and its period for various 8051-based system, with the
The clock source for crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
the time delay is the
crystal frequency
XTAL
of the 8051 oscillator
÷12
7. Timer 0 is stopped by the instruction CLR TR0. The DELAY subroutine ends,
and the process is repeated. 19
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025
PROGRAMMING 8051 TIMERS
Mode 1
Example 9-5
In Example 9-4, calculate the amount of time delay in the DELAY
subroutine generated by the timer. Assume XTAL = 11.0592 MHz.
Solution:
The timer works with a clock frequency of 1/12 of the XTAL
frequency; therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the
timer frequency.
As a result, each clock has a period of T = 1/921.6kHz = 1.085us. In
other words, Timer 0 counts up each 1.085 us resulting in delay =
number of counts x 1.085us.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13
decimal).
However, we add one to 13 because of the extra clock needed when it
rolls over from FFFF to 0 and raise the TF (b)
flag.inThis gives 14 x 1.085us =
decimal
(a) in hex
15.19us for half (FFFF
the pulse. For
– YYXX the entire period
+ 1) ConvertitYYXXis T = 2 x 15.19us =
30.38us as the time
x delay generated by thevalues
timer.of the TH,
1.085 us, where TL register to
YYXX are TH, TL decimal to get a
initial values NNNNN decimal,
respectively. then (65536 - 20
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025
NNNN) x
PROGRAMMING 8051 TIMERS
Example 9-6
Mode 1
In Example 9-5, calculate the frequency of the square wave generated on
pin P1.5.
Solution:
In the timer delay calculation of Example 9-5, we did not include the
overhead due to instruction in the loop. To get a more accurate timing, we
need to add clock cycles due to this instructions in the loop. To do that, we
use the machine cycle from Table A-1 in Appendix A, as shown below.
HERE: MOV TL0,#0F2H 2
Cycles
MOV 2
CPL TH0,#0FFH
P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY:
SETB TR0 1
AGAIN: JNB 14
TF0,AGAIN CLR 1
TR0 1
CLR 2
TF0 Total
RET
T = 2 28 1.085 us = 60.76 us and F 28
= 16458.2
Hz Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 21
PROGRAMMING 8051 TIMERS
Mode 1
Example 9-7
Find the delay generated by timer 0 in the following code, using both of the
Methods of Figure 9-4. Do not include the overhead due to instruction.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#3EH ;TL0=3Eh, the low byte
MOV TH0,#0B8H ;TH0=B8H, the high byte
SETB P2.3 ;SET high timer 0
SETB TR0 ;Start the timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag
0 TR0 ;Stop the timer 0
CLR
CLR TF0 ;Clear TF0 for next round
CLR P2.3
Solution:
a. (FFFFH – B83E + 1) = 47C2H = 18370 in decimal and 18370 x 1.085 us =
19.93145 ms
b. Since TH – TL = B83EH = 47166 (in decimal) we have 65536 – 47166 =
18370. This means that the timer counts from B83EH to FFFF. This plus
Rolling over to 0 goes through a total of 18370 clock cycles, where each
clock is 1.085 us in duration.
Microcontroller
c. Therefore, we have BEC405A-Dr.
18370 VEENA S K.us = 19.93145 ms as the width
x 1.085 of the22
01/25/2025
PROGRAMMING 8051 TIMERS
Example 9-8 Mode 1
Modify TL and TH in Example 9-7 to get the largest time delay possible.
Find the delay in ms. In your calculation, exclude the overhead due to the
instructions in the loop.
Solution:
To get the largest delay we make TL and TH both 0. This will count up from
0000 to CLR
FFFFH and then;Clear
P2.3 roll over to zero.
P2.3
MOV TMOD,#01 ;Timer 0, 16-
bitmode ;TL0=0, the low byte
HERE: MOV TL0,#0
MOV TH0,#0 ;TH0=0, the high byte
SETB P2.3 ;SET high P2.3
SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag
0 CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
CLR P2.3
Making TH and TL both zero means that the timer will count from 0000 to
FFFF, and then roll over to raise the TF flag. As a result, it goes through a
total Of 65536 states. Therefore, we have delay = (65536 - 0) x 1.085 us =
71.1065ms.
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 23
PROGRAMMING 8051 TIMERS
Example 9-9
Mode 1
The following program generates a square wave on P1.5 continuously using timer
1 for a time delay. Find the frequency of the square wave if XTAL = 11.0592
MHz. In your calculation do not include the overhead due to Instructions in
the loop.
MOV TMOD,#10;Timer 1, mod 1 (16-bitmode)
AGAIN: TL1,#34H ;TL1=34H, low byte of
MOV MOV timer
TH1,#76H ;TH1=76H, high byte
SETB
timerTR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CLR TR1 ;stop the timer 1
CPL P1.5 ;comp. p1. to get hi, lo
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;is not auto-reload
Solution:
Since FFFFH – 7634H = 89CBH + 1 = 89CCH and 89CCH = 35276
clock count and 35276 x 1.085 us = 38.274 ms for half of the square wave.
The frequency = 13.064Hz.
Also notice that the high portion and low portion of the square wave pulse are
equal.
In the above calculation, the overhead due to all the instruction in the loop is not
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 24
included.
PROGRAMMING 8051 TIMERS
Mode 1 - Finding the Loaded Timer Values
Solution:
You can use the Windows scientific calculator to verify the result provided by the
assembler. In Windows calculator, select decimal and enter 200. Then select hex,
then +/- to get the TH value. Remember that we only use the right two digits and
ignore the rest since our data is an 8-bit data.
Decimal 2’s complement (TH value)
-3 FDH
-12 F4H The advantage of using
The number 200 is the
timer count till the
-48 D0H negative values is that you
-60 don’t need to calculate
TFset to 1
is
-200 C4H the value loaded to THx
38H
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 35
SERIAL COMMUNICATION
D
Microcontroller BEC405A-Dr. VEENA S K. 7 01/25/2025 37
BASICS OF SERIAL
COMMUNICATION
• At the transmitting end, the byte of data must be converted to
serial bits using parallel-in-serial-out shift register
• At the receiving end, there is a serial-in-parallel-out shift
register to receive the serial data and pack them into byte
• When the distance is short, the digital signal can be
transferred as it is on a simple wire and requires no
modulation
• If data is to be transferred on the telephone line, it must be
converted from 0s and 1s to audio tones
• This conversion is performed by a device called a modem,
“Modulator/demodulator”
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 38
BASICS OF SERIAL
COMMUNICATION
• Serial data communication uses two methods
• Synchronous method transfers a block of data at a time
• Asynchronous method transfers a single byte at a time
Transmitte Receiver
Half r
Duplex Receiver Transmitte
r
Transmitte
Transmitte Receiver
Full rr
Duplex Receiver Transmitte
r
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 40
BASICS OF SERIAL COMMUNICATION
Start and Stop Bits
D7 D0
RS232 Connector
DB-25
groun
d
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 49
BASICS OF SERIAL COMMUNICATION
RS232 Pins
• DTR (data terminal ready)
• When terminal is turned on, it sends out signal DTR to
indicate that it is ready for communication
• DSR (data set ready)
• When DCE is turned on and has gone through the self-test, it
assert DSR to indicate that it is ready to communicate
• RTS (request to send)
• When the DTE device has byte to transmit, it assert RTS to
signal the modem that it has a byte of data to transmit
• CTS (clear to send)
• When the modem has room for storing the data it is to
receive, it sends out signal CTS to DTE to indicate that it can
receive the data now
SM2
• This enables the multiprocessing capability of the 8051
Example 10-5
(cont’)
;-----The message---------------
MYDATA: DB “We Are
Ready”,0 END
8051
P LE
To Tx
D
1 D
PC
COM Rx
P S
Port D
2 W
Nothing
worth having was ever achieved
without effort
Microcontroller BEC405A-Dr. VEENA S K. 01/25/2025 76
The0dore Roosevelt