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

Serial Port Programming:: RS 232 Standard

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Serial Port Programming:

RS 232 Standard

1) RS 232 is the most widely used serial I/O interfacing standard.

2) Its input and output voltage levels are not TTL compatible.

3) In RS 232 a 1 is represented by -3v to -25v, while a 0 bit is +3v to +25v.For this reason, we must use voltage converters

such as MAX 232 to convert the TTL logic levels to the RS 232 voltage levels and vice versa.

4) RS 232 is available in connector form such as DB-9 and DB-25 male and female connector.

5) DB-9 means it have 9 pins & DB-25 means it have 25 pins respectively.
RxD and TxD pins in the 8051
The 8051 has two pins that are used specifically for transferring and receiving data serially. These two pins are called TxD
and RxD and are part of the port 3 (P3.0 and P3.1). These pins are TTL compatible; therefore, they require a line driver to make
them RS232 compatible.
.
MAX232

We need a line driver (voltage converter) to convert the RS232′s signals to TTL voltage levels that will be acceptable to
the 8051‘s TxD and RxD pins. One example of such a converter is MAX232.The MAX232 converts from RS232 voltage levels to
TTL voltage levels, and vice versa. One advantage of the MAX232 chip is that it uses a +5 V power source which is the same as the
source voltage for the 8051.
The MAX232 has two sets of line drivers for transferring and receiving data. The line
drivers used for TxD are called Tl and T2 and RxD for R1 and R2.,

Figure :(a) Inside MAX232 and (b) its Connection to the 8051 (Null Modem)

1) The T1 line driver has a designation of “T1In” and “T1out” on pin numbers 11 and 14.

2) The “T1In” pin is the TTL side and” T1out” at RS 232 side.

3) The R1 lined driver has a designation of “R1In” and “R1out” on pin numbers 13 and 12.

4) The “R1In” is the RS 232 side and “R1out” is the TTL side.

5) MAX232 requires four capacitors ranging from 1 to 22 nF.

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

1) The no. of bit’s transmitted per second called baud rate


2) The rate of data transfer in serial communication is measure in bps (bits per second)
3) We use data transfer rate from 100 to 9600.

How to set baud rate for Timer:

1) 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).

3) The 8051‘s serial communication UART circuitry divides the machine cycle frequency of 921.6 kHz by 32 we get
baud rate. Therefore, 921.6 kHz divided by 32 gives 28,800 Hz.
Crystal frequency (XTAL) =11.0592 MHz for 8051

The machine cycle frequency of the 8051=11.0592 MHz/12

= 921.6 KHz, and 921.6 KHz/32

Baud rate =28,800 Hz

This is the no. we will use to find Timer 1 value to set the baud rate. When Timer 1 is used to set baud rate it must
be programmed in mode 2.i.e.8-bit auto-reload.

To get baud rate value compatible with PC we must load TH1 with tae values given below.

Note: XTAL = 11.0592 MHz.

Example:

With XTAL =11.0592 MHz, find the TH1 value needed to have the following baud rates.
a)9600 b)4800 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.
SBUF Register:

SBUF is an 8-bit register used for serial communication in 8051.For a bit data to be transferred via TxD line,it must be

placed in the SBUF register. Similarly SBUF hold’s byte of data when it is received by the 8051 via RxD line.

e.g: MOV SBUF, # ‘D’

MOV A, SBUF

MOV SBUF, A

SCON register (Serial Control):

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.

SM0, SM1

SM0 and SM1 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 bit.

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 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. For our applications, we will make SM2=0

REN
The REN (receive enable), bit is D4 of the SCON register .REN 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.

TB8

TB 8 (transfer bit 8) is bit D3 of SCON. We make TB8=0.

RB8

TB 8 (transfer bit 8) is bit D2 of SCON. We make RB8=0.

TI (transmit interrupt)

TI (transmit interrupt) is bit D1 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.

RI (receive interrupt)

RI (receive interrupt) is bit D0 of the SCONregister. This is an flag bit in the SCON register. When the 8051 receives data serially via

RxD, if it gets raised through the stop and start bits in the SBUF register. Then it raises RI flag bit to shows that has been received.

Programming steps to transfer data serially

1) The TMOD register is loaded with the value 20H indicating the use of Timer 1 in mode 2 (8-bit auto-reload) to see the baud

rate.

2) The TH1 is loaded with one of the values from baud rate table for serial data transfer.

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) The character byte to be transferred serially is written into the SBUF register.

6) The TI flag bit is monitored with the use of the instruction” JNBTI,xx” to see if the character has been transferred

completely.

7) TI is cleared by the” CLR TI “instruction.


Example:

Example:

Write program to transfer the message “YES “serially at 9600 baud rate,8 bit data 1 stop bit. Do this continusoly.
Example:
Program the 8051 to receive bytes of data serially and put them in P1 Set the baud rate at 4800 baud rate, 8 bit data 1

stop bit

You might also like