; *************************************************************************************
; ********** ZDJCIE NA KLANICIE, NIKON: (C) 2008 AUTOR RAD BRAD, LUCIDSCIENCE.COM
; ********** PLATFORMA : ATMEGA-88P Z WEWNTRZNYM OSCYLATOREM 8 MHZ
; *************************************************************************************

; DEFINICJE ATMEGA88
.include "m88def.inc"

; *************************************************************************************
; ********** DEFINICJE REJESTRW
; *************************************************************************************
.def t1 = r16
.def t2 = r17
.def t3 = r18
.def t4 = r19
.def clap = r20
.def reps = r21
.def lctr1 = r22
.def lctr2 = r23
.def wctr1 = r24
.def wctr2 = r25

; *************************************************************************************
; ********** WEKTORY RESETU I PRZERWA
; *************************************************************************************

; WEKTOR RESETU
reset:
rjmp startup
startup:

; WSKANIK STOSU
ldi t1 ,low(ramend)
out spl,t1
ldi t1 ,high(ramend)
out sph,t1

; *************************************************************************************
; ********** USTAWIENIE PRETWORNIKA ADC I PORTW WEJCIA-WYJCIA
; *************************************************************************************

; WEJCIE ANALOGOWE MIKROFONU
cbi ddrc,0
cbi portc,0

; STAN CZERWONEJ DIODY LED
sbi ddrb,0

; STAN NIEBIESKIEJ DIODY LED
sbi ddrd,7

; STAN ZIELONEJ DIODY LED
sbi ddrd,6

; WYJCIE DIODY PODCZERWONEJ
sbi ddrd,5

; WYJCIE BRZCZYKA PIEZOELEKTRYCZNEGO
sbi ddrd,0

; WEJCIE AUDIO
cbi ddrd,1

; USTAWIENIE JAOWEGO STANU PRZETWORNIKA ADC
lds t1,ADMUX
cbr t1,(1<<REFS1)
cbr t1,(1<<REFS0)
sbr t1,(1<<ADLAR)
cbr t1,(1<<MUX3)
cbr t1,(1<<MUX2)
cbr t1,(1<<MUX1)
cbr t1,(1<<MUX0)
sts ADMUX,t1
lds t1,ADCSRA
sbr t1,(1<<ADEN)
cbr t1,(1<<ADATE)
cbr t1,(1<<ADIE)
sbr t1,(1<<ADPS2)
cbr t1,(1<<ADPS1)
cbr t1,(1<<ADPS0)
sts ADCSRA,t1

; *************************************************************************************
; ********** START I INICJALIZACJA
; *************************************************************************************

; MIGNICIE DIODAMI I SYGNA DWIKOWY NA STARCIE
rcall delay
sbi portb,0
sbi portd,7
sbi portd,6
rcall beeper
rcall delay
cbi portb,0
cbi portd,7
cbi portd,6

; RESET REJESTRW
clr t1
clr t2
clr t3
clr clap
clr reps
clr lctr1
clr lctr2
clr wctr1
clr wctr2
clr xl
clr xh

; *************************************************************************************
; ********** GWNA PTLA
; *************************************************************************************
main:

; WCZENIE TRYBU AUTOMATYCZNEGO, JEELI PRZECZNIK JEST WCZONY
sbic pind,1
rjmp aut1

; TIMER AUTOMATYCZNEGO ZWOLNIENIA MIGAWKI PO 10 SEKUNDACH
ldi t1,4
adl:
sbi portb,0
sbi portd,7
rcall delay
cbi portb,0
cbi portd,7
rcall delay
dec t1
brne adl

; KONIEC OPӏNIENIA I WYKONANIE ZDJCIA
sbi portd,6
rcall beeper
rcall delay
rcall photosnap
cbi portd,6
rjmp main
aut1:

; ODCZYT WEJCIA PRZETWORNIKA ADC I USTAWIENIE REJESTRU CLAP
lds t1,ADCSRA
sbr t1,(1<<ADSC)
sts ADCSRA,t1
adcloop:
lds t1,ADCSRA
sbrc t1,ADSC
rjmp adcloop
lds clap,ADCH

; WYKRYCIE KLANICIA, STEROWANIE DIODAMI LED I ODLICZANIE ZWOKI
cpi clap,150
brne cdl1
sbi portb,0
ldi lctr1,20
ldi lctr2,255
cdl1:
cpi lctr1,0
breq cdl2
dec lctr2
brne cdl2
dec lctr1
cdl2:
cpi lctr1,0
brne cdl3
sbic pinb,0
inc reps
cbi portb,0
cdl3:

; WYKRYCIE KLANICIA I WYSTEROWANIE CZERWONEJ DIODY LED
cpi clap,150
brne dwl1
sbi portd,7
ldi wctr1,255
ldi wctr2,255
dwl1:
cpi wctr1,0
breq dwl2
dec wctr2
brne dwl2
dec wctr1
dwl2:
cpi wctr1,0
brne dwl3
clr reps
cbi portd,7
dwl3:

; WYKRYCIE TRZECIEGO KLANICIA I WYKONANIE ZDJCIA
cpi reps,3
brne tcv1
clr reps
clr lctr1
clr lctr2
clr wctr1
clr wctr2
cbi portd,6
cbi portd,7
sbi portd,6
rcall beeper
rcall delay
rcall photosnap
rcall delay
cbi portd,6
tcv1:

; KONTYNUACJA GWNEJ PTLI
rjmp main

; *************************************************************************************
; ********** PROCEDURA 1-SEKUNDOWEGO OPӏNIENIA
; *************************************************************************************
delay:
ldi yl,40
dly1:
ldi xh,high(60000)
ldi xl,low(60000)
dly2:
sbiw xl,1
brne dly2
dec yl
brne dly1
ret

; *************************************************************************************
; ********** BRZCZYK PIEZOELEKTRYCZNY
; *************************************************************************************
beeper:

; DWIK 1
ldi t1,100
bp1:
sbi portd,0
ldi xh,high(1500)
ldi xl,low(1500)
bp2:
sbiw xl,1
brne bp2
cbi portd,0
ldi xh,high(1500)
ldi xl,low(1500)
bp3:
sbiw xl,1
brne bp3
dec t1
brne bp1

; DWIK 2
ldi t1,100
bp4:
sbi portd,0
ldi xh,high(1200)
ldi xl,low(1200)
bp5:
sbiw xl,1
brne bp5
cbi portd,0
ldi xh,high(1200)
ldi xl,low(1200)
bp6:
sbiw xl,1
brne bp6
dec t1
brne bp4

; DWIK 3
ldi t1,100
bp7:
sbi portd,0
ldi xh,high(1000)
ldi xl,low(1000)
bp8:
sbiw xl,1
brne bp8
cbi portd,0
ldi xh,high(1000)
ldi xl,low(1000)
bp9:
sbiw xl,1
brne bp9
dec t1
brne bp7
ret

; *************************************************************************************
; ********** SYGNA PILOTA ZWALNIAJCY MIGAWK W APARACIE NIKON
; *************************************************************************************
photosnap:
ldi t3,2
snaploop:

; CYKL 1 = 16000 TAKTW ZEGARA, MODULACJA
ldi t1,77
c1:
sbi portd,5
ldi t2,33
m1:
dec t2
brne m1
nop
nop
nop
cbi portd,5
ldi t2,33
m2:
dec t2
brne m2
nop
nop
nop
dec t1
brne c1
nop

; CYKL 2 = 222640 TAKTW ZEGARA, PRZERWA
ldi xh,high(22264)
ldi xl,low(22264)
dl1:
nop
nop
nop
nop
nop
nop
sbiw xl,1
brne dl1

; CYKL 3 = 3120 TAKTW ZEGARA, MODULACJA
ldi t1,15
c2:
sbi portd,5
ldi t2,33
m3:
dec t2
brne m3
nop
nop
nop
cbi portd,5
ldi t2,33
m4:
dec t2
brne m4
nop
nop
nop
dec t1
brne c2
nop

; CYKL 4 = 12640 TAKTW ZEGARA, PRZERWA
ldi xh,high(1264)
ldi xl,low(1264)
dl3:
nop
nop
nop
nop
nop
nop
sbiw xl,1
brne dl3

; CYKL 5 = 3360 TAKTW ZEGARA, MODULACJA
ldi t1,16
c3:
sbi portd,5
ldi t2,33
m5:
dec t2
brne m5
nop
nop
nop
cbi portd,5
ldi t2,33
m6:
dec t2
brne m6
nop
nop
nop
dec t1
brne c3
nop

; CYKL 6 = 28640 TAKTW ZEGARA, PRZERWA
ldi xh,high(2864)
ldi xl,low(2864)
dl4:
nop
nop
nop
nop
nop
nop
sbiw xl,1
brne dl4

; CYKL 7 = 3200 TAKTW ZEGARA, MODULACJA
ldi t1,15
c4:
sbi portd,5
ldi t2,33
m7:
dec t2
brne m7
nop
nop
nop
cbi portd,5
ldi t2,33
m8:
dec t2
brne m8
nop
nop
nop
dec t1
brne c4
nop

; CYKL 8 = 505600 TAKTW ZEGARA, PRZERWA
ldi xh,high(50560)
ldi xl,low(50560)
dl5:
nop
nop
nop
nop
nop
nop
sbiw xl,1
brne dl5

; CYKL 8 = 505600 TAKTW ZEGARA, PRZERWA
ldi xh,high(50560)
ldi xl,low(50560)
dl6:
nop
nop
nop
nop
nop
nop
sbiw xl,1
brne dl6

; DWUKROTNE WYKONANIE SEKWENCJI
dec t3
sbrs t3,0
rjmp snaploop
ret
