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)

 

                                                                   

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

mov TMOD, #20H ;T1 is mode2
mov TH1, #0fd ;9600 baud
mov SCON, #50H ;8b, 1stop, 1start, REN enabled
anl PCON, #07fh ;To make SMOD =0 

setb TR1 ;start T1
 

 AGAIN

 mov SBUF, #’A’ ;letter A is transmitted
 

 HERE

 jnb TI, HERE ;poll TI until all the bits are transmitted
 clr TI ;clear TI for the next character
 sjmp AGAIN ;while(1)

    #include <Intel\8052.h>
#include <standard.h>
#include<stdio.h>
 

void main ()
{
TMOD = 0x20;
TH1 = 0xfd;
PCON &= 0x7f;
SCON = 0x50;
TCON =0x40;


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:

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

 

AGAIN:

clr RI ;ready to receive a byte

 

HERE:

jnb RI, HERE ;wait until one byte is Rx-ed

mov A, SBUF ;read the received byte from SBUF

mov P1, A ;display on P1

sjmp AGAIN ;while (1)

 

 

   

#include <Intel\8052.h>
#include <standard.h>
#include<stdio.h>

unsigned char a;
void main ()
{

TMOD = 0x20;
TH1 = 0xfd;
PCON &= 0x7f;
SCON = 0x50;
TCON =0x40;

while (1) /*continues loop */
{

a= getchar () ;
P1=a;
}

}

 

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
ljmp START

DATA: .db "HI,I AM MAHESH",0dh,0ah,0 ;0 at end to detect end of string(0d carrage return ,0a -line feed)

;********************TRANSMIT******************
TRANSMIT:
clr A ; clear A to get data
movc A,@A+DPTR ; get data from string at data pointer
jz EXITSTR ; if data zero, eos
lcall OUTCHAR ; else send character
inc dptr ; increment data pointer
sjmp TRANSMIT ; continue, zero condition will terminate

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
 
    #include <Intel\8052.h>
#include <standard.h>
#include<stdio.h>

void main ()
{
TMOD = 0x20;
TH1 = 0xfd;
PCON &= 0x7f;
SCON = 0x50;
TCON =0x40;

while (1) /*continues loop */
{
printf("HI I AM MAHESH\n"); /* transmit a along with CR & LF*/
}
}

 

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.