;EXAM8.ASM
;Demo Send String to RS232 and Send Char 'A'
;Recive from RS232 and Echo
;   Check key press ='h' and say Hello again

	ORG	0000H


MAIN:	LCALL	InitUART	;Initial UART 9600,n,8,1 no parity

;Demo Send char 'A' 
	MOV	A,#'A'		;Send Char 'A'
	LCALL	SBYTE	
	MOV	A,#0DH		;Send carriage Return(Cursor go to  Home)
	LCALL	SBYTE	
	MOV	A,#0AH		;Send Line feed (Next Line)
	LCALL	SBYTE	

;Demo Send String
	LCALL	SayHello	;Send String in Table 

;*** Get Message Loop ***
GetMsg:	JB	RI,GetByte	;Have Message or not
;do the other program
;...

	SJMP	GetMsg

GetByte:LCALL	RBYTE		;Read -> A
;do something

	LCALL	SBYTE		;A    -> ECHO
	XRL	A,#'h'		;Test Key 'h'
	JZ	DataIn
	SJMP	GetMsg

DataIn:	LCALL	SayHello	;Out put for test
	SJMP	GetMsg

;*** END Main ***

InitUART:
	MOV	SCON,#01010010B	;Set SCON Serial Control Mode 1: 8-bit UART (Timer-Based). 
	MOV	TMOD,#00100000B	;Set Mode 2  8-bit Auto-Reload
	MOV	TH1,#0FDH	;Set Baud Rate 9600
	SETB	TR1		;Enable Timer 1
	RET

;
SendStr:
	CLR	A
	MOVC	A,@A+DPTR	;load char
	SUBB	A,#00		;test end of string
	JZ	ENDSTR		;yes end
	LCALL	SBYTE		;send 1	byte
	INC	DPTR		;point to next char
	SJMP	SENDSTR		;load next char
EndStr:	RET


; ********** SBYTE SUB **********
;SEND BYTE
;IN  =	A
;REG =	NO
SBYTE:	JNB	TI,SBYTE	;Wait For Sending done
	CLR	TI
	MOV	SBUF,A
	RET

; ********** RBYTE SUB **********
;OUT =	A
;REG =	A
RBYTE:	JNB	RI,RBYTE	;Wait For Receive already
	CLR	RI
	MOV	A,SBUF
	RET


SayHello:
	MOV	DPTR,#HELLO
	LCALL	SENDSTR
	RET

hello:	DB	0Dh,"*** MCS-51 READY ***",0Dh,0AH,00

END
