
; LED per UART ein- und ausschalten
; verwendet Beispiel aus http://www.mikrocontroller.net/tutorial/uart.htm
;
; Achim Walther, www.voidpointer.de, 20.11.2004

.include "m8def.inc"

.def temp = R16
.equ CLOCK = 8000000
.equ BAUD = 9600
.equ UBRRVAL = CLOCK/(BAUD*16)-1

.org 0x00
	rjmp main

.org URXCaddr
	rjmp int_rxc

; Hauptprogramm
main:
	ldi temp, LOW(RAMEND)
	out SPL, temp
	ldi temp, HIGH(RAMEND)
	out SPH, temp

	 ldi temp, 0xFF
	 out DDRC, temp		;Port C durch Ausgabe von 0xFF ins
				;Richtungsregister DDRC als Ausgang konfigurieren
	 ldi temp, 0x00
	 out PORTC, temp		;PORTC auf 0xFF setzen -> alle LEDs aus

	; Baudrate einstellen
	ldi temp, LOW(UBRRVAL)
	out UBRRL, temp
	ldi temp, HIGH(UBRRVAL)
	out UBRRH, temp

	; Frame-Format: 8 Bit
	ldi temp, (1<<URSEL)|(3<<UCSZ0)
	out UCSRC, temp

	sbi UCSRB, RXCIE	; Interrupt bei Empfang
	sbi UCSRB, RXEN		; RX (Empfang) aktivieren

	sei			; Interrupts global aktivieren

loop:	rjmp loop		; Endlosschleife

; Interruptroutine: wird ausgeführt sobald ein Byte über das UART empfangen wurde
int_rxc:
	push temp		; temp auf dem Stack sichern

	in temp, UDR
	cpi temp, '1'		; empfangenes Byte mit '1' vergleichen
	brne int_rxc_1		; wenn nicht gleich, dann zu int_rcx_1
	cbi PORTC, 0		; LED einschalten
int_rxc_1:
	cpi temp, '0'		; empfangenes Byte mit '0' vergleichen
	brne int_rxc_2		; wenn nicht gleich, dann zu int_rcx_2
	sbi PORTC, 0		; LED ausschalten
int_rxc_2:
	pop temp		; temp wiederherstellen
	reti