Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module 3Complete

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Module – 3

8051 Timers/Counters:
Timers/Counters of 8051 Microcontroller ................................................... 2
Basic registers of Timer ................................................................................ 2
Timer – 0 registers .................................................................................. 2
Timer – 1 registers .................................................................................. 2
TMOD (Timer Mode) register .................................................................. 2
TCON (Timer Control) register ................................................................. 5
Mode 1 programming................................................................................... 6
Mode 2 programming................................................................................. 12
Counter Programming ................................................................................ 18
C/𝑇 bit in TMOD register ....................................................................... 19
Mode 1 Counter programming ................................................................... 19
Serial port Programming of 8051 ............................................................... 21
RxD and TxD pins in the 8051 ................................................................ 22
SBUF register......................................................................................... 22
SCON register (98h*) ............................................................................. 22
Setting the Baud rate ............................................................................ 24
3. 7. 5 Programming the 8051 to transfer data serially .................................. 25
3. 7. 6 Programming the 8051 to receive data serially ................................... 30

1
Module - 3 TCE, DSCE
3.1. Timers/Counters of 8051 Microcontroller
The 8051 microcontroller has two Timers/Counters, named Timer 0 (T0) and Timer 1 (T1). They
can be used either as timers to generate a required time delay or as a counter operation to count
external events happening. To operate these timers, knowledge about timer related SFRs are
necessary.

3.2. Basic registers of Timer


Both timer 0 and timer 1 are 16 – bit wide. Since the 8051 has an 8 – bit architecture, each 16 – bit
timer is accessed as two separate registers of low byte (TL) and high byte (TH).

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

TH0 (8Ch) TL0 (8Ah)


Fig 3.1: Timer 0 Registers
Note: Byte addresses are mentioned inside the brackets

D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

TH1 (8Dh) TL1 (8Bh)


Fig 3.2: Timer 1 Registers

3.2.1 Timer – 0 registers

The 16 – bit registers of timer 0 is accessed as low byte and high byte. The low byte is called TL0
(Timer 0 low byte) and the high byte register is referred to as TH0 (Timer 0 high byte). These
registers can be accessed like any other registers with the help of MOV instruction with the help
of its direct SFR addresses.
3.2.2 Timer – 1 registers

Timer 1 is also 16 – bits, and its 16 – bit register is split into two bytes, referred to as TL1 (Timer 1
low byte) and TH1 (Timer 1 high byte). The timer registers for both timers are shown in Fig. 3.1
and 3.2.
3.2.3 TMOD (Timer Mode) register

Both timers (Timer 0 and Timer1) use the same register, called TMOD (Timer mode) register to

2
Module - 3 TCE, DSCE
set the timer in various modes of operation. TMOD, as shown in Fig 3.3, is an 8 – bit register, in
which lower 4 – bits are set aside for Timer 0 and the upper 4 bits for Timer 1. In each case, the
lower 2 bits are used to set the timer mode and the upper 2 bits to specify the operations. This SFR
is accessed with the help of the byte address 89h.
MSB LSB

G C/T M1 M0 G C/T M1 M0

Timer 1 Timer 0

Fig 3.3: TMOD (Timer Mode) Register

Gate(G) Gating control bit.


 When set, the timer /counter is enabled only while INTX pin is high and TRX control pin is
set.
 When cleared, the timer is enabled whenever the TRX pin is set.

C/𝑇̅ Timer or counter selected.


 Cleared for Timer operation. For delay generation using timers, clear this bit.
 Set for counter operation. For counting any external events, set this bit.
M1 & M0 Mode bits 1 & 0. The Timer/Counter can be put in various modes of operation as
mentioned below

M1 M0 Mode Description

0 0 0 13 – Bit timer mode.

0 1 1 16 – bit timer mode


16 – Bit timer/counters with THX and TLX are cascaded.
1 0 2 8 – Bit auto reload.
8 – Bit auto reload timer/counter; THX holds the initial value to be loaded
into TLX each time timer overflows.
1 1 3 Split timer mode.

3
Module - 3 TCE, DSCE
Ex. 3.1 Find the values of TMOD to operate as timers in the following modes:
i). Mode 1 Timer 1
ii). Mode 2 Timer 0, Mode 1 Counter 1
iii). Mode 0 Timer 1
Sol: From Fig. 3.3,
i). TMOD is 0001 0000b = 10h.
ii). TMOD is 0101 0010b = 52h.
iii). TMOD is 0000 0000b = 00h.

̅ı̅m
̅ (Counter/T
C/T ̅e̅r):

This bit in the TMOD register is used to decide whether the timer is used as a delay
generator or an event counter. If C/𝑇̅= 0, it is used as a timer for delay generation. The
clock source for the time delay is the crystal frequency of the 8051.
Clock source for Timer:
Every timer needs a clock pulse to tick (increment). The source of the clock for the
timer operation (C/𝑇̅= 0), is the crystal (XTAL) attached to 8051. This means that the
size of the crystal frequency decides the speed at which the 8051 timer ticks. The
frequency of the timer is always 1 th of the frequency of the crystal attached to the
12

8051.

Timer
XTAL Frequency
Oscillator
÷ 12

Fig 3.4: Timer Frequency

Ex. 3.2 Find the timer’s clock frequency and its period for the various 8051 based
systems, with the following XTAL frequencies.
i). 12 MHz ii). 16 MHz iii). 11.0592 MHz
Sol: i). Timer Frequency = 1 * XTAL = 1 * 12 MHz= 1 MHz
12 12

So, Timer period (or) Timer Cycle (or) Machine cycle = 1 = 1 µs.
𝑇i𝑚𝑒𝑟 𝐹𝑟𝑒𝑞𝑢

4
Module - 3 TCE, DSCE
ii). For XTAL as 16 MHz,
Timer Freq = 1.33MHz & Timer Cycle = 0.75 µs
iii). For XTAL = 11.0592 MHz
Timer Freq = 921.6kHz & Timer Cycle = 1.085 µs
Gate:
The other bit of the TMOD register is the Gate (G) bit. Notice in the TMOD register of
Fig. 3.3 that both Timers 0 & 1 have gate bit. The purpose of this bit is explained
below:
Every timer has a means of starting and stopping. Some timers do this by software,
some by hardware, and some have both software and hardware controls. The timers in
the 8051 have both. The start and stop of the timer are controlled by way of software by
the Timer Run (TRX) bits (TR0 for Timer 0 and TR1 for Timer 1). So for starting the
timers with the help of only TRX pins are achieved by putting the Gate pin to ‘0’. This
way is referred as software control of timers. The hardware way of starting and
stopping the timers by an external source or with the help of a switch is achieved by
making Gate pin to ‘1’.

3.2.4 TCON (Timer Control) register

The bit pattern of Timer control register is shown in Fig. 3.4. Notice that, only the upper
nibble of TCON register have bits needed for timer operations. The Byte address of TCON
register is 88h. This register is a bit addressable SFR.
MSB LSB

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

Timer Control Interrupt Control

Fig 3.4: TCON (Timer Control) Register


The timers are started by using the instructions to set timer start bits TR0 and TR1, which are
called timer run control bits. The timer can be stopped by clearing these timer run control
bits. When a timer counts to its maximum value, it sets a flag named TF0 for Timer 0 and

5
Module - 3 TCE, DSCE
TF1 for Timer 1. The details of the bit pattern of TCON are shown below.

BIT Symbol Bit Address Function

TCON.7 TF1 8Fh Timer 1 overflow flag

TCON.6 TR1 8Eh Timer 1 Run control bit

TCON.5 TF0 8Dh Timer 0 overflow flag

TCON.4 TR0 8Ch Timer 0 Run control bit

3.3 Mode 1 programming


The following are the characteristics and operations of Mode 1 timer:
1. It is a 16 – bit timer; therefore, it allows values of 0000h to FFFFh to be loaded into
the timer’s registers TLX and THX.
2. After TH and TL registers are loaded with 16 – bit initial value, the timer must be
started. This is done by “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
3. The timer registers TH and TL counts up (increments) for every timer cycle until it
reaches its limit of FFFFh. When it rolls over from FFFFh to 0000h, it sets high a
flag bit called TF (Timer overflow flag). This timer flag can be monitored. Once
timer overflows, stop the timer with the instructions “CLR TR0” for timer 0 and
“CLR TR1” for timer 1.
4. After the timer reaches its limit and rolls over, in order to repeat the process the
registers TH and TL must be reloaded with its initial value and TF must be reset to 0.

XTAL
÷ 12
Oscillator TH TL TF

C/T = 0 TF goes high when FFFFh 0000h


TR

Fig 3.5: Mode 1 Timer operation using Software control

6
Module - 3 TCE, DSCE
Steps to program in Mode 1:
To generate a time delay, using the timer’s mode 1, the following steps are taken.
1. Load the TMOD register indicating which timer (Timer 0 or Timer 1) is to be used
and timer in mode 1 is selected.
2. The 16 – bit initial values are loaded in TH and TL registers.
3. Start the timer, by making the timer run bit (TR) to 1.
4. Keep on monitoring the timer flag (TF) with JNB TFX, $ instruction, to see if it is
raised. Get out of the loop when TF becomes high.
5. Stop the timer and clear the timer flag bit.
6. Go back to step 2, to reload the Timer registers and repeat the process.

Ex. 3.3 Write an 8051 ALP to generate a square wave of 2 kHz frequency on Port pin P2.3.
Assume XTAL = 12 MHz. Use Timer0 in Mode 1 of operation.
Sol: Given XTAL = 12MHz.
1 Machine cycle = 1 * 12
⟹1 Machine cycle = 1 * 12 = 1µs. K𝑇𝐴𝐿
12𝑀

Timer 0 in Mode 1:
TMOD = 0000 0001 b = 01h
250µs

250µs

To produce a square wave of 2kHz,


assuming 50% duty cycle on pin P2.3
1 P2.3
TTot = = 0.5ms
2 𝑘𝐻𝑧
t
⟹ TON = TOFF = 𝑇𝑇𝑜𝑡 = 0.25ms
2
0.5ms
So, the pin P2.3 should be complemented
Fig 3.6: Square wave of 2 kHz
forever for 250µs once.
The timer 0 in mode 1 should be configured to produce a delay of 250µs.
The number of timer cycles required to generate 250µs = 250µ = 250 timer cycles.

The 16 bit initial value to be loaded in timer 0 registers:


TH0 TL0
hex
-250d FF 06 h

7
Module - 3 TCE, DSCE
MOV TMOD, #01h ;Timer 0 in Mode 1
Back: MOV TH0, #0FFh ; The 16 – bit initial values are loaded in Timer registers
MOV TL0, #06h
ACALL Delay
CPL P2.3 ;Complement Port pin P2.3
SJMP Back

Delay: SETB TR0 ;Start Timer 0


Wait: JNB TF0, Wait ;Wait till Timer 0 overflows, thereby producing 250µs
CLR TR0 ;Stop Timer 0
CLR TF0 ;Reset the timer 0 Flag bit
RET

Ex. 3.4 Assuming that XTAL = 11.0592 MHz, what 16 – bit value need to be loaded into
timer registers to have a delay of 5ms. Also write an 8051 ALP for Timer 1 to
create a pulse width of 5ms on P1.5
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.

Timer 1 in Mode 1:
TMOD = 0001 0000 b = 10h
The timer 1 in mode 1 should be configured to produce a delay of 5ms.
The number of timer cycles required to generate 5ms = 5𝑚 = 4608 timer cycles.
1.085µ

The 16 bit initial value to be loaded in timer 1 registers:


TH1 TL1
hex
- 4608d EE 00 h

MOV TMOD, #10h ;Timer 1 in Mode 1


Back: MOV TH1, #0EEh ; The 16 – bit initial values are loaded in Timer registers
MOV TL1, #00h

8
Module - 3 TCE, DSCE
ACALL Delay
CPL P1.5 ;Complement Port pin P1.5
SJMP Back

Delay: SETB TR1 ;Start Timer 1


Wait: JNB TF1, Wait ;Wait till Timer 1 overflows, thereby producing 5ms
CLR TR1 ;Stop Timer 1
CLR TF1 ;Reset the timer 1 Flag bit
RET

Ex. 3.5 Generate a square wave with an ON time of 3ms and an OFF time of 10ms on all
pins of port 0. Assume XTAL = 22 MHz. Use Timer 0 in Mode 1 operation.

3ms 10ms

All Bits of P0

Fig 3.7: Square wave with different ON and OFF times

Sol: Given XTAL = 22 MHz


⟹1 Machine cycle = 1 * 12 = 0.545µs.
22𝑀

Timer 0 in Mode 1:
TMOD = 0000 0001 b = 01h
On Time calculations: Off Time calculations:
Delay required = 3ms Delay required = 10ms
Timer cycles required = 3𝑚 = 5505cycles 10𝑚
Timer cycles required = = 22026cycles
0.545µ 0.545µ
TH0 TL0 TH0 TL0
hex hex
- 5505d EA 7F h - 22026d A9 F6 h

MOV TMOD, #01h ;Timer 0 in Mode 1.


Back: MOV TH0, #0A9h ;To generate OFF Time, load A9 F6h in timer registers.
MOV TL0, #0F6h

9
Module - 3 TCE, DSCE
MOV P0, #00h ;Make all bits of P0 as Low.
ACALL Delay
MOV TH0, #0EAh ;To generate ON Time, load EA 7Fh in timer registers.
MOV TL0, #7Fh
MOV P0, #0FFh ; Make all bits of P0 as high.
ACALL Delay
SJMP Back ;Repeat for reloading to get continuous square wave.

Delay: SETB TR0 ;Start Timer 0


Wait: JNB TF0, Wait ;Wait till Timer 0 overflows, thereby producing 5ms
CLR TR0 ;Stop Timer 0
CLR TF0 ;Reset the timer 0 Flag bit
RET

Ex. 3.6 For the following XTAL frequencies, calculate the Maximum delay generated
using 8051 mode 1 timer operations
i). 8 MHz ii). 11.0592 MHz iii). 22 MHZ
Sol: To produce Maximum delay in mode 1 timer operation, the 16 – bit initial value to be
loaded in timer registers be all zeros. i.e., 00 00h
The timer spends 65, 536 timer cycles (2^16) to overflow producing maximum delay
Sl. No XTAL Timer cycle Max. Delay
1. 8MHz 1.5µs 1.5µs * 65536 ≅ 98ms
2. 11.0592MHz 1.085µs 1.085µs * 65536 ≅ 71ms
3. 22MHz 0.545µs 0.545µs * 65536 ≅ 36ms

Ex. 3.7 Assuming XTAL = 22 MHz, write a program to generate a pulse train of 2 seconds on
pin P2.4. Use Timer 1 in mode 1.
Sol: Given XTAL = 22 MHz
⟹1 Machine cycle = 1 * 12 = 0.545µs.
22𝑀

10
Module - 3 TCE, DSCE
Timer 1 in Mode 1:
TMOD = 0001 0000 b = 10h
For a time period of 2 seconds, the

1 Sec

1 Sec
half period should be 1 second. From
Ex. 3.6, it is quite clear that the P2.4

maximum delay possible using mode t


1 timer with XTAL of 22 MHz is
2 Sec
35.71ms, which is very less than the
Fig 3.8: Pulse train of 2 Sec period
required amount of 1 sec delay.
So, configure a delay of lesser than the max. delay, say 25ms, and repeat this delay for ‘n’ times to
get the required amount of 1 sec.
1 sec = 25ms * n
⟹ n = 40
For a Delay of 25 ms: TH1 TL1
hex
Timer cycles required = 25𝑚 = 45,872cycles - 45872d 4C D0 h
0.545µ

MOV TMOD, #10h ;Timer 1 in Mode 1.


Rept: MOV R7, #40 ;Load the ‘n’ value to R register to repeat the delay
Back: MOV TH1, #4Ch ;To generate 25ms, load 4C D0h in timer registers.
MOV TL1, #0D0h
ACALL Delay
DJNZ R7, Back ;If R7 G 0, then repeat the delay of 25 ms.
CPL P2.4
SJMP Rept ;Repeat for reloading to get continuous square wave.

Delay: SETB TR1 ;Start Timer 1


Wait: JNB TF1, Wait ;Wait till Timer 1 overflows, thereby producing 5ms
CLR TR1 ;Stop Timer 1
CLR TF1 ;Reset the timer 1 Flag bit
RET

11
Module - 3 TCE, DSCE
3.4 Mode 2 programming
The following are the characteristics and operations of Mode 2.
1. It is an 8 – bit timer; therefore it allows only values of 00h to FFh to be loaded into
the timer’s register TH.
2. After TH is loaded with the 8 – bit value, the 8051 gives a copy of it to TL. Then
the timer must be started. This is done by the instruction “SETB TR0” for timer 0
and “SETB TR1” for timer 1.
3. Once the timer is started, it start to count up by incrementing the TL register. It
counts up until it reaches it limit of FFh. When it rolls over from FFh to 00h, it sets
high the TF flag.
4. When the TL register rolls from FFh to 00h, and TF is set to 1, TL is reloaded
automatically with its original value kept in TH register. To repeat the process, one
must clear the TF flag, and let it go without the need of programmer to reload the
initial value.
This makes mode – 2 an auto reload, in contrast with mode 1 in which the programmer
has to reload TH and TL registers.

XTAL
÷ 12
Oscillator TL TF

Reload
C/T = 0 TF goes high
TR
TH when FFh 00h

Fig 3.9: Mode 2 Timer operation using Software control

Steps to program timers in mode 2


To generate a time delay using the timer in mode 2, the following steps need to be taken.
1. Load the TMOD register indicating which timer (Timer 0 or Timer 1) is to be used and
select the mode 2 timer.
2. Load the 8 – bit initial value in TH register.
3. Start the timer.
4. Keep on monitoring the timer flag (TF) with JNB TFX, $ instruction, to see if it is
raised. Get out of the loop when TF becomes high.

12
Module - 3 TCE, DSCE
5. Clear the TF flag
6. Go back to step 4, since mode 2 is auto reloaded.

Ex. 3.8 Assuming that XTAL is 22 MHz, write a program to generate a square wave of
frequency 5 kHz on pin P1.2. Use timer 1 in mode 2.
Sol: Given XTAL = 22MHz.
1 Machine cycle = 1 * 12
⟹ 1 Machine cycle = 1 * 12 =
22𝑀 K𝑇𝐴𝐿
0.545µs.
Timer 1 in Mode 2:
TMOD = 0010 0000 b = 20h

100µs

100µs
To produce a square wave of 5kHz,
assuming 50% duty cycle on pin P1.2
1 P1.2
TTot = = 200µs
5 𝑘𝐻𝑧
t
⟹ TON = TOFF = 𝑇𝑇𝑜𝑡 = 100µ
2
200µs
So, the pin P1.2 should be
Fig 3.10: Square wave of 5 kHz
complemented forever for 100µs once.
Delay required = 100µs
Timer cycles required = 100µ = 183 timer cycles.
0.545µ

So, - 183d should be loaded in TH1 register. The assembler will convert the negative value
to its appropriate 8 – bit value and loads into TH1.
MOV TMOD, #20h ; Timer 1 in mode 2
MOV TH1, # - 183 ;Load the initial value in TH1
SETB TR1 ; Start timer 1
Back: ACALL Delay
CPL P1.2
SJMP Back ; Repeat to get continuous square wave.

Delay: JNB TF1, Delay ;Wait till timer overflows, after 183 cycles, TF1 = 1
CLR TF1 ; Clear the overflow flag
RET

13
Module - 3 TCE, DSCE
Ex. 3.9 Assuming that timers are in mode 2 operation, find the value (in hex) loaded into
TH for each of the following cases
i). MOV TH1, # -200 ii). MOV TH0, # -60 iii). MOV TH1, # -3
Sol: Enter -200 in decimal mode and select hex, to get the 2’s complement value. Also
remember to take least 2 hex digits, since mode 2 is an 8 – bit timer operation.
i) -200d = 38h
ii) -60d = C4h
iii). -3d = FDh.

Ex. 3.10 For the following XTAL frequencies, calculate the Maximum delay generated
using 8051 mode 2 timer operations
i). 8 MHz ii). 11.0592 MHz iii). 22 MHZ
Sol: To produce Maximum delay in mode 2 timer operation, the 8 – bit initial value to be
loaded in timer registers be all zeros. i.e., 00h
The timer spends 256 timer cycles (2^8) to overflow producing maximum delay
Sl. No XTAL Timer cycle Max. Delay
1. 8MHz 1.5µs 1.5µs * 256 = 384µs
2. 11.0592MHz 1.085µs 1.085µs * 256 ≅ 278µs
3. 22MHz 0.545µs 0.545µs * 256 ≅ 140µs

Ex. 3.11 Assuming XTAL = 11.0592 MHz, write a program to generate a pulse train of 2 seconds
on pin P2.4. Use Timer 1 in mode 2.
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.
11.0592𝑀

Timer 1 in Mode 2:
TMOD = 0010 0000 b = 20h
For a time period of 2 seconds, the half period should be 1 second. From Ex. 3.10, it is quite
clear that the maximum delay possible using mode 2 timer with XTAL of 11.0592 MHz is
277µs, which is very less than the requires amount of 1 sec delay.
So, configure a delay of lesser than the max. delay, say 250µs, and repeat this delay for ‘n’ times

14
Module - 3 TCE, DSCE
to get the required amount of 1 sec.
1 sec = 250µs * n
⟹ n = 4000
Since, 4000 is > 8 – bits, this can be as a product of two 8 – bit numbers as follows:
4000 = 250 * 16
For a Delay of 250 µs:
Timer cycles required = 250µ = 230cycles
1.085µ

MOV TMOD, #20h ;Timer 1 in Mode 1.


MOV TH1, # - 230 ;Load the initial value in TH1
SETB TR1 ;Start Timer 1
Rept: MOV R7, # 16 ;Load the ‘n’ value to R register to repeat the delay
Outer: MOV R6, # 250
Inner: ACALL Delay
DJNZ R6, Inner ;If R6 G 0, then go to Label inner.
DJNZ R7, Outer ;If R7 G 0, then go to Label outer.
CPL P2.4
SJMP Rept ;Repeat for reloading to get continuous square wave.

Delay: JNB TF1, $ ;Wait till Timer 1 overflows, thereby producing 250µs
CLR TF1 ;Reset the timer 1 Flag bit
RET

Ex. 3.12 Assuming XTAL = 11.0592 MHz, Find


i). the frequency of the square wave generated on pin P1.0
ii). the smallest frequency achievable in the given program and the TH value in doing so.
MOV TMOD, #20h
MOV TH1, # 5
SETB TR1
Back: JNB TF1, S
CPL P1.0

15
Module - 3 TCE, DSCE
CLR TF1
SJMP Back

Sol: i). Based on the value available in TH1, the timer spends 251 timer cycles before rolls over,
producing a delay of 251 * 1.085µ = 272µs.
So, TON = TOFF = 272µs.
TTOT = 272µ + 272µ = 544µs
The frequency of the square wave on pin P1.0 is 1 = 1.83 kHz.
544µ

ii). To get the smallest frequency possible, the delay should be of maximum. This can be
achieved by loading 00h in TH1 register as the initial value.
In this case, TON = TOFF = 256 * 1.085µ = 277.5µs.
TTOT = 277.5µ + 277.5µ = 555µs.
The frequency of the square wave on pin P1.0 is 1 = 1.80 kHz.
555µ

Ex. 3.13 Write an ALP to read input from port 1, complement it and output via port 2. Transfer
should be done once in 50ms, use timer 0 for delay generation and assume XTAL =
11.0592 MHz
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.
11.0592𝑀

Since delay required is 50ms, Mode 1 timer is best suited


Timer 0 in mode 1:
TMOD = 0000 0001b = 01h
Timer cycles required for 50ms = 50𝑚 = 46, 083 cycles.
1.085µ

TH0 TL0
hex
- 46083d 4B FD h
MOV TMOD, #01h ; Timer 0 in mode 1
MOV P0, #0FFh ;P0 as Input port
Back: MOV TH0, #4Bh ; 16 – bit initial value loaded in timer registers
MOV TL0, #0FDh
ACALL Delay

16
Module - 3 TCE, DSCE
MOV A, P1 ; Read port 1 and move to Acc
CPL A ; Complement A
MOV P2, A ; Output via port 2
SJMP Back ; go for next byte transfer.

Delay: SETB TR0 ;Start Timer 0


Wait: JNB TF0, Wait ;Wait till Timer 0 overflows, thereby producing 50ms
CLR TR0 ;Stop Timer 0
CLR TF0 ;Reset the timer 0 Flag bit
RET

Ex. 3.14 A switch ‘SW’ is connected to pin P1.2. Write an 8051 ALP to monitor the switch and
create the following frequencies on P1.7
i. When ‘SW’ = 0; F = 500 Hz
ii. When ‘SW’ = 1; F = 750 Hz.
Use timer 0, mode 1 for both of them. Assume XTAL = 11.0592 MHz.
Sol: Given XTAL = 11.0592 MHz
⟹1 Machine cycle = 1 * 12 = 1.085µs.
11.0592𝑀

TMOD = 0000 0001b = 01h


For 500Hz frequency (When ‘SW’ = 0) For 750 Hz frequency (When ‘SW’ = 1)
TTot = 1 = 2ms TTot = 1 = 1.33ms
500 750

⟹ TON = TOFF = 𝑇𝑇𝑜𝑡 = 1ms ⟹ TON = TOFF = 𝑇𝑇𝑜𝑡 = 0.667ms


2 2

So, the pin P1.7 should be complemented So, the pin P1.7 should be complemented
forever for 1ms once. forever for 0.667ms once.
Timer cycles for 1ms = 1𝑚 = 922cycles Timer cycles for 1ms = 0.667𝑚 = 615cycles
1.085µ 1.085µ

TH0 TL0 TH0 TL0


hex hex
- 922d FC 66 h - 615d FD 99 h

17
Module - 3 TCE, DSCE
MOV TMOD, #01h ; Timer 0 mode 1
SETB P1.2 ; Make Port P1.2 as input pin
Back: JNB P1.2, Fre_500Hz ; Read the switch position
Fre_750Hz: MOV TH0, # 0FDh ; Load the 16 – bit initial value for 750Hz
MOV TL0, # 99h
ACALL Delay
SJMP Last
Fre_500Hz: MOV TH0, # 0FCh ; Load the 16 – bit initial value for 500Hz
MOV TL0, # 66h
ACALL Delay
Last: CPL P1.7
SJMP Back ; Go back to check the status of the switch.

Delay: SETB TR0 ;Start Timer 0


Wait: JNB TF0, Wait ;Wait till Timer 0 overflows,
CLR TR0 ;Stop Timer 0
CLR TF0 ;Reset the timer 0 Flag bit
RET

3.5 Counter Programming


In the previous discussions, the timers of 8051 microcontroller are used for delay generation.
These timers can also be used as an event counter, counting events happening outside the 8051.
The use of timers as an event counter is covered in this section.
 When timer/counter is used as a timer for delay generation, the 8051’s crystal is used as
the source for incrementing the timer registers, as discussed in earlier sections.
 When timer/counter is used as an event counter for counting external events, the pulse
applied to port P3.4 or P3.5 is used to increment the timer registers.
In counter mode, the role of TMOD, THX and TLX remains same as discussed for timer modes.

18
Module - 3 TCE, DSCE
̅ bit in TMOD register
C/T

Recall from the last section, that the C/𝑇̅ bit in the TMOD register decides the source of the
clock for the timer.
 If C/𝑇̅= 0, Timer mode, the timer gets pulses from the crystal connected to the 8051.
 If C/𝑇̅= 1, Counter mode, the timer gets pulses from outside the 8051.
̅ = 1, the counter counts up as pulses are fed from pins 14th and 15th of the 40 – pin
∴, when C/𝑇
DIP. These pins are called T0 (Timer 0 input) and T1 (Timer 1 input). Notice these pins belong to
port 3. In the case of Timer 0, when C/𝑇̅= 1, pin P3.4 provides the clock pulse and the counter
counts up for each of the clock pulse coming from that pin. Similarly, for timer 1, when C/𝑇̅= 1,
each clock pulse coming in from pin P3.5 makes the counter to count up.
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
MSB LSB

G C/T M1 M0 G C/T M1 M0

Timer 1 Timer 0

Fig 3.11: TMOD (Timer Mode) Register

Mode 1 Counter programming


The following are the characteristics and operations of Mode 1 counter:
1. It is a 16 – bit counter; initially, the timer’s registers TLX and THX are set to 00 00h.
2. The counter must be started. This is done by “SETB TR0” for timer 0 and “SETB TR1”
for timer 1.
3. The timer registers TH and TL counts up (increments) for every external clock pulses fed
to P3.4 for Timer 0 and P3.5 for Timer 1.
These clock pulses could be the number of the people passing through the entrance, or the
number of wheel rotation, or any other events that can be converted to pulses.

19
Module - 3 TCE, DSCE
Overflow Flag Overflow Flag
Timer 0 External input Timer 1 External input
P3.4 TH0 TL0 TF0 P3.5 TH1 TL1 TF1

C/T = 1
TR0 C/T = 1
TR1
a). Timer 0 with external input b). Timer 1 with external input

Fig 3.12: Counter in mode 1 Programming

Ex. 3.14 Find the frequency of an input signal fed to pin P3.4. Use XTAL = 22MHz.
Sol: Since external pulse is fed to pin P3.4, the timer 0 should be configured as an event counter.
The frequency is the number of clock pulses per second duration. So Timer 1 should be
configured to generate a delay of 1 second. During this 1 second, Timer 0 is to run as a 16 –bit
event counter, as the input pulse is fed to P3.4. At the end of one sec, the values in TL0 and TH0
give the number of pulses that were received at pin p3.4 during this second. This gives the
frequency of the unknown signal.
TMOD = 0 0 0 1 0 1 0 1 b = 15h; T0 as Mode – 1 Event counter & T1 as Mode – 1 timer.
Given XTAL = 22MHz ⟹ 1 machine cycle = 0.545µs
Max. delay for timers in mode 1 = 216 machine cycle = 65 536 * 0.545µ = 35.71ms.
So, configure a delay of lesser than the max. delay, say 25ms, and repeat this delay for ‘n’ times
to get the required amount of 1 sec.
1 sec = 25ms * n
⟹ n = 40
For a Delay of 25 ms: TH1 TL1
hex
Timer cycles required = 25𝑚 = 45,872cycles - 45872d 4C D0 h

ORG 0000h
RPT: MOV TMOD, #15h ;Timer 0 as counter and Timer 1 as timer for 1 sec delay gen.
SETB P3.4 ; make P3.4 as input port.
MOV TL0, # 00h ;set the 16 – bit initial values in Timer 0 registers as 00 00h
MOV TH0, # 00h
SETB TR0 ;Start the Timer 0(Counter operation)

20
Module - 3 TCE, DSCE
MOV R0, # 40
Again: MOV TL1, # 0D0h ; Initialize the 16 – bit value in timer registers.
MOV TH1, # 4Ch
SETB TR1 ;Start timer 1
Wait: JNB TF1, $ ;Wait till timer 1 overflows producing 25ms delay
CLR TR1 ;Stop timer 1
CLR TF1 ;Clear timer 1 overflow flag
DJNZ R0, Again ;Repeat 25ms delay for 40 times.
MOV A, TL0
MOV P2, A ;Move TL0 to P2 and TH0 to P1 which is connected to some
MOV A, TH0 ;output devices
MOV P1, A
SJMP RPT ;Repeat this process
END

3.7 Serial port Programming of 8051


When a microprocessor communicates with the outside world, it provides the data in byte-sized
chunks. In some cases, such as printers, the information is simply grabbed from the 8 – bit data
bus and presented to the 8 – bit data bus of the printer. This can work only if the cable is not too
long, since long cables diminish and even distort signals. Further an 8 – bit data path is
expensive. For these reasons, serial communication is used for transferring data between two
systems located at distances of hundreds of feet to millions of miles apart. Fig 3.13 diagrams
serial vs. parallel data transfers.
D7

Sender Receiver Sender Receiver


D0

a). Serial Transfer b). Parallel Transfer

Fig 3.13: Serial vs Parallel data transfer

21
Module - 3 TCE, DSCE
3.7.1 RxD and TxD pins in the 8051

The 8051 has 2 pins that are used specially for transferring and receiving data serially. These two
pins are called TxD and RxD and are part of port 3 group (P3.0 and P3.1). Pins 11 of the 8051
(P3.1) is assigned to TxD and pin 10 (P3.0) is designated as RxD.
3.7.2 SBUF register
SBUF is an 8 – bit register used only for serial communication in the 8051. For a byte of data to
be transferred via TxD pin, it must be placed in SBUF. Similarly, SBUF holds the byte of the
data when it is received by the RxD pin of 8051. The moment a byte is placed in SBUF, it is
framed with the start bit before D0 bit and stop bit after D7 bit of the data and transferred serially
via TxD pin. Similarly, when bits are received serially via RxD pin, the 8051 de frames it by
eliminating the start and stop bits, and then places the received byte in SBUF
3.7.3 SCON register (98h*)
Fig 3.14 shows the SCON SFR, which is at the address 98h (a bit addressable SFR).

SM0 SM1 SM2 REN TB8 RB8 TI RI

Fig 3.14: SCON register (98h*)

SM0 SCON.7 Serial mode specifier


SM1 SCON.6 Serial mode specifier
SM2 SCON.5 Used for Multiprocessor mode; make as ‘0’
REN SCON.4 Set/cleared by software to enable/disable reception
TB8 SCON.3 Not widely used
RB8 SCON.2 Not widely used
TI SCON.1 Transmit interrupt flag.
 Set by hardware at the beginning of the stop bit in mode 1.
 Must be cleared by software.
RI SCON.0 Receive interrupt flag.
 Set by hardware at the halfway through the stop bit in mode 1.
 Must be cleared by software.
Note: Make SM2, TB8, RB8 as ‘0’

22
Module - 3 TCE, DSCE
SM0, SM1
SM0 and SM1 are D7 and D6 bits of the SCON register, respectively. These bits determine the
framing of data by specifying the number of bits per character, and the start and stop bits. They
take the following combinations:

SM0 SM1 Modes of operation


0 0 Serial Mode 0
0 1 Serial Mode 1 ;8 –bit data with 1 stop bit and 1 start bit
1 0 Serial Mode 2
1 1 Serial Mode 3
Of the 4 serial modes, only mode 1 is of interest to us. In the SCON register, when serial mode 1
is chosen, the data framing is 8 – bits, 1 stop bit and one start bit, as shown in fig. 3.15.
One frame of data

Stop 1 1 Start Mark


Space 0 0 0 0 0 0
bit bit

D7
Goes out last Goes out first
Fig 3.15: Framing ASCII “A” (41h) with start and stop bit
More importantly, serial mode 1 allows the baud rate to be variable and is set by the timer 1 of
the 8051. In serial mode 1, for each character a total of 10 bits are transferred, where the first bit
is the start bit, followed by 8 bits of data and finally 1 stop bit.
SM2
SM2 is the D5 bit of SCON register. This bit enables the multiprocessor capability of the 8051
and for our discussion, set SM2 as ‘0’.
REN
The REN (receive enable) bit is D4 of the SCON register. The REN bit is also referred to as
SCON.4 since SCON is a bit addressable SFR.
when REN = 1: UART is enabled

23
Module - 3 TCE, DSCE
when REN = 0; UART is disabled.
So, by making REN as 1, 8051 can transfer and receive data serially via, TxD and RxD pins.
TB8 and RB8
TB8 (Transfer bit) and RB8 (Receive bit) are D3 and D2 bits of SCON register. We make these
bits as ‘0’, since it is used for serial modes 2 and 3.
TI
TI (Transmit interrupt) is the D1 bit of SCON register. This is an extremely important flag bit in
the SCON register. When the 8051 finishes the transfer of the 8 – bit character, it raises the TI
flag to indicate that UART is ready to transfer next character. The TI is raised by 8051 at the
beginning of the stop bit.
RI
RI (Receive interrupt) is the D0 bit of SCON register. This is another extremely important flag
bit in the SCON register. When the 8051 receives data serially via RxD, it get rid of start and
stop bits and places the byte in SBUF register. Then it raises the RI flag to indicate that a byte is
received serially. The RI is raised by 8051 at the halfway through the stop bit.
3.7.4 Setting the Baud rate

The rate of data transfer in serial data communication is stated in bps (bits per second). Another
widely used terminology for bps is baud rate.
The 8051 transfers and receives data serially at many different baud rates. The baud rate in serial
mode 1 is variable and can be set by timer 1 in mode 2 operation.

XTAL Machine cycle freq. ÷32 28,800 Hz


÷12
Oscillator 921.6kHz by UART To Timer 1 for setting
the Baud rate
11.0592MHz
Fig 3.16: 8051 UART – Setting the Baud rate

As shown in fig 3.16, the 8051 divides the crystal frequency by 12 to get the machine cycle
frequency. In the case of XTAL as 11.0592 MHz, the machine cycle frequency is 921.6 kHz. The
8051 serial communication UART circuitry divides the machine cycle frequency by 32 before
given to timer 1 to set the baud rate. Therefore, 921.6 kHz divided by 32 gives 28, 800 Hz. This
frequency can be divided by a value to get compatible baud rates. For instance, if 9600 baud rate
is needed, -3d should be loaded to TH1meaning dividing the UART frequency by 3 times.

24
Module - 3 TCE, DSCE
Baud Rate TH1 (decimal) TH1 (Hex)
9600 -3 FD
4800 -6 FA
2400 -12 F4
1200 -24 E8

Ex. 3.15 With XTAL = 11.0592 MHz, find the TH1 value needed to have the following baud
rates.
a). 9600 b). 4800 c). 2400
Sol: With XTAL = 11.0592 MHz,
⇒ Machine cycle frequency = K𝑇𝐴𝐿 = 11.0592𝑀 = 921.6 kHz
12 12

and UART frequency = 𝑀𝑎𝑐ℎi𝑛𝑒 𝑐𝑦𝑐𝑙𝑒 𝐹𝑟𝑒𝑞 = 921.6 𝑘 = 28, 800Hz.


32 12

a). 28,800/3 = 9600; where -3d = FDh is loaded in TH1


b). 28,800/6 = 4800; where -6d = FAh is loaded in TH1
c). 28,800/12 = 2400; where -12d = F4h is loaded in TH1.

3. 7. 5 Programming the 8051 to transfer data serially

In programming the 8051 to transfer character bytes serially, the following steps must be taken
1. The TMOD register is loaded with the value 20h, indicating the use of Timer in mode 2
(8 – bit auto reload) for setting the baud rate.
2. The TH1 is loaded with the one of the values in the above table to set the baud rate for
serial data transfer.
3. The SCON is loaded with 50h, indicating serial mode 1, where an 8 – bit data is framed
with start and stop bits.
4. TR1 is set to 1 to start Timer 1.
5. The character byte to be transferred serially is placed in SBUF register.
6. The TI flag is monitored with the use of JNB TI, $ instruction to check whether all the 8
– bits in the character is completely transferred.
7. Clear TI flag and repeat step 5 for next transfer.

25
Module - 3 TCE, DSCE
Ex. 3.16 Write a program to transfer a letter ‘Y’ serially at 9600 baud rate continuously,
and also send a letter ‘N’ through port 0, which is connected to a display device.
Sol Here one byte of data (ASCII value
‘Y’
of ‘Y’, which is 59h) is transmitted P3.1
(TX
serially via TxD pin (P3.1), and )
another is transmitted in parallel 8051
‘N’
(ASCII value of ‘N’, which is 4Eh) P0 DISPLAY
via P0 pins.

Note: relate the ALP written below with the step sequence stated above in section3.7.
3.7.4
MOV TMOD, # 20h 5
; Timer 1, Mode 2 (Auto reload operation)
MOV TH1, # -3 ; 9600 Baud rate
MOV SCON , # 50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
Again: MOV SBUF, # ’Y’ ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
MOV P0, # ’N’ ; Move ‘N’ to P0 for parallel transfer
SJMP Again ; Repeat this process for ever.

Ex. 3.17 Take data in through ports 0, 1, and 2, one after the other and transfer the data
serially, continuously at a baud rate of 4800.
Sol: MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)
MOV TH1, # -6 ; 4800 Baud rate
MOV SCON, # 50h ;Serial mode 1, 8 – bit UART with start and stop bits
MOV P0, # 0FFh ;Make P0 as an input port
MOV P1, # 0FFh ;Make P1 as an input port
MOV P2, # 0FFh ;Make P2 as an input port
SETB TR1 ;Start timer 1
Rpt: MOV A, P0 ; Read P0 pins
ACALL Send ; Call the subroutine
MOV A, P1 ; Read P1 pins

26
Module - 3 TCE, DSCE
ACALL Send ; Call the subroutine
MOV A, P2 ; Read P2 pins
ACALL Send ; Call the subroutine
SJMP Rpt Repeat this process for ever
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
RET ; Return to the main program

Ex. 3.18 Write an 8051 ALP to transmit serially the message “Welcome” at a baud rate of
9600, and assume XTAL freq as 11.0592 MHz
Sol: MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)
MOV TH1,# -3 ; 9600 Baud rate
MOV SCON, #50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
MOV DPTR, # Msg ;Load pointer for the message
Rpt: CLR A
MOVC A, @A+DPTR ;Get the character
JZ Last ; If last character, get out
ACALL Send ; Otherwise call the subroutine
INC DPTR ; Point the next character
SJMP Rpt ; Repeat the process
Last: SJMP $
Msg: DB “Welcome” 0 ; Define Byte the message with last character as null
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
RET ; Return to the main program

27
Module - 3 TCE, DSCE
Ex. 3.19 Write an 8051 ALP to transmit serially the message “SWITCH ON” or
“SWITCH OFF”, depending on the status of the simple switch connected to pin
P1.2. Use 2400 Baud rate, and assume XTAL freq as 11.0592 MHz
Sol
SW
SW = 1; “SWITCH ON”
P1.2 P3.1
8051 (TX SW = 0; “SWITCH OFF”
)

MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)

MOV TH1,# -12 ; 9600 Baud rate


MOV SCON, #50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
SETB P1.2 ; Make P1.2 as input pin
Rpt: JNB P1.2, SW_OFF ; Check for the switch position
SW_ON: MOV DPTR, # Msg1 ; Load pointer for Switch on
SJMP Trans
SW_OFF: MOV DPTR, # Msg2 ; Load pointer for Switch off
Trans: CLR A
MOVC A, @A+DPTR ;Get the character
JZ Last ; If last character, get out
ACALL Send ; Otherwise call the subroutine
INC DPTR ; Point the next character
SJMP Trans ; Repeat the process
Last: SJMP Rpt ; Read the switch position
Msg1: DB “SWITCH ON” 0
Msg2: DB “SWITCH OFF” 0
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
RET ; Retun to the main program

28
Module - 3 TCE, DSCE
Ex. 3.20 Write a program in assembly language for 8051 to send 2 messages “Normal
Speed” and “High Speed” to the serial port. Assuming that a SW is connected to
pin P2.0, monitor its status and set the baud rate as follows:
when SW = 0; MSG1 at 4800 Baud rate
when SW = 1; MSG2 at 9600 Baud rate
Sol: VCC

SW SW = 1; “High Speed @9600 Baud rate”


P2.0 P3.1
8051
SW = 0; “Normal Speed @4800 Baud rate”

; Timer 1, Mode 2 (Auto reload operation)

MOV TMOD, # 20h


MOV SCON, #50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB 2.0 ; Make P2.0 as input pin
Rpt: JNB P2.0, SW_OFF
SW_ON: MOV TH1,# -3 ; 9600 Baud rate
SETB TR1 ;Start timer 1
MOV DPTR, # Msg1 ;Place DPTR pointing High speed message
SJMP Trans
SW_OFF: MOV TH1,# -6 ; 4800 Baud rate
SETB TR1 ;Start timer 1
MOV DPTR, # Msg2 ;Place DPTR pointing Normal speed message
Trans: CLR A
MOVC A, @A+DPTR ;Get the character
JZ Last ; If last character, get out
ACALL Send ; Otherwise call the subroutine
INC DPTR ; Point the next character
SJMP Trans ; Repeat the process

29
Module - 3 TCE, DSCE
SJMP Rpt ; Read the switch position

Msg1: DB “High Speed” 0

(TX
)

30
Module - 3 TCE, DSCE
Msg2: DB “Normal Speed” 0
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Here: JNB TI, Here ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer
RET ; Return to the main program

Ex. 3.21 Write an 8051 ALP to send 2 different strings to the serial port. Assuming that a
SW is connected to pin P2.5, monitor its status and make a decision as follows:
when SW = 0; Send your first name
when SW = 1; Send your last name;
Assume XTAL = 11.0592 MHz, and a baud rate of 9600.
Sol: VCC

SW
SW = 0; “First name”
P2.5 P3.1
8051 (TX
) SW = 1; “Last name”

* Refer Ex. 3.19 for Solution.

3. 7. 6 Programming the 8051 to receive data serially

In programming the 8051 to transfer character bytes serially, the following steps must be taken
1. The TMOD register is loaded with the value 20h, indicating the use of Timer in mode 2
(8 – bit auto reload) for setting the baud rate.
2. The TH1 is loaded with the one of the values in the above table to set the baud rate.
3. The SCON is loaded with 50h, indicating serial mode 1, where an 8 – bit data is framed
with start and stop bits.
4. TR1 is set to 1 to start Timer 1.
5. The RI flag is monitored with the use of JNB RI, $ instruction to check whether all the 8
– bits in the character is completely received.

31
Module - 3 TCE, DSCE
6. When RI is raised, SBUF has the byte. Its contents are moved into a safe place, like
accumulator.
7. Clear RI flag and repeat step 5 to receive next character.

Ex. 3.22 Write an 8051 ALP to receive the data which has been sent in serial form and
sent it out to port 0 in parallel form. Also save the data at RAM loc. 60h.
Sol:
‘Y’
R P3.0
x

8051
Y
P0 DISPLAY
60h Y

Let the data received via RxD pin is the ASCII value of character ‘Y’ (59h). When the
8051 receives data serially via RxD, it gets rid of start and stop bits and places the byte in
SBUF register. Then it raises the RI flag to indicate that a byte is received serially. The
below program demonstrates this theory of receiving the data serially.
MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)
MOV TH1, # -3 ; 9600 Baud rate
MOV SCON, # 50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
Wait: JNB RI, Wait ;Wait till all the bits in the character is received
CLR RI ;RI flag is cleared
MOV A, SBUF ; Received data, which is available in SBUF, is moved to A
MOV P0, A ; Send to P0 in parallel & and store in 60h internal RAM
MOV 60h, A
SJMP $

Ex. 3.23 Assume that the serial port is connected to the COM port of the IBM PC, and on
the PC we are using the hyper Terminal program to send and receive data serially.
P1 and P2 of the 8051 are connected to LEDs and switches, respectively. Write an
8051 program to
a). Send to the PC, the message “We are READY”,

32
Module - 3 TCE, DSCE
b). Receive any data sent by the PC and put it on the LEDs connected to P1, and
c). Get data on switches connected to P2 and sent it to PC serially.
The program should perform part (a) once, but parts (b) and (c) continuously. Use
4800 baud rate.
Sol:
LEDs
To PC P1
TxD
8051
From
RxD SW
COM Port P2

MOV P2, #0FFh ;Make P2 as input port


MOV TMOD, # 20h ; Timer 1, Mode 2 (Auto reload operation)
MOV TH1, # -6 ; 4800 Baud rate
MOV SCON, # 50h ;Serial mode 1, 8 – bit UART with start and stop bits
SETB TR1 ;Start timer 1
MOV DPTR, #Msg Place DPTR pointing initial We are READY message
Init: CLR A
MOVC A, @A+DPTR ;Get the character
JZ Rpt ; If last character, get out
ACALL Send ; Otherwise call the subroutine
INC DPTR ; Point the next character
SJMP Init ; Repeat the process
Rpt: MOV A, P2 ;Read port 2 switch positions and place it in A
ACALL Send ; Send it to PC serially.
ACALL Recev ; Receive Data serially from PC and store it in ACC
MOV P1, A ; send the data to LEDs connected to P1
SJMP Rpt ; Repeat this process for ever.
; Transferring data serially – subroutine
Send: MOV SBUF, A ;place the character in SBUF to transmit serially
Wait: JNB TI, Wait ;Wait till all the bits in the character is transferred
CLR TI ; Clear the TI flag for next serial transfer

33
Module - 3 TCE, DSCE
RET ; Return to the main program
; Receiving data serially – subroutine
Recev: JNB RI, Recev ;Wait till all the bits in the character is received
MOV A, SBUF ;RI flag is cleared
CLR RI ; Received data, which is available in SBUF, is moved to A
RET ; Return to the main program

Msg: DB “We are READY” 0


END

34
Module - 3 TCE, DSCE

You might also like