; 2010 (c) Bartosz Chodorowski <chomzee@ethernet.pl>
;                              <166142@student.pwr.wroc.pl>
;
; Lista 2, Zadanie 1
; 16 marca 2010
;


#include<usb1287def.inc>

.cseg
.org 0x0000
rjmp Start

.equ waitdelay=6

; global variables
.def counter=R16
.def ratio=R17

Start:
ldi R16, low(RAMEND)
ldi R17, high(RAMEND)
out SPH, R17
out SPL, R16

ldi R16, 0b11100000	;PORTB linie 5 do 7 w trybie wejściowym
in R17, PORTB
or R17, R16
out PORTB, R17

ldi R16, 0b00110000	;PORTE linie 4 i 5 w trybie wejściowym
in R17, PORTE
or R17, R16
out PORTE, R17

ldi R16, 0b11110000	;linie 4-7 portu D w trybie wyjściowym, 
in R17, DDRD
or R17, R16			;zasilanie diod LED
out DDRD, R17

ldi ratio, 10


; ------------ MainLopp begins here ------------
MainLoop:


clr counter

Loop1:
cp counter, ratio
breq Loop1End
call Green
inc counter
rjmp Loop1
Loop1End:

Loop2:
cpi counter, 0xff
breq Loop2End
call Red
inc counter
rjmp Loop2
Loop2End:


call None

sbis PINB, 7
call Up
sbis PINE, 5
call Down

rjmp MainLoop
; ------------ MainLopp ends here ------------


; ------------ Up begins here ------------
Up:
inc ratio
brne UpEnd
ldi ratio, 0xff

UpEnd:
ret
; ------------ Up ends here ------------


; ------------ Down begins here ------------
Down:
dec ratio
cpi ratio, 0xff
brne DownEnd
ldi ratio, 0

DownEnd:
ret
; ------------ Down ends here ------------


; ------------ Green begins here ------------
Green:
push R16
push R17
in R16, PORTD
andi R16, 0x0F
ori R16, 0x40			;0b0100 0000(górna zielona)
out PORTD, R16

ldi R17, waitdelay
GreenRed:
dec R17
brne GreenRed

pop R17
pop R16
ret
; ------------ Green ends here ------------


; ------------ Red begins here ------------
Red:
push R16
push R17
in R16, PORTD
andi R16, 0x0F
ori R16, 0x80			;0b1000 0000(górna zielona)
out PORTD, R16

ldi R17, waitdelay
LoopRed:
dec R17
brne LoopRed

pop R17
pop R16
ret
; ------------ Red ends here ------------


; ------------ None begins here ------------
None:
push R16
in R16, PORTD
andi R16, 0x0F
out PORTD, R16
pop R16
ret
; ------------ None ends here ------------
