You are on page 1of 3

8051 interfacing tutorials with RS232 logic devices with circuit c and assembly code & example Pgina 1 de 3

Home 8051 Interfacings 8051 Downloads About Me Contact Me

8051 INTERFACING
Online FREE !

Serial Communication
RS-232 WAVEFORM

TTL/CMOS Serial Logic Waveform

The diagram above, shows the expected waveform from the UART when using the common 8N1 format. 8N1 signifies 8 Data bits, No Parity and 1 Stop
Bit. The RS-232 line, when idle is in the Mark State (Logic 1). A transmission starts with a start bit which is (Logic 0). Then each bit is sent down the
line, one at a time. The LSB (Least Significant Bit) is sent first. A Stop Bit (Logic 1) is then appended to the signal to make up the transmission.

The data sent using this method, is said to be framed. That is the data is framed between a Start and Stop Bit .

RS-232 Voltage levels


1. +3 to +25 volts to signify a "Space" (Logic 0)
2. -3 to -25 volts for a "Mark" (logic 1).
3. Any voltage in between these regions (i.e. between +3 and -3 Volts) is undefined.
The data byte is always transmitted least-significant-bit first.
The bits are transmitted at specific time intervals determined by the baud rate of the serial signal.

This is the signal present on the RS-232 Port of your computer, shown below.

RS-232 Logic Waveform

RS-232 LEVEL CONVERTER

Standard serial interfacing of microcontroller (TTL) with PC or any RS232C Standard device , requires TTL to RS232 Level converter . A MAX232 is used
for this purpose. It provides 2-channel RS232C port and requires external 10uF capacitors.

The driver requires a single supply of +5V .

MAX-232 includes a Charge Pump, which generates +10V and -10V from a single 5v supply.

MICROCONTROLLER INTERFACING WITH RS-232 STANDARD DEVICES

 MAX232 (+5V -> +-12V converter)


 Serial port male 9 pin connector (SER)

http://www.freewebs.com/maheshwankhede/rs232.html 10/02/2010
8051 interfacing tutorials with RS232 logic devices with circuit c and assembly code & example Pgina 2 de 3

SETTING SERIAL PORT.

SCON
8 bit UART ,RN enabled , TI & RI operated by program. - 50hex

Timer 1 Count
TH1 = 256 - ((Crystal / 384) / Baud) -PCON.7 is clear.
TH1 = 256 - ((Crystal / 192) / Baud)-PCON.7 is set.

so with PCON.7 is clear we get timer value = FDhex

CODE EXAMPLE

1. TRANSMITTING 'A' CONTINUOUSLY ON SERIAL PORT.

ASSEMBLY LANGUAGE C LANGUAGE (SPJ SIDE51)

START #include <Intel\8052.h>


mov TMOD, #20H ;T1 is mode2 #include <standard.h>
mov TH1, #0fd ;9600 baud #include<stdio.h>
mov SCON, #50H ;8b, 1stop, 1start, REN enabled
anl PCON, #07fh ;To make SMOD =0
setb TR1 ;start T1 void main ()
{
AGAIN TMOD = 0x20;
mov SBUF, #A ;letter A is transmitted TH1 = 0xfd;
PCON &= 0x7f;
HERE SCON = 0x50;
jnb TI, HERE ;poll TI until all the bits are transmitted TCON =0x40;
clr TI ;clear TI for the next character
sjmp AGAIN ;while(1) while (1) /*continues loop */
{
printf("a"); /* transmit a along with CR & LF.
}

2.TO RECEIVE DATA FROM SERIAL PORT AND SENT IT TO PORT 1.

ASSEMBLY LANGUAGE C LANGUAGE (SPJ SIDE51)

START: #include <Intel\8052.h>


mov TMOD, #20H ;T1 in mode 2 #include <standard.h>
mov TH1, #-3 ;9600 baud #include<stdio.h>
mov SCON, #50H ;8b, 1start, 1stop
anl PCON, #07fh ;To make SMOD =0 unsigned char a;
setb TR1 ;start T1 void main ()
{
AGAIN: TMOD = 0x20;
clr RI ;ready to receive a byte TH1 = 0xfd;
PCON &= 0x7f;
HERE: SCON = 0x50;
jnb RI, HERE ;wait until one byte is Rx-ed TCON =0x40;
mov A, SBUF ;read the received byte from SBUF while (1) /*continues loop */
mov P1, A ;display on P1 {
sjmp AGAIN ;while (1) a= getchar () ;
P1=a;
}
}

http://www.freewebs.com/maheshwankhede/rs232.html 10/02/2010
8051 interfacing tutorials with RS232 logic devices with circuit c and assembly code & example Pgina 3 de 3

3. SENDING DATA IN STRING TO SERIAL PORT

In Assembly Lan. prog. : Data is stored in string at pointer DATA. 0 is appended at end of string. In transmit subroutine data in string is transmitted till
0 is detected.

ASSEMBLY LANGUAGE (A51)


C LANGUAGE (SPJ SIDE51)

.org 0000h #include <Intel\8052.h>


ljmp START #include <standard.h>
#include<stdio.h>
DATA: .db "HI,I AM MAHESH",0dh,0ah,0 ;0 at end to detect end
of string(0d carrage return ,0a -line feed) void main ()
{
;********************TRANSMIT****************** TMOD = 0x20;
TRANSMIT: TH1 = 0xfd;
clr A ; clear A to get data PCON &= 0x7f;
movc A,@A+DPTR ; get data from string at data pointer SCON = 0x50;
jz EXITSTR ; if data zero, eos TCON =0x40;
lcall OUTCHAR ; else send character
inc dptr ; increment data pointer while (1) /*continues loop */
sjmp TRANSMIT ; continue, zero condition will terminate {
printf("HI I AM MAHESH\n"); /* transmit a along with CR & LF*/
EXITSTR: }
ret }

OUTCHAR:
mov sbuf,a ; place A into Serial Port 1 Buffer

WAITCHAR:
jnb ti,WAITCHAR ; wait buffer empty flag is set
clr ti ; clear buffer empty flag
ret

START:
;******************INITIALISATION*****************
mov TMOD, #20H ;T1 in mode 2
mov TH1, #-3 ;9600 baud
mov SCON, #50H ;8b, 1start, 1stop
anl PCON, #07fh ;To make SMOD =0
setb TR1 ;start T1

;*****************To SEND DATA*******************


mov dptr,#DATA
lcall TRANSMIT
sjmp START

EXAMPLE - MOBILE PHONE AND GPS RECEIVER.

You can use same circuit for communicating with Mobile phones/GSM Module or GPS. Communicating with both of these require a Multiplexer ,which can
be implemented using NAND gates.

GPS serial output


Most GPS are capable of sending information through a simple serial link. Only the TXD and GROUND pins need to be connected . The GPS must be set
at 9600 bps (or 4800) , 8 bits, No Parity, and 1 stop bit.

NAND gate as 2:1 Mux. which connects Rx of GSM modem or GPS receiver according to select bit logic level (pin P1.0 of uC)

Site Developed and maintained by Mahesh Wankhede.

http://www.freewebs.com/maheshwankhede/rs232.html 10/02/2010