	org 0h			
	jmp start

; ***** prirazeni promennych *****
	sega bit p3.7			;segment a
	segb bit p1.5			;segment b
	segc bit p1.6			;segment c
	segd bit p1.4			;segment d
	sege bit p1.2			;segment e
	segf bit p1.0			;segment f	
	segg bit p1.3			;segment g
	zobr1 bit p3.2			;zobrazovac 1
	zobr2 bit p3.3			;zobrazovac 2
	zobr3 bit p1.7			;zobrazovac 3
	dt bit p1.1			;desetinna tecka
	vstup bit p3.4			;vstupni port
	napajeni bit p3.0		;vystup pro napajeni
	
	pdr1 equ 20			;registr pro odpocet power down
	pdr2 equ 21			;registr pro odpocet power down
	vcek1 equ 22			;registr pro mereni delky impulsu
	vcek2 equ 23			;registr pro odpocet pauzy mezi impulsy
	vcek3 equ 24			;registr pro odpocet pauzy mezi impulsy
	vysledek equ 25			;registr s hodnotou posledniho vysledku
	ctverimp equ 26			;registr pro zjisteni 4 impulsu
	cekr1 equ 15			;registr pro cekaci smycku
	cekr2 equ 16			;registr pro cekaci smycku
	efr equ	17			;registr pro pocet efektu po resetu

	powd bit 21			;ukazatel pro odpocet power down
	kratke bit 35			;ukazatel na pocitani kratkych impulsu
	pauzaimp bit 34			;ukazatel na odpocet pauzy mezi impulsy

; ***** casovac 0 - mereni *****
	org 0Bh
	mov th0,#0d0h
	mov tl0,#050h			;preteceni kazdych 20ms (pro 7.372 MHz krystal)
	inc r7
	cjne r7,#0,s1			;sem za 5,1s = zobrazeni pomlcek = nezmereno
	clr tr0
	mov r1,#10
	mov r2,#10
	mov r3,#10
	mov r7,#0
	setb dt
	setb powd			;povoleni odpoctu power down
	mov ctverimp,#4
s1:	reti

	org 870
; ***** zacatek *****
start:	mov r1,#0
	mov r2,#0
	mov r3,#0
	mov r5,#0
	mov r6,#0
	mov ctverimp,#4
	mov vysledek,#0
	mov vcek1,#20
	mov vcek2,#0
	mov vcek3,#30
	clr kratke
	setb powd
	setb pauzaimp
	clr napajeni
	mov ie,#10000010b
	mov tmod,#00000001b
	mov th0,#0d0h
	mov tl0,#050h
	clr it0
	call efekt
	setb tr0
	call vystup
	jmp $
	
; ***** vypocet *****
vypocet:cpl dt
	djnz ctverimp,s5
	mov ctverimp,#4
	clr powd
	mov pdr1,#0
	mov pdr2,#0
	jmp s4
s5:	ret

s4:	jb kratke,s9
	cjne r7,#64,s2			;na s4 = prisly 4 impulsy
	jmp s3
s2:	jnc s3
s9:	setb kratke			;perioda 4 impulsu je mensi nez 64*20ms, bude se pocitat 8 impulsu
	setb tr0
	inc r5				
	cjne r5,#4,s5
	mov r5,#0
	clr tr0				
	clr kratke
	mov dptr,#tab2
	mov a,r7
	movc a,@a+dptr
	mov vysledek,a
	mov r7,#0
	call zapis
	ret
		
s3:	mov dptr,#tab1			;na s3 = perioda 4 impulsu je vetsi nez 64*20ms vcetne, dlouhe impulsy
	mov a,r7
	movc a,@a+dptr
	mov vysledek,a
	mov r7,#0
	call zapis
	ret

;***** zapis vysledku na vystup *****
zapis:	mov a,vysledek
	mov b,#10
	div ab
	mov r3,b
	mov b,#10
	div ab
	mov r2,b
	mov r1,a
	ret

;***** cekaci smycka kratka *****
cekej:	mov r4,#128
ccc1:	djnz r4,ccc1
	ret

;***** multiplex displaye *****
vystup:	mov r0,#1
sk16:	jnb powd,sk14
	djnz pdr1,sk14
	djnz pdr2,sk14
	call nic
	setb napajeni
	mov pcon,#00000010b
	jmp $

sk14:	cjne r0,#1,sk12
	setb zobr3
	call nic
	clr zobr1
	jmp sk11
sk12:	cjne r0,#2,sk13
	setb zobr1
	call nic
	clr zobr2
	jmp sk11
sk13:	cjne r0,#3,sk11
	setb zobr2
	call nic
	clr zobr3

sk11:	cjne @r0,#0,sk9
	call nula
	call cekej
	jmp sk10
sk9:	cjne @r0,#1,sk1
	call jednic
	call cekej
	jmp sk10
sk1:	cjne @r0,#2,sk2
	call dvojka
	call cekej
	jmp sk10
sk15:	jmp sk16
sk2:	cjne @r0,#3,sk3
	call trojka
	call cekej
	jmp sk10
sk3:	cjne @r0,#4,sk4
	call ctyrka
	call cekej
	jmp sk10
sk4:	cjne @r0,#5,sk5
	call petka
	call cekej
	jmp sk10
sk5:	cjne @r0,#6,sk6
	call sestka
	call cekej
	jmp sk10
sk6:	cjne @r0,#7,sk7
	call sedmic
	call cekej
	jmp sk10
sk21:	jmp sk15
sk7:	cjne @r0,#8,sk8
	call osmic
	call cekej
	jmp sk10
sk8:	cjne @r0,#9,sk17
	call devitk
	call cekej
	jmp sk10
sk17:	cjne @r0,#10,sk10
	call pomlck
	call cekej

sk10:	jnb pauzaimp,sk110
	djnz vcek2,s121
	djnz vcek3,s121
	clr pauzaimp
sk110:	jb vstup,s121
	djnz vcek1,s122
	jb vstup,s121
	jb tr0,s321
	setb tr0
s321:	setb pauzaimp			;nacten prichozi impuls
	mov vcek2,#0
	mov vcek3,#2
	call vypocet
s121:	mov vcek1,#20
s122:	inc r0
	cjne r0,#4,sk21
	mov r0,#1
	jmp sk21

; ***** zobrazeni cislic *****
nic:	setb sega
	setb segb
	setb segc
	setb segd
	setb sege
	setb segf
	setb segg
	ret
nula:	clr sega
	clr segb	
	clr segc
	clr segd	
	clr sege	
	clr segf
	setb segg
	ret
jednic:	setb segc
	clr sega
	clr segb
	setb segd
	setb sege
	setb segf
	setb segg
	ret
dvojka:	clr sega
	clr segf
	setb sege
	clr segg
	clr segc
	clr segd
	setb segb
	ret
trojka:	clr sega
	clr segb
	clr segg
	clr segc
	clr segf
	setb segd
	setb sege
	ret
ctyrka: setb segc
	setb segd
	setb segf
	clr sege
	clr segg
	clr segb
	clr sega
	ret
petka:	clr segb
	setb sega
	clr segf
	clr segg
	clr segc
	clr sege
	setb segd
	ret
sestka:	clr segb
	setb sega
	clr segf
	clr segg
	clr segc
	clr segd
	clr sege
	ret
sedmic:	clr sega
	clr segb
	clr segf
	setb segd
	setb sege
	setb segc
	setb segg
	ret
osmic:	clr sega
	clr segb
	clr segc
	clr segd
	clr sege
	clr segf
	clr segg
	ret
devitk:	clr sega
	clr segb
	clr segc
	clr segf
	clr sege
	clr segg
	setb segd
	ret
pomlck:	clr segg
	setb sega
	setb segb
	setb segc
	setb segd
	setb sege
	setb segf
	ret

; ***** cekaci smycka dlouha *****
cekejdl:mov cekr1,#0
	mov cekr2,#80
cs1:	djnz cekr1,$
	djnz cekr2,cs1
	ret

; ***** reset efekt *****
efekt:	mov efr,#5
ef2:	djnz efr,ef1
	call nic
	call cekej
	call cekej
	call cekej
	call cekej
	call cekej
	ret
	call nic
ef1:	clr zobr1
	clr zobr2
	clr zobr3
	clr sega
	setb segf
	call cekejdl
	clr segb
	setb sega
	call cekejdl
	clr segc
	setb segb
	call cekejdl
	clr segd
	setb segc
	call cekejdl
	clr sege
	setb segd
	call cekejdl
	clr segf
	setb sege
	call cekejdl
	jmp ef2

; ***** prevodni tabulky ****
tab1: 	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0		;20
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0		;40
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0		;60
	DB 0,0,0,188,185,182,179,176,174,171,169,167		;72
	DB 164,162,160,158,156,154,152,150,148,146,145		;83
	DB 143,141,140,138,136,135,133,132,130,129,128		;94
	DB 126,125,124,122,121,120,119,118,117,115,114,113	;106
	DB 112,111,110,109,108,107,106,105,104,103,103,102	;118
	DB 101,100,99,98,98,97,96,95,94,94,93,92,92,91,90	;133
	DB 90,89,88,88,87,86,86,85,85,84,83,83,82,82,81,81	;149
	DB 80,79,79,78,78,77,77,76,76,75,75,75,74,74,73,73	;165
	DB 72,72,71,71,71,70,70,69,69,69,68,68,67,67,67,66	;181
	DB 66,66,65,65,65,64,64,63,63,63,63,62,62,62,61,61	;197
	DB 61,60,60,60,59,59,59,59,58,58,58,57,57,57,57,56	;213
	DB 56,56,56,55,55,55,55,54,54,54,54,53,53,53,53,52	;229
	DB 52,52,52,52,51,51,51,51,50,50,50,50,50,49,49,49	;245
	DB 49,49,48,48,48,48,48,47,47				;254

tab2:	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		20
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		40
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		60	
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		80
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		100
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		120
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		140
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		160
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		180
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;		200
	DB 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;			217
	DB 220,219,218,217,216,215,214,213,212,211;		227
	DB 211,210,209,208,207,206,205,204,203,203;		237
	DB 202,201,200,199,198,198,197,196,195,194;		247
	DB 194,193,192,191,190,0,0;				254
	

	end
