Please update your Flash Player to view content.

                  FOLLOW  US





 
 
Serial Communication
Code Library - 8051 Assembly

Here are a few simple subroutines for serial communications. For more information on serial communication please check this article.

baudrate: This subroutine initializes the controllers com port, initializes Timer 1 in Mode 2 i.e. for auto reload mode which is used to set the baudrate.

We have used 9600 baudrate.

baudrate:

mov scon,#50h 

//Tnitialize serial communication

mov TMOD,#20h //Timer 1 Mode 2
mov TH1,#0fdh //For 9600 baudrate
setb TR1 //Start Timer 1
ret

send: This subroutine sends serially whatever data is in the accumulator i.e. loads it into SBUF and waits for the byte to be sent i.e.

we wait till the TI (Transmit Interrupt) flag is set.

send:

mov sbuf,a

jnb TI,$

clr TI

ret

receive: This subroutine waits till a byte is received i.e. till RI (receive Interrupt) flag is set. Then the received byte is copied into the accumulator.

send:

mov sbuf,a

jnb TI,$

clr TI

ret

check_receive: Unlike the receive subroutine which waits till a byte is received this subroutine checks if a byte is received, if no byte is received the Carry flag is cleared and return. If a byte is received then the carry flag is set and the received byte is copied into the accumulator and return.

check_receive:

jb RI,c1_check_receive

clr c

ret

c1_check_receive:

clr RI

mov a,sbuf

setb c

ret

send_newline: I find this subroutine very useful especially when i have to use HyperTerminal for checking the received data. This subroutine sends the newline character and the carriage return character. So basically the next byte that we send is displayed at the start of next line.

send_newline:

mov a,#10

call send

mov a,#13

call send

ret

Here is a complete program for serial communication. When Power is turned ON the Controller sends ‘DNA TECHNOLOGY’ serially to the PC and then waits for a byte to be received from the PC. The controller sends back whatever byte that has been received serially. All the bytes are sent on new line.

Org 0000h

                call baudrate                 //Initialize the COM port and set the baudrate

                mov a,#'D'

                call send

                mov a,#'N'

                call send

                mov a,#'A'

                call send

                mov a,#20h

                call send

                mov a,#'T'

                call send

                mov a,#'E'

                call send

                mov a,#'C'

                call send

                mov a,#'H'

                call send

                mov a,#'N'

                call send

                mov a,#'O'

                call send

                mov a,#'L'

                call send

                mov a,#'O'

                call send

                mov a,#'G'

                call send

                mov a,#'Y'

                call send

                call send_newline 

 

loop:

                call receive

                call send

                call send_newline

                ajmp loop

      

baudrate:

                mov scon,#50h                  //Initialize serial communication

                mov TMOD,#20h               //Timer 1 Mode 2

                mov TH1,#0fdh                 //For 9600 baudrate

                setb TR1                            //Start Timer 1

                ret

  

send:

                mov sbuf,a

                jnb TI,$

                clr TI

                ret

 

receive:

                jnb RI,$

                clr RI

                mov a,sbuf

                ret

  

check_receive:

                jb RI,c1_check_receive

                clr c

                ret

 

c1_check_receive:

                clr RI

                mov a,sbuf

                setb c

                ret

 

send_newline:

                mov a,#10

                call send

                mov a,#13

                call send

                ret

End

  You can use Hyper Terminal for sending and receiving bytes serially. Click here to see how to setup Hyper Terminal for serial communication.
 

Add comment


Security code
Refresh

 

Featured Products

Rs1 800.00

Rs600.00
Rs540.00
You Save: 10.00%

 

Random Products

Rs180.00











Latest Video

Random Articles

  • The Junction Diode
    The Junction Diode The effect described in the previous tutorial is achieved...
  • Summary
    Capacitor Tutorial Summary 1.     A capacitor consists of two...
  • 8-bit HEX to ASCII Convertor
    This subroutine converts 8 bit Hexadecimal number into its equivalent ASCII value. The number...
  • Industrial projects
    RPM Meter. RPM Switch. Length Counter. Totalizer. Data Acquisition System. Sequential...

Search Products

User Login






 
 
.
 TUTORIALS
.
  8051 Tutorial
 
  Power Supply
 
CODE LIBRARY
.

  8051 Assembly
 
  C Code Library

.
 PROJECTS
.
  Project List
 
  Synopsis and Abstract
 
DOWNLOADS
.

  Datasheets
 
  Download

.
 POPULAR
.
  About Us
 
  DNA Shop
 
  Totorial
 
  Code Library
 
  Download
 

.
 CONTACT US
.

Mr. Amol Shah

DNA Technology Nashik,

Maharashtra 422009 India

Phone : 0253-3023939

Mobile : 09225145135

Email : contact@dnatechindia.com