;==============================================================================
#message __DATE__ ,__TIME__
#message __FILE__

#include <m8Adef.inc>

#define F_CPU 1000000
#define USART_BAUDRATE 9600
// BAUD_PRESCALE pro U2X=0
#define BAUD_PRESCALE ((( F_CPU / ( USART_BAUDRATE * 16.0))) - 1)
// BAUD_PRESCALE pro U2X=1
#define BAUD_PRESCALE2 ((( F_CPU / ( USART_BAUDRATE * 8.0))) - 1)


;------------------------------------------------------------------------------
; Definice registrových proměnných

.def Temp=R16				; Pomocná proměnná

;------------------------------------------------------------------------------
; Tabulka skoků

.org 0
        rjmp RESET 			; Reset Handler
        rjmp EXT_INT0 		; IRQ0 Handler
        rjmp EXT_INT1 		; IRQ1 Handler
        rjmp TIM2_COMP 		; Timer2 Compare Handler
        rjmp TIM2_OVF 		; Timer2 Overflow Handler
        rjmp TIM1_CAPT 		; Timer1 Capture Handler
        rjmp TIM1_COMPA 	; Timer1 CompareA Handler
        rjmp TIM1_COMPB 	; Timer1 CompareB Handler
        rjmp TIM1_OVF 		; Timer1 Overflow Handler
        rjmp TIM0_OVF 		; Timer0 Overflow Handler
        rjmp SPI_STC 		; SPI Transfer Complete Handler
        rjmp USART_RXC 		; USART RX Complete Handler
        rjmp USART_UDRE 	; UDR Empty Handler
        rjmp USART_TXC 		; USART TX Complete Handler
        rjmp ADC_INT		; ADC Conversion Complete Handler
        rjmp EE_RDY 		; EEPROM Ready Handler
        rjmp ANA_COMP 		; Analog Comparator Handler
        rjmp TWSI 			; Two-wire Serial Interface Handler
        rjmp SPM_RDY 		; Store Program Memory Ready Handler

;------------------------------------------------------------------------------
; Rutiny obsluhy přerušení

EXT_INT0: 					; IRQ0 Handler
        reti
EXT_INT1: 					; IRQ1 Handler
        reti
TIM2_COMP: 					; Timer2 Compare Handler
        reti
TIM2_OVF: 					; Timer2 Overflow Handler
        reti
TIM1_CAPT: 					; Timer1 Capture Handler
        reti
TIM1_COMPA: 				; Timer1 CompareA Handler
        reti
TIM1_COMPB: 				; Timer1 CompareB Handler
        reti
TIM1_OVF: 					; Timer1 Overflow Handler
        reti
TIM0_OVF: 					; Timer0 Overflow Handler
        reti
SPI_STC: 					; SPI Transfer Complete Handler
        reti
USART_RXC: 					; USART RX Complete Handler
        in Temp, UDR
        out UDR, Temp
        reti
USART_UDRE: 				; UDR Empty Handler
        reti
USART_TXC: 					; USART TX Complete Handler
        reti
ADC_INT: 					; ADC Conversion Complete Handler
        reti
EE_RDY: 					; EEPROM Ready Handler
        reti
ANA_COMP: 					; Analog Comparator Handler
        reti
TWSI: 						; Two-wire Serial Interface Handler
        reti
SPM_RDY: 					; Store Program Memory Ready Handler
        reti
;

;------------------------------------------------------------------------------
; Hlavní program - inicializace

RESET:  ldi temp,high(RAMEND); Main program start
        out SPH,temp ; Set Stack Pointer to top of RAM
        ldi temp,low(RAMEND)
        out SPL,temp

        ldi XH, high(SRAM_SIZE)
        ldi XL, low(SRAM_SIZE)
        ldi YH, high(SRAM_START)
        ldi YL, low(SRAM_START)
        clr temp
Init_RAM:
        st Y+, temp
        sbiw XL, 1
        brne Init_RAM

		clr r0
	    clr r1
		movw r2:r3, r0:r1
		movw r4:r5, r0:r1
		movw r6:r7, r0:r1
		movw r8:r9, r0:r1
		movw r10:r11, r0:r1
		movw r12:r13, r0:r1
		movw r14:r15, r0:r1
		movw r16:r17, r0:r1
		movw r18:r19, r0:r1
		movw r20:r21, r0:r1
		movw r22:r23, r0:r1
		movw r24:r25, r0:r1
		movw r26:r27, r0:r1
		movw r28:r29, r0:r1
		movw r30:r31, r0:r1


;------------------------------------------------------------------------------
; Zapnout Pull-Up odpory na všech pinech

        ldi Temp, 0xFF
        out PORTB, Temp
        out PORTC, Temp
        out PORTD, Temp

;------------------------------------------------------------------------------
; Nastavit TXD pin jako výstupní (ostatní jsou vstupní)

        ldi Temp, 0
        out DDRB, Temp
        out DDRC, Temp

        ldi Temp, (1<<PD1)
        out DDRD, Temp

;------------------------------------------------------------------------------
; Nastavení přenosové rychlosti na 9600,8,N,1 pro Fmcu = IntRC 1 MHz (default pojistky)
;   UBRR=12 pro U2X=1

        ldi Temp, high(BAUD_PRESCALE2)
        out UBRRH, Temp
        ldi Temp, low(BAUD_PRESCALE2)
        out UBRRL, Temp

        ldi Temp, (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0)
        out UCSRC, Temp

        ldi Temp, (1<<U2X)
        out UCSRA, Temp

        ldi Temp, (1<<RXCIE)|(1<<RXEN)|(1<<TXEN)
        out UCSRB, Temp

        sei

;------------------------------------------------------------------------------
; Hlavní program

Hlavni_Smycka:

		rjmp Hlavni_Smycka
