
;serial routines using interrupts
$mod51

org 0
	jmp	START	;
org 23h
	jmp	Ser_Int	;
org 30h
;------------------------------------------------------------
; set up uart in mode 1 (8 bits uart) with
; timer 1 in mode 2 (8 bits auto reload timer)

START:
	mov SP,#6Fh	; set up stack
	mov SCON, #50h	; uart in mode 1 (8 bit), REN=1
	orl TMOD, #20h	; Timer 1 in mode 2
	mov TH1, #0FDh	; 9600 Baud at 11.059MHz
	setb ES		; Enable serial interrupt
	setb EA		; Enable global interrupt
	setb TR1	; Timer 1 run
	
	call Test
	jmp $		; endless
;------------------------------------------------------------
;serial interrupt
Ser_Int:
	push ACC	; save accumulator
	jnb RI,Trans	; test if it is a reception
	clr RI		; clear reception flag for next reception
	mov A,SBUF	; read data from uart
	jmp ExitISR	; return
Trans: 
	CLR TI		; clear transmission flag for next trans
ExitISR:
	pop ACC		; restore accumulator
	reti
;------------------------------------------------------------
Test:
	mov	dptr,#Test_String
	call	OutStr
	ret
;********************************************************************
; Send a null terminated string out serial port
;********************************************************************
OutStr:
	clr a
      movc a,@a+dptr	; get character
      jz exit		; stop if char = null
;     jnb TI,$	        ; wait for last bit to transfer
      mov SBUF,A  	;
;
;***If a delay is inserted here, it works!!
;	call	Delay10mS

      inc dptr		; point to next char
      sjmp OutStr
exit:
	ret

Test_String:
DB	'This is a Test',0dh,0ah,0
;-----------------------------------------------------------------
end
