; 2010 (c) Bartosz Chodorowski <chomzee@ethernet.pl>
;                              <166142@student.pwr.wroc.pl>
;
; Lista 2, Zadanie 2
; 16 marca 2010
;


#include<usb1287def.inc>

.cseg
.org 0x0000
rjmp Start

.equ waitdelay=6

; global variables
.def counter=R16
.def tmp=R17
.def ratio=R18
.def const1=R19
.def const2=R20
.def waiter=R21

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
clr waiter
ldi const1, 21
ldi const2, 197

; ------------ MainLopp begins here ------------
MainLoop:

clr counter

Loop1:
cp counter, ratio
breq Loop1End
call Red
inc counter
rjmp Loop1
Loop1End:

Loop2:
cpi counter, 99
breq Loop2End
call Green
inc counter
rjmp Loop2
Loop2End:


call None

inc waiter
cpi waiter, 0
brne MainLoop
clr waiter
mul ratio, const1
mov ratio, R0
LoopMod2:
cpi ratio, 100
brlo LoopMod2End
subi ratio, 100
rjmp LoopMod2
LoopMod2End:

add ratio, const2
LoopMod:
cpi ratio, 100
brlo LoopModEnd
subi ratio, 100
rjmp LoopMod
LoopModEnd:

rjmp MainLoop
; ------------ MainLopp 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 ------------
