0% found this document useful (0 votes)
69 views

Serial Port Programming-M3

This document discusses asynchronous serial communication and programming the serial ports of the 8051 microcontroller. It explains data framing with start and stop bits, and how the 8051 uses its SBUF and SCON registers to transmit and receive serial data at programmable baud rates set by Timer 1. Registers like SBUF hold data for transmission and reception, while SCON is used to program the data frame format and enable the receiver. Baud rates compatible with PCs can be achieved by loading appropriate values into Timer 1.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Serial Port Programming-M3

This document discusses asynchronous serial communication and programming the serial ports of the 8051 microcontroller. It explains data framing with start and stop bits, and how the 8051 uses its SBUF and SCON registers to transmit and receive serial data at programmable baud rates set by Timer 1. Registers like SBUF hold data for transmission and reception, while SCON is used to program the data frame format and enable the receiver. Baud rates compatible with PCs can be achieved by loading appropriate values into Timer 1.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

MODULE-3

Serial port programming in assembly and C:


8051 serial port programming in assembly language, Programming the second serial port, Serial
port programming in 8051 C.

1
DAKSHAYINI M R
Asynchronous serial communication and data framing
The data coming in at the receiving end of the data line in a serial data transfer is all Os and Is; it
is difficult to make sense of the data unless the sender and receiver agree on a set of rules,

a protocol, on how the data is packed, how many bits constitute a character, and when the data
begins and ends.
Start and stop bits
Asynchronous serial data communication is widely used for character-oriented transmissions,
while block-oriented data transfers use the synchronous method. In the asynchronous method,
each character is placed between start and stop bits. This is called framing. In data framing for
asynchronous communications, the data, such as ASCII characters, are packed between a start bit
and a stop bit. The start bit is always one bit, but the stop bit can be one or two bits. The start bit
is always a 0 (low) and the stop bit(s) is 1 (high). For example, look at Figure 10-3 in which the
ASCII character “A” (8-bit binary 0100 0001) is framed between the start bit and a single stop
bit. Notice that the LSB is sent out first.

Figure 10-3. Framing ASCII “A” (41H)

Notice in Figure 10-3 that when there is no transfer, the signal is 1 (high), which is referred to
as mark. The 0 (low) is referred to as space. Notice that the transmission begins with a start bit
followed by DO, which is the LSB, then the rest of the bits until the MSB (D7), and finally, the
one stop bit indicating the end of the character “A”.

In asynchronous serial communications, peripheral chips and modems can be programmed for
data that is 7 or 8 bits wide. This is in addition to the number of stop bits, 1 or 2. While in older
systems ASCII characters were 7-bit, in recent years, due to the extended ASCII characters, 8-bit
data has become common. In some older systems, due to the slowness of the receiving
mechanical device, two stop bits were used to give the device sufficient time to organize itself
before transmission of the next byte. In modern PCs however, the use of one stop bit is standard.
Assuming that we are transferring a text file of ASCII characters using 1 stop bit, we have a total
of 10 bits for each character: 8 bits for the ASCII code, and 1 bit each for the start and stop bits.
Therefore, for each 8-bit character there are an extra 2 bits, which gives 20% overhead.

In some systems, the parity bit of the character byte is included in the data frame in order to
maintain data integrity. This means that for each character (7- or 8-bit, depending on the system)
we have a single parity bit in addition to start and stop bits. The parity bit is odd or even. In the
case of an odd-parity bit the number of data bits, including the parity bit, has an odd number of
Is. Similarly, in an even-parity bit system the total number of bits, including the parity bit, is
even. For example, the ASCII character “A”, binary 0100 0001, has 0 for the even-parity bit.
2
DAKSHAYINI M R
UART chips allow programming of the parity bit for odd-, even-, and no-parity options.

Data transfer 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. However, the baud and bps rates are not
necessarily equal. This is due to the fact that baud rate . is the modem terminology and is defined
as the number of signal changes per second. In modems a single change of signal, sometimes
transfers several bits of data. As far as the conductor wire is concerned, the baud rate and bps are
the same, and for this reason in this book we use the terms bps and baud interchangeably.

The data transfer rate of a given computer system depends on communication ports incorporated
into that system. For example, the early IBM PC/XT could transfer data at the rate of 100 to
9600 bps. In recent years, however, Pentium-based PCs transfer data at rates as high as 56K bps.
It must be noted that in asynchronous serial data communication, the baud rate is generally
limited to 100,000 bps.

8051 SERIAL PORT PROGRAMMING IN ASSEMBLY

In this section we discuss the serial communication registers of the 8051 and show how to
program them to transfer and receive data serially. Since IBM PC/compatible computers are so
widely used to communicate with 8051-based systems, we will emphasize serial communications
of the 8051 with the COM port of the PC. To allow data transfer between the PC and an 8051
system without any error, we must make sure that the baud rate of the 8051 system matches the
baud rate of the PC’s COM port. Some of the baud rates supported by PC BIOS are listed in
Table 10-3. You can examine these baud rates by going to the Windows Hyper Terminal

3
DAKSHAYINI M R
program and clicking on the Communication Settings option. The HyperTerminal program
comes with Windows. HyperTerminal supports baud rates much higher than the ones listed in
Table 10-3.

Baud rate in the 8051


The 8051 transfers and receives data serially at many different baud rates. The baud rate in the
8051 is programmable. This is done with the help of Timer 1. Before we discuss how to do that,
we will look at the relationship between the crystal frequency and the baud rate in the 8051.

As discussed in previous chapters, the 8051 divides the crystal frequency by 12 to get the
machine cycle frequency. In the case of XTAL = 11.0592 MHz, the machine cycle frequency is
921.6 kHz (11.0592 MHz / 12 = 921.6 kHz). The 8051 ‘s serial communication UART circuitry
divides the machine cycle frequency of 921.6 kHz by 32 once more before it is used by Timer 1
to set the baud rate. Therefore, 921.6 kHz divided by 32 gives 28,800 Hz. This is the number we
will use throughout this section to find the Timer 1 value to set the baud rate. When Timer 1 is
used to set the baud rate it must be programmed in mode 2, that is 8-bit, auto-reload. To get baud
rates compatible with the PC, we must load TH1 with the values shown in Table 10-4. Example
10-1 shows how to verify the data in Table 10-4.

Note: XTAL = 11.0592 MHz.

Example 10-1With XTAL = 11.0592 MHz, find the TH1 value needed to have the following
baudrates. (a) 9600 (b) 2400 (c) 1200
Solution:With XTAL = 11.0592 MHz, we have:
The machine cycle frequency of the 8051 = 11.0592 MHz / 12 = 921.6 kHz, and 921.6 kHz / 32
= 28,800 Hz is the frequency provided by UART to Timer 1 to set baud rate.

Notice that 1/12th of the crystal frequency divided by 32 is the default value upon activation of
the 8051 RESET pin. We can change this default setting. This is explained at the end of this
chapter.
11.0592MHz

4
DAKSHAYINI M R
SBUF register
SBUF is an 8-bit register used solely for serial communication in the 8051. For a byte of data to
be transferred via the TxD line, it must be placed in the SBUF register. Similarly, SBUF holds
the byte of data when it is received by the 8051 ‘s RxD line. SBUF can be accessed like any
other register in the 8051. Look at the following examples of how this register is accessed:

The moment a byte is written into SBUF, it is framed with the start and stop bits and transferred
serially via the TxD pin. Similarly, when the bits are received serially via RxD, the 8051
deframes it by eliminating the stop and start bits, making a byte out of the data received, and then
placing it in the SBUF.

SCON (serial control) register


The SCON register is an 8-bit register used to program the start bit, stop bit, and data bits of data
framing, among other things.
The following describes various bits of the SCON register.

Figure 10-9. SCON Serial Port Control Register (Bit-Addressable)


SMO, SM1
SMO and SMI are D7 and D6 of the SCON register, respectively. These two 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.

5
DAKSHAYINI M R
Of the 4 serial modes, only mode I is of interest to us. Further explanation for the other three
modes is in Appendix A.2. They are rarely used today. In the SCON register, when serial mode 1
is chosen, the data framing is 8 bits, 1 stop bit, and 1 start bit, which makes it compatible with
the COM port of IBM/compatible PCs. More importantly, serial mode 1 allows the baud rate to
be variable and is set by 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 the SCON register. This bit enables the multiprocessing capability of the
8051 and is beyond the discussion of this chapter. For our applications, we will make SM2 = 0
since we are not using the 8051 in a multiprocessor environment.
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 register. When the REN bit is high, it allows the 8051
to receive data on the RxD pin of the 8051. As a result if we want the 8051 to both transfer and
receive data, REN must be set to 1. By making REN = 0, the receiver is disabled. Making
REN — 1 or REN = 0 can be achieved by the instructions “SETB SCON. 4″ and “CLR SCON.
4″, respectively. Notice that these instructions use the bit-addressable features of register SCON.
This bit can be used to block any serial data reception and is an extremely important bit in the
SCON register.
TBS
TBS (transfer bit 8) is bit D3 of SCON. It is used for serial modes 2 and 3. We make TBS = 0
since it is not used in our applications.
RB8
RB8 (receive bit 8) is bit D2 of the SCON register. In serial mode 1, this bit gets a copy of the
stop bit when an 8-bit data is received. This bit (as is the case for TBS) is rarely used anymore.
In all our applications we will make RB8 = 0. Like TB8, the RB8 bit is also used in serial modes
2 and 3.
Tl
TI (transmit interrupt) is bit Dl of the 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 it is ready to transfer another byte. The TI bit is raised at the beginning of the stop
bit. We will discuss its role further when programming examples of data transmission are given.
Rl
RI (receive interrupt) is the DO bit of the SCON register. This is another extremely important
flag bit in the SCON register. When the 8051 receives data serially via RxD, it gets rid of the
start and stop bits and places the byte in the SBUF register. Then it raises the RI flag bit to
indicate that a byte has been received and should be picked up before it is lost. RI is raised
halfway through the stop bit, and we will soon see how this bit is used in programs for receiving
data serially.

6
DAKSHAYINI M R
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
1 in mode 2 (8-bit auto-reload) to set the baud rate.
2. The TH1 is loaded with one of the values in Table 10-4 to set the baud rate for
serial data transfer (assuming XTAL = 11.0592 MHz).
3. The SCON register is loaded with the value 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. TI is cleared by the “CLR TI” instruction.
6. The character byte to be transferred serially is written into the SBUF register.
7. The TI flag bit is monitored with the use of the instruction ” JNB TI, xx” to
see if the character has been transferred completely.
8. To transfer the next character, go to Step 5.

Example: shows a program to transfer data serially at 4800 baud. Example 10-3 showshow
to transfer “YES” continuously.

Example :Write a program to transfer the message “YES” serially at 9600 baud, 8-bit
data, 1 stop bit. Do this continuously.

7
DAKSHAYINI M R
Importance of the Tl flag
To understand the importance of the role of TI, look at the following sequence of steps that
the 8051 goes through in transmitting a character via TxD.
1. The byte character to be transmitted is written into the SBUF register.
2. The start bit is transferred.
3. The 8-bit character is transferred one bit at a time.
4. The stop bit is transferred. It is during the transfer of the stop bit that the 8051
raises the TI flag (TI =1), indicating that the last character was transmitted
and it is ready to transfer the next character.
5. By monitoring the TI flag, we make sure that we are not overloading the SBUF
register. If we write another byte into the SBUF register before TI is raised, the
untransmitted portion of the previous byte will be lost. In other words, when the 8051
finishes transferring a byte, it raises the TI flag to indicate it is ready for the next
character.
6. After SBUF is loaded with a new byte, the TI flag bit must be forced to 0 by the “CLR
TI” instruction in order for this new byte to be transferred.

From the above discussion we conclude that by checking the TI flag bit, we know whether or
not the 8051 is ready to transfer another byte. More importantly, it must be noted that the TI
flag bit is raised by the 8051 itself when it finishes the transfer of data, whereas it must be
cleared by the programmer with an instruction such as “CLR TI”. It also must be noted that if
we write a byte into SBUF before the TI flag bit is raised, we risk the loss of a portion of the
byte being transferred. The TI flag bit can be checked by the instruction “JNB TI, . . .” or we
can use an interrupt, as we will see in Chapter 11. In Chapter 11 we will show how to use
interrupts to transfer data serially, and avoid tying down the microcontroller with instructions
such as “JNB TI, xx”.

8
DAKSHAYINI M R
Programming the 8051 to receive data serially:
In the programming of the 8051 to receive character bytes serially, the following steps must
be taken.
1. The TMOD register is loaded with the value 20H, indicating the use of Timer 1 in mode
2 (8-bit auto-reload) to set the baud rate.
2. TH1 is loaded with one of the values in Table 10-4 to set the baud rate (assuming XTAL
= 11.0592MHz).
3. The SCON register is loaded with the value 50H, indicating serial mode 1, where 8-bit
data is framed with start and stop bits and receive enable is turned on.
4. TR1 is set to 1 to start Timer 1.
5. RI is cleared with the “CLR RI” instruction.
6. The RI flag bit is monitored with the use of the instruction “JNB RI, xx” to
see if an entire character has been received yet.
7. When RI is raised, SBUF has the byte. Its contents are moved into a safe place.
8. To receive the next character, go to Step 5.

Example :Program the 8051 to receive bytes of data serially, and put them in PI. Set the
baud rate at 4800, 8-bit data, and 1 stop bit.

Example :Assume that the 8051 serial port is connected to the COM port of the IBM PC, and
on the PC we are using the HyperTerminal program to send and receive data serially. PI 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”, (b) receive any data sent by the PC and put
it on LEDs connected to PI, and (c) get data on switches connected to P2 and send it to the
PC serially. The program should perform part (a) once, but parts (b) and (c) continuously.
Use the 4800 baud rate.

9
DAKSHAYINI M R
Importance of the Rl flag bit
In receiving bits via its RxD pin, the 8051 goes through the following steps.
1. It receives the start bit indicating that the next bit is the first bit of the character
byte it is about to receive.
2. The 8-bit character is received one bit at time. When the last bit is received, a byte is
formed and placed in SBUF.
3. The stop bit is received. When receiving the stop bit the 8051 makes RI = 1indicating
that an entire character byte has been received and must be picked up before it gets
overwritten by an incoming character.
4. By checking the RI flag bit when it is raised, we know that a character has been
received and is sitting in the SBUF register. We copy the SBUF contents to a safe
place in some other register or memory before it is lost.
5. After the SBUF contents are copied into a safe place, the RI flag bit must be forced
to 0 by the “CLR RI” instruction in order to allow the next received character byte to
be placed in SBUF. Failure to do this causes loss of the received character.

10
DAKSHAYINI M R
From the above discussion we conclude that by checking the RI flag bit we know
whether or not the 8051 has received a character byte. If we fail to copy SBUF into a
safe place, we risk the loss of the received byte. More importantly, it must be noted that
the RI flag bit is raised by the 8051, but it must be cleared by the programmer with an
instruction such as “CLR RI”. It also must be noted that if we copy SBUF into a safe
place before the RI flag bit is raised, we risk copying garbage. The RI flag bit can be
checked by the instruction “JNB RI, xx” or by using an interrupt, as we will see in
Chapter 11.

Doubling the baud rate in the 8051:


There are two ways to increase the baud rate of data transfer in the 8051.
1. Use a higher-frequency crystal.
2. Change a bit in the PCON register, shown below.

Option 1 is not feasible in many situations since the system crystal is fixed. More
importantly, it is not feasible because the new crystal may not be compatible with the
IBM PC serial COM port’s baud rate. Therefore, we will explore option 2. There is a
software way to double the baud rate of the 8051 while the crystal frequency is fixed.
This is done with the register called PCON (power control). The PCON register is an 8-
bit register. Of the 8 bits, some are unused, and some are used for the power control
capability of the 8051. The bit that is used for the serial communication is D7, the
SMOD (serial mode) bit. When the 8051 is powered up, D7 (SMOD bit) of the PCON
register is zero. We can set it to high by software and thereby double the baud rate. The
following sequence of instructions must be used to set high D7 of PCON, since it is not a
bit-addressable register:

To see how the baud rate is doubled with this method, we show the role of the SMOD bit (D7 bit
of the PCON register), which can be 0 or 1. We discuss each case.

Baud rates for SMOD = 0


1. When SMOD = 0, the 8051 divides 1/12 of the crystal frequency by 32 and uses that
frequency for Timer 1 to set the baud rate. In the case of XTAL = 11.0592 MHz we have:
Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHz and 921.6 kHz / 32 = 28,800 Hz since
SMOD = 0.
This is the frequency used by Timer 1 to set the baud rate. This has been the basis of all the
examples so far since it is the default when the 8051 is powered up. The baud rate for
SMOD = 0 was listed in Table 10-4.

11
DAKSHAYINI M R
Baud rates for SMOD = 1
With the fixed crystal frequency, we can double the baud rate by making SMOD – 1. When
the SMOD bit (D7 of the PCON register) is set to 1, 1/12 of XTAL is divided by 16 (instead
of 32) and that is the frequency used by Timer 1 to set the baud rate. In the case of XTAL =
11.0592 MHz, we have: Machine cycle freq. = 11.0592 MHz / 12 = 921.6 kHz and
921.6 kHz / 16 = 57,600 Hz since SMOD = 1
This is the frequency used by Timer 1 to set the baud rate.

Table 10-5: Baud Rate Comparison for SMOD = 0 and SMOD = 1

Note: XTAL = 11.0592 MHz.

Example :Assuming that XTAL = 11.0592 MHz for the following program, state (a) what
this program does, (b) compute the frequency used by Timer 1 to set the baud rate, and
(c) find the baud rate of the data transfer.

1. This program transfers ASCII letter B (01000010 binary) continuously.


2. With XTAL = 11.0592 MHz and SMOD = 1 in the above program, we have:
11.0592 MHz / 12 = 921.6 kHz machine cycle frequency
921.6 kHz 716 = 57,600 Hz frequency used by Timer 1 to set the baud rate
57,600 Hz / 3 = 19,200 baud rate

12
DAKSHAYINI M R
Example:Find the TH1 value (in both decimal and hex) to set the baud rate to each of the
following. (a) 9600 (b) 4800 if SMOD = 1 Assume that XTAL – 11.0592 MHz.
Solution:
With XTAL = 11.0592 MHz and SMOD = 1, we have Tinier 1 frequency = 57,600 Hz.
1. 57,600 / 9600 = 6; therefore, TH1 = -6 or TH1 = FAH.
2. 57,600 / 4800 = 12; therefore, TH1 = -12 or TH1 = F4H.

Example
Find the baud rate if TH1 = -2, SMOD = 1, and XTAL – 11.0592 MHz. Is this baud rate
supported by IBM/compatible PCs?
Solution:With XTAL – 11.0592 MHz and SMOD = 1, we have Timer 1 frequency =
57,600-Hz. The baud rate is 57,600 / 2 = 28,800. This baud rate is not supported by the
BIOS of the PCs; however, the PC can be programmed to do data transfer at such a speed,
Also, HyperTerminal in Windows supports this and other baud rates.

Example : Assume a switch is connected to pin PL7. Write a program to monitor its
status and send two messages to serial port continuously as follows:
SW=0 send “NO”
SW=1 send “YES”
Assume XTAL = 11.0592 MHz, 9600 baud, 8-bit data, and 1 stop bit.
Solution:

13
DAKSHAYINI M R
14
DAKSHAYINI M R
Example:

15
DAKSHAYINI M R
PROGRAMMING THE SECOND SERIAL PORT
Many of the new generations of the 8051 microcontrollers come with two serial ports. The
DS89C4xO.(DS89C420/30/40/…) and DS80C320 are among them. In this section we show the
programming of the second serial port of the DS89C4xO chip.
DS89C4xO second serial port
The second serial port of the DS89C4xO uses pins PI .2 and PI .3 for the Rx and Tx lines,
respectively. See Figure 10-10. The MDE8051 Trainer (available from
www.MicroDigitalEd.com) uses the DS89C4xO chip and comes with two serial ports already
installed. It also uses the MAX232 for the RS232 connection to DB9. The connections for the
RS232 to the DS89C4xO of the MDE8051 Trainer are shown in Figure 10-11. Notice that the
first and second serial ports are designated as Serial #0 and Serial #1, respectively.
Addresses for all SCON and SBUF registers
All the programs we have seen so far in this chapter assume the use of the first serial port as the
default serial port since every version of the 8051 comes with at least one serial port. The SCON,
SBUF, and PCON registers of the 8051 are part of the special function registers. The address for
each of the SFRs is shown in Table 10-6. Notice that SCON has address 98H, SBUF has address
99H, and finally PCON is assigned the 87H address. The first serial port is supported by all
assemblers and C compilers in the market for the 8051. If you examine the list file for 8051
Assembly language programs, you will see that these labels are replaced with their SFR
addresses. The second serial port is not implemented by all versions of the 8051/52
microcontroller. Only a few versions of the 8051/52, such as the DS89C4xO, come with the
second serial port. As a result, the second serial port uses some reserved SFR addresses for the
SCON and SBUF registers and there is no universal agreement among the makers as to which
addresses should be used. In the case of the DS89C4xO, the SFR addresses of COH and C1H are
set aside for SBUF and SCON, as shown in Table 10-6. The DS89C4xO technical
documentation refers to these registers as SCON1 and SBUF1 since the first ones are designated
as SCONO and SBUFO.
Table 10-6: SFR Byte Addresses for DS89C4xO Serial Ports

16
DAKSHAYINI M R
Table 10-7: SFR Addresses for the DS89C4xO (420, 430, etc.)
Symbol Name Address
ACC* Accumulator EOH
B* B register FOR
PSW* Program status word DOH
SP Stack pointer 81H
DPTR Data pointer 2 bytes
DPL Low byte 82H
DPH High byte 83H
PO* PortO 80H
PI* Port 1 90H
P2* Port 2 OAOH
P3* Port3 BOH
IP* Interrupt priority control B8H
IE* Interrupt enable control A8H
TMOD Timer/counter mode control 89H
ICON* Timer/counter control 88H
T2CON* Timer/counter 2 control C8H
T2MOD Timer/counter mode control C9H
THO Timer/counter 0 high byte 8CH
TLO Timer/counter 0 low byte 8AH
TH1 Timer/counter 1 high byte 8DH
TL1 Timer/counter 1 low byte 8BH
TH2 Timer/counter 2 high byte CDH
TL2 Timer/counter 2 low byte CCH
RCAP2H T/C 2 capture register high byte CBH
RCAP2L T/C 2 capture register low byte CAH
SCONO* Serial control (first serial port) 98H
SBUFO Serial data buffer (first serial port) 99H
PCON Power control 87H
SCON1* Serial control (second serial port) COH
SBUF1 Serial data buffer (second serial port) C1H
* Bit-addressable

Programming the second serial port using timer 1


While each serial port has its own SCON and SBUF registers, both ports can use Timer 1 for
setting the baud rate. Indeed, upon reset, the DS89C4xO chip uses Timer 1 for setting the baud
rate of both serial ports. Since the older 8051 assemblers do not support this new second serial

17
DAKSHAYINI M R
port, we need to define them as shown in Example 10-11. Notice that in both C and Assembly,
SBUF and SCON refer to the SFR registers of the first serial port. To avoid confusion, in
DS89C4xO programs we use SCONO .and SBUFO for the first and SCON1 and SBUF1 for the
second serial ports. For this reason, the MDE8051 Trainer designates the serial ports as
Serial #0 and Serial #1 in order to comply with this designation.
See Examples 10-12 through 10-14.

Figure 10-12. SCONO and SCON1 Bit Addresses (TI and RI bits must be noted)

Example :Write a program for the second serial port of the DS89C4xO to continuously
transfer the letter “A” serially at 4800 baud. Use 8-bit data and 1 stop bit. Use Timer 1.

18
DAKSHAYINI M R
Example:Write a program to send the text string “Hello” to Serial #1. Set the baud rate at

9600, 8-bit data, and 1 stop bit.


Example:Program the second serial port of the DS89C4xO to receive bytes of data serially
and put themon PI. Set the baud rate at 4800, 8-bit data, and 1 stop bit.

19
DAKSHAYINI M R
Example:Assume that a switch is connected to pin P2.0. Write a program to monitor the
switch and perform the following:
1. If SW = 0 send the message “Hello” to the Serial #0 port.
2. If SW = 1 send the message “Goodbye” to the Serial #1 port.

20
DAKSHAYINI M R
SERIAL PORT PROGRAMMING IN C:
This section shows C programming of the serial ports for the 8051/52 and DS89C4xO chips.

Transmitting and receiving data in 8051 C


As we stated in the last chapter, the SFR registers of the 8051 are accessible directly in 8051 C
compilers by using the regSl.h file. Examples 10-15 through 10-19 show how to program the
serial port in 8051 C. Connect your 8051 Trainer to the PC’s COM port and use HyperTerminal
to test the operation of these examples.
Example:

Example:

21
DAKSHAYINI M R
Example:

Example:Write an 8051 C program to send two different strings to the serial port. Assuming
that :SW is connected to pin P2.0, monitor its status and make a decision as follows: SW = 0:
send your first name ,SW = 1: send your last name. Assume XTAL = 11.0592 MHz, baud rate
of 9600, 8-bit data, 1 stop bit.

22
DAKSHAYINI M R
Example: Write an 8051 C program to send the two messages “Normal Speed” and “High
Speed”to the serial port. Assuming that SW is connected to pin P2.0, monitor its status and set
the baud rate as follows: SW = 0 28,800 baud rate SW = 1 56K baud rate.Assume that XTAL =
11.0592 MHz for both cases.
Solution:

8051 C compilers and the second serial port


Since many C compilers do not support the second serial port of the DS89C4xO chip, we have to
declare the byte addresses of the new SFR registers using the sfr keyword. Table 10-6 and Figure
10-12 provide the SFR byte and bit addresses for the DS89C4xO chip. Examples 10-20 and 10-
21 show C versions of Examples 10-11 and 10-13 in Section 10.4.

23
DAKSHAYINI M R
Notice in both Examples 10-20 and 10-21 that we are using Tinier 1 to set the baud rate for the
second serial port. Upon reset, Timer 1 is the default for the second serial port of the DS89C4xO
chip.
Example: Write a C program for the DS89C4xO to transfer letter “A” serially at 4800 baud
continuously. Use the second serial port with 8-bit data and 1 stop bit. We can only use
Timer 1 to set the baud rate. Solution:

Example :Program the DS89C4xO in C to receive bytes of data serially via the second serial
port and put them in PI. Set the baud rate at 9600, 8-bit data, and 1 stop bit. Use Timer 1 for
baud rate generation.

24
DAKSHAYINI M R
SUMMARY
This chapter began with an introduction to the fundamentals of serial communication. Serial
communication, in which data is sent one bit a time, is used when data is sent over significant
distances since in parallel communication, where data is sent a byte or more a time, great
distances can cause distortion of the data. Serial communication has the additional advantage of
allowing transmission
over phone lines. Serial communication uses two methods: synchronous and asynchronous. In
synchronous communication, data is sent in blocks of bytes; in asynchronous, data is sent in
bytes. Data communication can be simplex (can send but cannot receive), half duplex (can send
and receive, but not at the same time), or full duplex (can send and receive at the same time).
RS232 is a standard for serial communication connectors.
The 8051 ‘s UART was discussed. We showed how to interface the 8051 with an RS232
connector and change the baud rate of the 8051. In addition, we described the serial
communication features of the 8051, and programmed the 8051 for serial data communication.
We also showed how to program the second serial port of the DS89C4xO chip in Assembly and
C.

25
DAKSHAYINI M R
26
DAKSHAYINI M R

You might also like