; redFrik 050803

.nolist
.include "c:\program files\atmel\avr tools\avrassembler\appnotes\tn26def.inc"
.list

.def temp= r16
.def index= r17
.def lastindex= r18
.def flags= r19

.cseg
.org 0x00

;--
start:
	ldi r16, RAMEND					;Init Stack Pointer
	out SP, r16
	ldi temp, 255					;port as output
	out DDRA, temp
	ldi index, 0
	ldi lastindex, 255				;force output at startup
	rcall updateflag
	;ldi flags, 0b00000001
	;out portA, flags

;--	
main:
	rcall buttons
	;rcall network
	cpc index, lastindex
	breq main
	mov lastindex, index
	out portA, flags
	rjmp main

;--	
buttons:
	sbis pinB, 4
	rcall but0
	sbis pinB, 5
	rcall but1
	sbis pinB, 6
	rcall but2
	sbis pinB, 3
	rcall but3
	ret
	
but0:
	sbrc flags, 4
	ret
	ldi index, 0
	rcall updateflag
	ret
but1:
	sbrc flags, 5
	ret
	ldi index, 1
	rcall updateflag
	ret
but2:
	sbrc flags, 6
	ret
	ldi index, 2
	rcall updateflag
	ret
but3:
	sbrc flags, 7
	ret
	ldi index, 3
	rcall updateflag
	ret

;--
updateflag:
	cpi index, 0
	brne updateflag2
	ldi flags, 0b00010000		;0-1 max454, 2-3 max454, 4-7 leds
	ret
updateflag2:
	cpi index, 1
	brne updateflag3
	ldi flags, 0b00100101
	ret
updateflag3:
	cpi index, 2
	brne updateflag4
	ldi flags, 0b01001010
	ret
updateflag4:
	ldi flags, 0b10001111
	ret
