;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
        clr a
        movc a,@a+dptr	; get character
        jz exitISR	; stop if char = null
        mov SBUF,A  	;

        inc dptr	; point to next char

ExitISR:
	pop ACC		; restore accumulator
	reti
;------------------------------------------------------------
Test:
	mov	dptr,#Test_String
	clr     ti
	clr a
        movc a,@a+dptr	; get character
        mov SBUF,A  	;

	;do other stuff, my ideal time waster is:

idle	equ	1
loop:	orl     pcon,#idle
	jmp	loop

Test_String:
DB	'This is a Test',0dh,0ah,0
;-----------------------------------------------------------------
end
