;***************************************************************************
;* File Name		:uart.asm
;* Title		:UART based RS232 communication
;* Date	 		:09/28/97
;* Version 		:1.0
;* Target MCU		:AT90S8515
;* 
;* DESCRIPTION
;* Test the UART based RS232 communication
;***************************************************************************

;***** Directives
.device	 at90s8515
.nolist
.include "8515def.inc"
.list

.def	temp = r16


;***** Interrupt vector table

	rjmp	RESET		; Reset handle
	reti			; External Interrupt0 handle
	reti			; External Interrupt1 handle
	reti			; T/C1 Capture Event Interrupt handle
	reti			; T/C1 CompareA Interrupt handle
	reti			; T/C1 CompareB Interrupt handle
	reti			; T/C1 Overflow Interrupt handle
	reti			; T/C0 Overflow Interrupt handle
	reti			; SPI Transfer Complete Interrupt handle
	reti			; UART Rx Complete Interrupt handle
	reti			; UART Data Register Empty Interrupt handle
	reti			; UART Tx Complete Interrupt handle
	reti			; Analog Comparator Interrupt handle



;***** Subroutines

uart_init:
	ldi	temp, 25	; Baudrate  9600 @ 4MHz Clock
;	ldi	temp, 12	; Baudrate 19200 @ 4MHz Clock
	out	UBRR, temp	
	ret

getc:	sbi	UCR, RXEN	; Rx Enable	
	sbis	USR, RXC	; Wait for character
	rjmp 	getc
	in	temp, UDR	; Read character
	cbi	UCR, RXEN	; Rx Disable
	ret

putc:	sbi	UCR, TXEN	; Tx Enable
	sbis	USR, UDRE 	; Wait until Data Register is empty
	rjmp 	putc
	out	UDR, temp	; Write character
	cbi	UCR, TXEN	; Tx Disable
	ret

;***** Main


RESET:	ldi	temp, LOW(RAMEND)
	out	SPL, temp		; Initialize SPL
	ldi	temp, HIGH(RAMEND)
	out	SPH, temp		; Initialize SPH

	rcall 	uart_init	; Initialize UART
	
		
loop:	rcall	getc		; Read character from RS232
	inc	temp
	rcall	putc		; Echo back incremented character

	rjmp	loop		; Repeat endless


