.include "m48def.inc"
 
.def temp1         = r17
 
.equ XTAL = 1000000
 
    rjmp    init
 
;.include "keys.asm" 
;
;
 
init:
    ldi      temp1, HIGH(RAMEND)     ; Stackpointer initialisieren
    out      SPH, temp1
    ldi      temp1, LOW(RAMEND)
    out      SPL, temp1
	; Port B auf Ausgang
    ldi      temp1, 0x06
    out      DDRB, temp1
    ldi r16, 0x00            ; Port D auf Eingang    
    out DDRD, r16
    ;
    ; Timer 1 einstellen
    ;
    ; Modus 14:
    ;    Fast PWM, Top von ICR1
    ;
    ;     WGM13    WGM12   WGM11    WGM10
    ;      1        1       1        0
    ;
    ;    Timer Vorteiler: 256
    ;     CS12     CS11    CS10
    ;      1        0       0
    ;
    ; Steuerung des Ausgangsport: Set at BOTTOM, Clear at match
    ;     COM1A1   COM1A0
    ;      1        0
    ;
 
    ldi      temp1, (1<<COM1A1) | (1<<COM1B1) | (1<<WGM10) | (1<<WGM11)
    sts      TCCR1A, temp1
 
    ldi      temp1, (1<<WGM13) | (1<<WGM12) | (1<<CS11)
    sts      TCCR1B, temp1
 
    ;
    ; den Endwert (TOP) für den Zähler setzen
    ; der Zähler zählt bis zu diesem Wert
    ;
    ldi      temp1, HiGH(1000)
    sts      ICR1H, temp1
    ldi      temp1, LOW(1000)
    sts      ICR1L, temp1
 
    ;
    ; der Compare Wert
    ; Wenn der Zähler diesen Wert erreicht, wird mit
    ; obiger Konfiguration der OC1A Ausgang abgeschaltet
    ; Sobald der Zähler wieder bei 0 startet, wird der
    ; Ausgang wieder auf 1 gesetzt
    ;
main:
	  rcall    geradeaus
	  rcall    WAIT_1s
	  rcall 	 linkskurve
	  rcall    WAIT_1s
	  rcall	   rechtskurve
	  rcall    WAIT_1s
	  rcall	   stop
	  rcall    WAIT_1s
	  rjmp		 main
geradeaus:
    ldi      temp1, HIGH(600)
    sts      OCR1AH, temp1
    ldi      temp1, LOW(600)
    sts      OCR1AL, temp1
    ldi      temp1, HIGH(600)
    sts      OCR1BH, temp1
    ldi      temp1, LOW(600)
    sts      OCR1BL, temp1
ret

stop:
    ldi      temp1, HIGH(0)
    sts      OCR1AH, temp1
    ldi      temp1, LOW(0)
    sts      OCR1AL, temp1
    ldi      temp1, HIGH(0)
    sts      OCR1BH, temp1
    ldi      temp1, LOW(0)
    sts      OCR1BL, temp1
ret

linkskurve:

    ldi      temp1, HIGH(600)
    sts      OCR1AH, temp1
    ldi      temp1, LOW(600)
    sts      OCR1AL, temp1
    ldi      temp1, HIGH(300)
    sts      OCR1BH, temp1
    ldi      temp1, LOW(300)
    sts      OCR1BL, temp1
ret

rechtskurve:

    ldi      temp1, HIGH(300)
    sts      OCR1AH, temp1
    ldi      temp1, LOW(300)
    sts      OCR1AL, temp1
    ldi      temp1, HIGH(600)
    sts      OCR1BH, temp1
    ldi      temp1, LOW(600)
    sts      OCR1BL, temp1
ret

warte:
        push temp1
        push temp2
    

    
         ldi temp1, 160
banane:  dec temp1

         ldi temp2, 255
apfel:   dec temp2
         cpi temp2, 0
         brne apfel

         cpi temp1, 0
         brne banane

        
         pop temp2
         pop temp1
         ret




WAIT_1s:
    push temp1           ; temp1 auf dem Stack sichern            
    push temp2           ; temp2 auf dem Stack sichern            
    push temp3           ; temp3 auf dem Stack sichern            

;------------
    ldi    temp3,5            
    clr temp2
    clr    temp1            
wait1s_w:    
    dec    temp1
    brne    wait1s_w
    dec    temp2        
    brne    wait1s_w
    dec    temp3
    brne    wait1s_w
;------------
    pop temp3              ; temp3 wiederherstellen                    
    pop temp2              ; temp2 wiederherstellen                    
    pop temp1              ; temp1 wiederherstellen                    
ret
WAIT_5s:
    rcall WAIT_1s
    rcall WAIT_1s
    rcall WAIT_1s
    rcall WAIT_1s
    rcall WAIT_1s
    ret


