;ledsatmega8zz.asm		programa para acender ciclicamente 1 dentre 8 leds ligados
;			aos 8 bits da porta B, no modo "vai-e-volta".
;			NCM mar/2008
;                       should work with any AVR with an 8-bit portB
;******************************************************************************
.include "m88def.inc"

	rjmp	RESET		;reset handle

;* Register variables

	.def  ledON   = r16
	.def  T1      = r17
	.def  T2      = r18
	.def  temp    = r19
	.equ  T1ini   = 200
	.equ  T2ini   = 80

;******************************************************************    
delay:     			; delay subroutine - uses 2 nested do-nothing loops
				; outer loop counts from T1ini down to zero
				; inner loop counts from T2ini down to zero


	ldi   T1,T1ini	;T1 used as outer loop count
delay_1:  			;delay outer loop  
	ldi   T2,T2ini	;T2 used as inner inner count
delay_2:			;delay inner loop
	dec   T2
	brne  delay_2   
	dec   T1
	brne  delay_1
	ret                     
;********************************************************************


RESET:
	ldi temp, low(RAMEND)		;initialize stack pointer
	out SPL, temp			;to point to the end of RAM 
	ldi temp, high(RAMEND)		;uncomment these 2 lines
	out SPH, temp			;for AVR´s with 16-bit stack pointer

	ser temp			;make DDRB all ones, ie:
	out DDRB, temp			;all portB bits are outputs

	ldi ledON,1			;initially turn ON the rigtmost led

goingL:
	out PORTB,ledON			;turn ON the single led specified
					;by the single bit set in ledON
	;rcall delay			;wait a bit before continuing
	lsl ledON			;move to the next led left
	cpi ledON,0b10000000		;is this the leftmost led?	
	brne goingL			;no-continue going left
					;yes-change direction
goingR:
	out PORTB,ledON			;turn ON the single led specified
					;by the single bit set in ledON
	;rcall delay			;wait a bit before continuing
	lsr ledON			;move to the next led right
	cpi ledON,1			;is this the rigtmost led?	
	brne goingR			;no-continue going right
	rjmp goingL			;yes-change direction
