;####################################################################################
;##                                                                                ##
;##   Dawid Urbanski                                                               ##
;##   10.2.2010r                                                                   ##
;##                                                                                ##
;##   kontakt: dawid.urb@wp.pl                                                     ##
;##   web: www.dawidurbanski.pl                                                    ##
;##                                                                                ##
;####################################################################################
;##   nazwa:                                                                       ##
;##   swiatlo rowerowe 1 guzik                                                     ##
;##   ver:                                                                         ##
;##   1.01                                                                         ##
;####################################################################################
;##   changelog:                                                                   ##
;##   1.01: dodano przewania (zmiana schematu)                                     ##
;####################################################################################
;##   schemat:                                                                     ##
;##   PORTD_1 - switch/guzik                                                       ##
;##                                                                                ##
;##   PORTB_1 - led(red) 1                                                         ##
;##   PORTB_2 - led(red) 2                                                         ##
;##   PORTB_3 - led(red) 3                                                         ##
;##   PORTB_4 - led(red) 4                                                         ##
;##                                                                                ##
;##   PORTB_5 - led(green) 5                                                       ##
;##   PORTB_6 - led(green) 6                                                       ##
;####################################################################################

.nolist
.include "tn2313def.inc"

;--przerwania-

.org  0b0000				;po resecie/wlaczeniu
rjmp  init

.org 0x0001					;INT0
rjmp  Nastepna
;-------------

;--definicje--
.list
.listmac

.def tmp     = r16
.def tmp2    = r17
.def tmp3    = r18
.def enable_f= r19
.def clock   = r20
;-------------

.cseg
;=========================================
init:	

	;inicjacja stosu progamowego
	ldi	tmp,LOW(RAMEND)	
	out	SPL,tmp

	;output: (piny)
	ldi	tmp,0xFF		; same jedynki
	out	PORTB,tmp		; PORTD - wyjscie w stanie wysokim	;DDRD

	;input: (switch)
	ldi	tmp,0b0000000100; aktywny pin
	out DDRD,tmp		; PORTB - wejscia PULL-UP (w stanie wysokim)	;PORTB

	;inicjacja przerwań
	cli						;globalna blokada przerwan
	ldi   r16, (1<<INT0)	;wlaczymy przerwania INT0,INT1
    out   GIMSK, r16 		;GIMSK register is used to enable and disable individual external interrupts.

	;wybór rodzaju sygnalu wzbudzajacego przerwanie (mozliwe tylko dla pinów INT0,INT1)
	in R16,MCUCR 
	sbr R16,1<<ISC01 		;zbocze...
	cbr R16,1<<ISC00 		;...narastajace
	out MCUCR,R16

	sei            			;wlacz przerwania

rjmp Main;wskocz do glównej petli programu
;=========================================

;####################################################################################
;## petla glówna programu
Main:

	;-----wybor trybu pracy
	CPI  tmp2,0x0	;porownaj natychmiast
		breq Main_0	;takie same: Main_0

	CPI  tmp2,0x1	;porownaj natychmiast
		breq Main_1	;takie same: Main_1
	;------------------------

rjmp	Main		;powrot do petli
;####################################################################################

;=========================================
Nastepna:

	INC  tmp2		;  zwieksz (nastepna funkcja)

	CPI  tmp2,0x2	;  jezeli przekroczono zakrez zacznij od zera
	breq Zeruj		;  takie same: Zeruj
reti

Zeruj:
	ldi  tmp2,0b0000000000	;  zeruj
reti; note: chcialem aby wracalo do wykonywania dalszej czesci..nie dziala
;=========================================


;=========================================
Main_0:;wszystko wylacz

	rcall PORTB_OFF

rjmp	Main		;  powrot do petli
;=========================================


;=========================================
Main_1:;wszystko miga

	rcall PORTB_ON

	ldi clock,2
	rcall Delay

	rcall PORTB_OFF

	ldi clock,2
	rcall Delay

rjmp	Main		;  powrot do petli
;=========================================


;####################################################################################
;## PORTD led swieci
PORTD_ON:
	ldi tmp,0xFF
	out	PORTD,tmp		; PORTD - wyjscie w stanie wysokim
ret
PORTD_0_ON:
	sbi PORTD,0
ret
PORTD_1_ON:
	sbi PORTD,1
ret
PORTD_2_ON:
	sbi PORTD,2
ret
PORTd_3_ON:
	sbi PORTD,3
ret
PORTD_4_ON:
	sbi PORTD,4
ret
PORTD_5_ON:
	sbi PORTD,5
ret
PORTD_6_ON:
	sbi PORTD,6
ret
;####################################################################################
;## PORTD led NIE swieci
PORTD_OFF:
	ldi tmp,0x00
	out	PORTD,tmp		; PORTD - wyjscie w stanie wysokim
ret

PORTD_0_OFF:
	cbi PORTD,0
ret
PORTD_1_OFF:
	cbi PORTD,1
ret
PORTD_2_OFF:
	cbi PORTD,2
ret
PORTD_3_OFF:
	cbi PORTD,3
ret
PORTD_4_OFF:
	cbi PORTD,4
ret
PORTD_5_OFF:
	cbi PORTD,5
ret
PORTD_6_OFF:
	cbi PORTD,6
ret
;####################################################################################
;## PORTB led swieci
PORTB_ON:
	ldi tmp,0xFF
	out	PORTB,tmp		; PORTD - wyjscie w stanie wysokim
ret		

PORTB_0_ON:
	sbi PORTB,0
ret
PORTB_1_ON:
	sbi PORTB,1
ret
PORTB_2_ON:
	sbi PORTB,2
ret
PORTB_3_ON:
	sbi PORTB,3
ret
PORTB_4_ON:
	sbi PORTB,4
ret
PORTB_5_ON:
	sbi PORTB,5
ret
PORTB_6_ON:
	sbi PORTB,6
ret

;####################################################################################
;## PORTB led NIE swieci
PORTB_OFF:
	ldi tmp,0x00
	out	PORTB,tmp		; PORTD - wyjscie w stanie wysokim
ret

PORTB_0_OFF:
	cbi PORTB,0
ret
PORTB_1_OFF:
	cbi PORTb,1
ret
PORTB_2_OFF:
	cbi PORTB,2
ret
PORTB_3_OFF:
	cbi PORTB,3
ret
PORTB_4_OFF:
	cbi PORTB,4
ret
PORTB_5_OFF:
	cbi PORTB,5
ret
PORTB_6_OFF:
	cbi PORTB,6
ret
;####################################################################################

;####################################################################################
;## opóznienie
;## ustawiamy rejestrem 19 'clock'
Delay:
	clr r1
	clr r2

delay_loop:
	dec r1
	brne delay_loop
	dec r2
	brne  delay_loop
	dec clock
	brne delay_loop
ret


.exit
;-----------------------------------------------------------------------------

