;
; P1.asm
;
; Created: 12.10.2023 22:54:52
; Author : Safak Hazinedar
;

.include "m328pdef.inc"

.org 0x00
rjmp		init

init:
	ldi		r16, LOW(RAMEND)			; Stack einrichten für Befehle
	out		SPL, r16
	ldi		r16, HIGH(RAMEND)
	out		SPH, r16

	ldi		r17, 0xFF
	out		DDRD, r17					; Data direction register D (D0-D7) as output

	ldi		r18, 0x03
	out		DDRB, r18					; Data direction register B (B0-B1) as output

	ldi		r17, 0x01					; initialize D0

portD_left:								
	out		PORTD, r17					
	rcall	delay
	lsl		r17							; shift left until PortD is 0x00
	cpi		r17, 0x00					; 
		brne	portD_left					
	out		PORTD, r17					; D0-D7 = 0
	ldi		r18, 0x01					; switch to PortB

portB_left:	
	out		PORTB, r18					; D8 = 1
	rcall	delay
	lsl		r18							 
	cpi 	r18, 0x02					; length of PortB is variable 
		brne 	portB_left
										; D9 = 1 => last LED turned on
portB_right:							
	out		PORTB, r18					
	rcall	delay
	lsr		r18							; turn direction
	cpi		r18, 0x00					; shift right until PortB is 0x00
		brne	portB_right
	out		PORTB, r18					; turn of PortB or rather D8 & D9					
	ldi		r17, 0x80					

portD_right:									
	out		PORTD, r17					; turn on D7
	rcall	delay
	lsr		r17
	cpi		r17, 0x01					; shift right until D0 is reached
		brne	portD_right						
	jmp		portD_left					; turn direction

delay:
    ldi  r19, 17
    ldi  r20, 60
    ldi  r21, 204

L1: dec  r21                   
    brne L1                    
    dec  r20                   
    brne L1                     
    dec  r19                    
    brne L1                     
    ret