Anda di halaman 1dari 6

Serial Communication

Code Library - 8051 Assembly Share inShare Download SocButtons Here are a few simple subroutines for serial communications. For more information on serial communication ple baudrate: This subroutine initializes the controllers com port, initializes Timer 1 in Mode 2 i.e. for auto reload m 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 b

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 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 i

received the Carry flag is cleared and return. If a byte is received then the carry flag is set and the received 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 subroutine sends the newline character and the carriage return character. So basically the next byte that we send 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 TE to the PC and then waits for a byte to be received from the PC. The controller sends back whatever byte that ha All the bytes are sent on new line. Org 0000h call baudrate baudrate //Initialize the COM port and set the

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 mov TMOD,#20h mov TH1,#0fdh setb TR1 ret //Initialize serial communication //Timer 1 Mode 2 //For 9600 baudrate //Start Timer 1

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.

Anda mungkin juga menyukai