Serial Communication
Written by Amol Shah, on Aug-2008
Views 1180    

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.

receive:

                jnb RI,$

                clr RI

                mov a,sbuf

                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 this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Quote this article in website Favoured Print Send to friend Related articles Save this to del.icio.us

Users' Comments (0) RSS feed comment

No comment posted

Add your comment



mXcomment 1.0.5 © 2007-2009 - visualclinic.fr
License Creative Commons - Some rights reserved
 
< Prev   Next >
Home arrow Code Library arrow Serial Communication