
; serial communication
;set serial receive for 19200/8/n/1

#include 8051.H

         .org 0000H
	ajmp	Main
; LCD Routine

DATA	.equ	080H		;P0
DB0	.equ	080H		;P0.0
DB1	.equ	081H		;P0.1
DB2	.equ	082H		;P0.2
DB3	.equ	083H		;P0.3
DB4	.equ	084H		;P0.4
DB5	.equ	085H		;P0.5
DB6	.equ	086H		;P0.6
DB7	.equ	087H		;P0.7
RS	.equ	0A2H		;P2.2
RW	.equ	0A1H		;P2.1
EN	.equ	0A0H		;P2.0

INIT_LCD
	mov	R0,	#00H
	SETB	EN
	CLR	RS
	MOV	DATA,#38h
	CLR	EN
	LCALL	WAIT_LCD
	SETB	EN
	CLR	RS
	MOV	DATA,#0Eh
	CLR	EN
	LCALL	WAIT_LCD
	SETB	EN
	CLR	RS
	MOV	DATA,#06h
	CLR	EN
	LCALL	WAIT_LCD

WRITE_LCD
	SETB	EN
	SETB	RS
	MOV	DATA,A
	CLR	EN
	LCALL	WAIT_LCD
	RET

CLEAR_LCD
	SETB	EN
	CLR	RS
	MOV	DATA,#01h
	CLR	EN
	LCALL	WAIT_LCD
	RET

WAIT_LCD:
	SETB	EN		;Start LCD command
	CLR	RS		;It's a command
	SETB	RW		;It's a read command
	MOV	DATA,#0FFh	;Set all pins to FF initially
	MOV	A,DATA		;Read the return value
	JB	ACC.7,WAIT_LCD	;If bit 7 high, LCD still busy
	CLR	EN		;Finish the command
	CLR	RW		;Turn off RW for future commands
	RET


	.org 0100H

Main	acall	Init		; initialise
run	acall	REC		; call RECeive
	acall	SND
	sjmp	run
	.end


Init	acall	INIT_LCD
	mov	TMOD,	#20h 	; Timer 1 in Auto-reload mode 
	mov 	IE,	#00h 	; reset intterupts
	mov 	PCON,	#80h	; set SMOD for 19200 baud
	mov	SCON,	#50h	; serial mode 1, REN=1
	mov	TH1,	#253	; load T1 for 19200 baud
	setb	TR1		; start timer
	ret			; return


REC	jnb	RI,	$	; Wait for the 8051 to set the RI flag
	mov	A,	SBUF	; Read the character from the serial port
	clr	RI		; set for next character
	ret			; return

SEND	clr	TI		; Be sure the bit is initially clear
	mov	SBUF,	A	; send character
	jnb	TI,	$	; Pause until the TI bit is set. 
	ret			; return


SND	acall	SEND
	acall	WRITE_LCD
	ret


