	$modxx51
Timer1	equ	(65535+1)-610	;nemenit
Delitel1	equ	100	;nemenit
Delitel2	equ	8	;nemenit
Kalibrace1	equ	179	;tohle menit max. v rozsahu +/- 2
Kalibrace2	equ	6	;tohle od 1 do 100
	;doba pipani budiku
DobaPipani	equ	20
	;chovani tlacitek
AkcelerKonst	equ	60
AutoLowSp	equ	25
AutoHighSp	equ	3
	;prirazeni vyvodu
Pipak	equ	P3.7
Katody	equ	P1.0
COUT	equ	P3.1
DOUT	equ	P3.0
BUFF	equ	P3.2
BSet	equ	acc.3
BInc	equ	acc.4
BDec	equ	acc.5

	dseg	at 30h
HH:	ds	1
MM:	ds	1
SS:	ds	1
HHal:	ds	1
MMal:	ds	1
Cifra123456:	ds	1
TohleNaDisp:	ds	1
Hrany:	ds	1
Stavy:	ds	1

Zasobnik:	ds	40

	bseg	at 20h
SetupTime:	dbit	1
SetupAlarm:	dbit	1
SetupMOD:	dbit	1
AlAktivni:	dbit	1
Blikani:	dbit	1
CasRun:	dbit	1
DrziBset:	dbit	1
AutoInc:	dbit	1
AutoDec:	dbit	1
Pipani:	dbit	1
Preskocit:	dbit	1
NecoProAlarm:	dbit	1

	cseg		
	org	0
	jmp	Init
	org 	00Bh
	jmp	MPX
			
	;inicializace
Init:	mov	P1,#11111111b
	mov	P3,#255
	;nastaveni vychoziho casu
	mov	HH,#12h
	mov	MM,#00h
	mov	SS,#00h
	;nastaveni vychoziho casu pro budik
	mov	HHal,#12h
	mov	MMal,#01h
	;zbytek nastaveni
	mov	sp,#Zasobnik
	mov	Cifra123456,#100
	mov	R1,#Delitel2
	mov	R0,#Delitel1
	
	clr	rs0
	clr	rs1
	
	setb	rs1
	mov	R6,#0
	mov	R5,#1
	clr	rs1
	
	clr	SetupTime
	clr	SetupAlarm
	clr	SetupMOD
	clr	AlAktivni
	clr	Blikani
	clr	DrziBSet
	clr	Pipani
	setb	CasRun
	clr	Preskocit
	clr	NecoProAlarm
	
	mov	th0,#high Timer1
	mov	tl0,#low Timer1
	mov	scon,#00000000b
	mov	tmod,#00000001b
	setb	ea
	clr	es
	setb	et0
	setb	tr0
				
	jmp	$
	;zjistovani jestli se ma spustit alarm
TestAlarm:	setb	rs1
	mov	R1,SS
	mov	R2,MM
	mov	R3,HH
	mov	a,MMal
	cjne	R1,#0,NespustitAl
	clr	c
	subb	a,R2
	jnz	NespustitAl
	mov	a,HHal
	clr	c
	subb	a,R3
	jnz	NespustitAl
	setb	Pipani
	mov	R7,#0
NespustitAl:	cjne	R7,#DobaPipani,NevypinatAl
	clr	Pipani
NevypinatAl:	jnb	Pipani,ManOff
	jb	P3.3,ManOff
	clr	Pipani
	mov	R6,#128
ManOff:	clr	rs1
	ret
	;Pricteni vteriny
PrictiSec:	setb	rs1
	inc	R7
	clr	rs1
	inc	SS
	mov	a,SS
	clr	c
	da	a
	mov	SS,a
	cjne	a,#60h,ZblitekJeOk
	mov	SS,#0
	inc	MM
	mov	a,MM
	clr	c
	da	a
	mov	MM,a
	cjne	a,#60h,ZblitekJeOk
	mov	MM,#0
	inc	HH
	mov	a,HH
	clr	c
	da	a
	mov	HH,a
	cjne	a,#24h,ZblitekJeOk
	mov	HH,#0
ZblitekJeOk:	ret
	;nastaveni - nejvetsi dilo vsech dob !!!!!
Nastaveni:	;se nactou vstupy
	mov	a,P3
	mov	Stavy,a
	xrl	a,R7
	mov	R7,a
	mov	a,P3
	cpl	a
	anl	a,R7
	mov	Hrany,a
	;osetreni zakmitu BSet
	setb	rs1
	mov	a,R6
	jz	VolnoBSet
	dec	R6
	mov	Hrany,#0
VolnoBSet:	clr	rs1
	mov	a,Stavy
	jb	BSet,NeBlokSet
	setb	rs1
	mov	R6,#25
	clr	rs1
	;se provadi vlastni nastaveni
NeBlokSet:	jb	SetupMOD,Vnastaveni
	mov	a,Hrany
	jnb	BSet,NicSet
	;po prvnim stisku cudlu set
	mov	c,AlAktivni
	cpl	c
	mov	Blikani,c
	setb	SetupMOD
	setb	SetupAlarm
	jmp	NicSet
Vnastaveni:	;tu to ceka po prvnim stisku
	mov	a,Hrany
	jb	BInc,IncMM
	jb	BDec,DecMM
	mov	a,Stavy
	jnb	BInc,IncMMSec
	jnb	BDec,DecMMSec
	jb	DrziBSet,OnOffAl
	mov	a,Hrany
	jnb	BSet,NicSet
	jb	SetupTime,KratkejByl
	jmp	PredOnOffAl
	;po druhem kratkem stisku cudlu set
KratkejByl:	clr	DrziBSet
	jb	SetupTime,KonecSetMOD
	clr	SetupAlarm
	setb	SetupTime
	setb	Blikani
	jmp	NicSet
KonecSetMOD:	clr	SetupMOD
	clr	Blikani
	clr	SetupTime
	setb	CasRun
NicSet:	mov	R7,P3
	ret
	;zapinani vypinani budiku
PredOnOffAl:	setb	DrziBSet
	mov	R6,#0
	jmp	NicSet
OnOffAl:	djnz	R6,WaitOnOffal
	;po prodleve
	cpl	AlAktivni
	mov	c,AlAktivni
	cpl	c
	mov	Blikani,c
	clr	DrziBSet
	jmp	NicSet
WaitOnOffAl:	mov	a,Stavy
	jb	BSet,KratkejByl
	jmp	NicSet
	
	;pricitani tlacitkem +
IncMM:	mov	R4,#AutoLowSp
	mov	R3,#AutoLowSp
	mov	R5,#AkcelerKonst
	clr	AutoInc
	acall	PrictiMMHH
	mov	R2,#0
IncMMSec:	jb	AutoInc,IncAuto
	djnz	R2,NeIncAuto
	setb	AutoInc
IncAuto:	cjne	R4,#AutoHighSp,IncJoSpUp
	jmp	IncNeSpUp
IncJoSpUp:	djnz	R5,IncNeSpUp
	mov	R5,#AkcelerKonst
	dec	R4
IncNeSpUp:	djnz	R3,NeIncAuto
	mov	a,R4
	mov	R3,a
	acall	PrictiMMHH	
NeIncAuto:	jmp	NicSet
	;odecitani tlacitkem -
DecMM:	mov	R4,#AutoLowSp
	mov	R3,#AutoLowSp
	mov	R5,#AkcelerKonst
	clr	AutoDec
	acall	OdectiMMHH
	mov	R2,#0
DecMMSec:	jb	AutoDec,DecAuto
	djnz	R2,NeDecAuto
	setb	AutoDec
DecAuto:	cjne	R4,#AutoHighSp,DecJoSpUp
	jmp	DecNeSpUp
DecJoSpUp:	djnz	R5,DecNeSpUp
	mov	R5,#AkcelerKonst
	dec	R4
DecNeSpUp:	djnz	R3,NeDecAuto
	mov	a,R4
	mov	R3,a
	acall	OdectiMMHH	
NeDecAuto:	jmp	NicSet
	;Pricteni HH a MM
PrictiMMHH:	setb	rs0
	;nejdriv vyber cas/alarm
	jb	SetupAlarm,IncMMHHsel
	clr	CasRun
	clr	rs0
	mov	R0,#Delitel1
	mov	R1,#Delitel2
	setb	rs0
	clr	Blikani
	mov	SS,#0
	mov	R1,MM
	mov	R2,HH
	jmp	UzIncMMHH
IncMMHHsel:	setb	AlAktivni
	clr	Blikani
	mov	R1,MMal
	mov	R2,HHal
UzIncMMHH:	inc	R1
	mov	a,R1
	clr	c
	da	a
	mov	R1,a
	cjne	a,#60h,ZbytekIncOk
	mov	R1,#0
	inc	R2
	mov	a,R2
	clr	c
	da	a
	mov	R2,a
	cjne	a,#24h,ZbytekIncOk
	mov	R2,#0
ZbytekIncOk:	jb	SetupAlarm,TedIncAlarm
	mov	MM,R1
	mov	HH,R2	
	jmp	KonecIncMMHH
TedIncAlarm:	mov	MMal,R1
	mov	HHal,R2
KonecIncMMHH:	clr	rs0
	ret
	;Odecteni HH a MM
OdectiMMHH:	setb	rs0
	jb	SetupAlarm,DecMMHHsel
	clr	CasRun
	clr	rs0
	mov	R0,#Delitel1
	mov	R1,#Delitel2
	setb	rs0
	clr	Blikani
	mov	SS,#0
	mov	R6,MM
	mov	R7,HH
	jmp	UzDecMMHH
DecMMHHsel:	setb	AlAktivni
	clr	blikani
	mov	R6,MMal
	mov	R7,HHal	
UzDecMMHH:	mov	a,R6
	anl	a,#00001111b
	mov	R1,a
	mov	a,R6
	anl	a,#11110000b
	mov	R2,a
	mov	a,R7
	anl	a,#00001111b
	mov	R3,a
	mov	a,R7
	anl	a,#11110000b
	mov	R4,a
	dec	R1
	cjne	R1,#255,ZbytekDecOk
	mov	R1,#9
	mov	a,R2
	clr	c
	subb	a,#16
	mov	R2,a
	cjne	R2,#240,ZbytekDecOk
	mov	R2,#80
	dec	R3
	cjne	R3,#255,ZbytekDecOk
	cjne	R4,#0,DecNic9
	mov	R3,#3
	jmp	DecNeco9
DecNic9:	mov	R3,#9
DecNeco9:	mov	a,R4
	clr	c
	subb	a,#16
	mov	R4,a
	cjne	R4,#240,ZbytekDecOk
	mov	R4,#32
ZbytekDecOk:	mov	a,R1
	orl	a,R2
	mov	R6,a	
	mov	a,R3
	orl	a,R4
	mov	R7,a
	jb	SetupAlarm,TedDecAlarm
	mov	MM,R6
	mov	HH,R7
	jmp	KonecDecMMHH
TedDecAlarm:	mov	MMal,R6
	mov	HHal,R7
KonecDecMMHH:	clr	rs0
	ret
	;multiplexni zobrazeni cifer na displejich
MPX:	setb  rs1
	djnz	R5,$
	clr	rs1
	mov	th0,#high Timer1
	mov	tl0,#low Timer1
	;tady se generuje pipani alarmu
	setb	rs1
	mov	R5,#1
	clr	rs1
	jnb	Pipani,SemSkocit
	mov	a,R1
	jb	acc.0,SemSkocit
	clr	Pipak
SemSkocit:	cpl	Preskocit
	jnb	Preskocit,NeKonecMPX
	jmp	KonecMPX
	;pricitani vteriny
NeKonecMPX:	jnb	CasRun,CasStoji
	djnz	R0,CasStoji
	mov	R0,#Delitel1
	setb	rs1
	mov	R5,#Kalibrace1
	clr	rs1
	djnz	R1,CasStoji
	mov	R1,#Delitel2
	setb	rs1
	mov	a,#Kalibrace1
	add	a,#Kalibrace2
	mov	R5,a
	clr	rs1
	acall	PrictiSec
	;spustit alarm?
CasStoji:	jb	SetupMOD,NeTestAl
	jnb	AlAktivni,NeTestAl
	acall	TestAlarm
	jb	Pipani,NeNastaveni
	;testni nastaveni
NeTestAl:	acall	Nastaveni
	;Zhasnuti displeje
NeNastaveni:	mov	a,#255
	jnb	Katody,SkocSem
	mov	a,#0
SkocSem:	clr	ti
	mov	sbuf,a
	jnb	ti,$
	setb	BUFF
	clr	BUFF
	;vyber alarm/cas
	jb	SetupAlarm,ZobrazAl
	;vyber v tomto cyklu zobrazovane cifry pro cas
	mov	a,Cifra123456	
	cjne	a,#100,SS1_ne
	mov	a,SS
	anl	a,#00001111b
	mov	TohleNaDisp,a
SS1_ne:	cjne	a,#101,SS10_ne
	mov	a,SS
	swap	a
	anl	a,#00001111b
	mov	TohleNaDisp,a
SS10_ne:	cjne	a,#102,MM1_ne
	mov	a,MM
	anl	a,#00001111b
	mov	TohleNaDisp,a
MM1_ne:	cjne	a,#103,MM10_ne
	mov	a,MM
	swap	a
	anl	a,#00001111b
	mov	TohleNaDisp,a
MM10_ne:	cjne	a,#104,HH1_ne
	mov	a,HH
	anl	a,#00001111b
	mov	TohleNaDisp,a
HH1_ne:	cjne	a,#105,HH10_ne
	mov	a,HH
	swap	a
	anl	a,#00001111b
	jnz	Nepotlac0
	mov	a,#11
Nepotlac0:	mov	TohleNaDisp,a
	jmp	HH10_ne
	;vyber v tomto cyklu zobrazovane cifry pro alarm
ZobrazAl:	mov	a,Cifra123456	
	cjne	a,#100,SS1al_ne
	mov	TohleNaDisp,#13
SS1al_ne:	cjne	a,#101,SS10al_ne
	mov	TohleNaDisp,#12
SS10al_ne:	cjne	a,#102,MM1al_ne
	mov	a,MMal
	anl	a,#00001111b
	mov	TohleNaDisp,a
MM1al_ne:	cjne	a,#103,MM10al_ne
	mov	a,MMal
	swap	a
	anl	a,#00001111b
	mov	TohleNaDisp,a
MM10al_ne:	cjne	a,#104,HH1al_ne
	mov	a,HHal
	anl	a,#00001111b
	mov	TohleNaDisp,a
HH1al_ne:	cjne	a,#105,HH10_ne
	mov	a,HHal
	swap	a
	anl	a,#00001111b
	jnz	Nepotlac0al
	mov	a,#11
Nepotlac0al:	mov	TohleNaDisp,a
	;nastaveni prislusne spolecne elektrody
HH10_ne:	mov	a,#0
	jnb	Katody,Anody	
	mov	a,#6
Anody:	add	a,Cifra123456
	clr	c
	subb	a,#100
	mov	dptr,#SelEl
	movc	a,@a+dptr
	mov	c,Pipani
	cpl	c
	mov	acc.1,c
	mov	P1,a
	;vyslani prislusneho znaku na posuvny registr
	mov	dptr,#CharMap
	mov	a,TohleNaDisp
	movc	a,@a+dptr
	jnb	Katody,Neneguj
	cpl	a
NeNeguj:	clr	ti
	mov	sbuf,a
	jnb	ti,$
	;tu se dela blikani displeje
	jnb	Blikani,JoBuff
	mov	a,R1
	jb	acc.1,NeBuff
JoBuff:	setb	BUFF
	clr	BUFF
	;nastaveni pocitadla pro vyber cifry
Nebuff:	inc	Cifra123456
	mov	a,Cifra123456
	cjne	a,#106,KonecMPX
	mov	Cifra123456,#100
KonecMPX:	setb	Pipak
	reti

CharMap:	db 	00000011b	;0
	db 	10011111b	;1
	db 	00100101b	;2
	db 	00001101b	;3
	db 	10011001b	;4
	db 	01001001b	;5
	db 	01000001b	;6
	db 	00011111b	;7
	db 	00000001b	;8
	db 	00001001b	;9	
	db	11111111b	;nic
	db	11111111b	;nic
	db	00010001b	;A
	db	11100011b	;L
	
SelEl:	db	11111011b	;SS1
	db	11110111b	;SS10
	db	11101111b	;MM1
	db	11011111b	;MM10
	db	10111111b	;HH1
	db	01111111b	;HH10
	db	00000111b	;totez pro NPN tranzistory	
	db	00001011b
	db	00010011b
	db	00100011b
	db	01000011b
	db	10000011b
	end