	DSEG AT 30h	
	F1_1:		DS 1
	F1_2:		DS 1
	F2_1:		DS 1
	F2_2:		DS 1
	F3_1:		DS 1
	F3_2:		DS 1
	F4_1:		DS 1
	F4_2:		DS 1
	P_L:		DS 1
	P_H:		DS 1

	BSEG AT 20h
	poslednibyte:	DBIT 1
	ZNAM:		DBIT 1
	RS		BIT 	P1.6		;bit RS na portu 1.0
	E		BIT	P1.7
	DB4		BIT	P3.5
	DB5		BIT	P1.1
	DB6		BIT	P1.2
	DB7		BIT	P1.3
	;DATASHT 	BIT	P1.0
	;SCK		BIT	P3.4
	SDA	 	BIT	P1.0
	SCL		BIT	P3.4
	XCLR		BIT	P3.3
	CSEG AT 00h
	CEKANI	EQU 5	;zhruba 5 us


; ***	MASTER PROGRAM	****************
	org	0000h
	mov	SP,#08h		; ukazatel vrcholu zasobniku
ZAC:	clr	XCLR
	setb	XCLR
	call 	Cek770ms
	call 	INICIAL	
	
	call	START
	mov 	A,#0EEh		;Adresa modulu
	call 	ZAPISSMD
	mov 	A,#0F4h		;Adresa kontr.registru
	call 	ZAPISSMD
	mov 	A,#0F0h		;Adresa modulu
	call 	ZAPISSMD
	call 	STOP

	call	Cek35ms

	call	START
	mov 	A,#0EEh		;Adresa modulu
	call 	ZAPISSMD
	mov 	A,#0F6h		;Adresa modulu
	call 	ZAPISSMD
	call	START
	mov 	A,#0EFh		;Adresa modulu
	call 	ZAPISSMD
	
	clr 	poslednibyte	;Cteni 2B 
	call 	POSLECHSMD
	mov	P_H,A
	call	PREVODB	
	setb 	poslednibyte
	call 	POSLECHSMD
	mov	P_L,A
	call	PREVODB	
	call	STOP
	ljmp 	ZAC
; ***	SUBRUTINY SMD500 *******************
;START-Sestupna hrana SDA pri SCL=1
START:	setb 	SCL	;neaktivní stav
	setb 	SDA
	mov 	B,#CEKANI
	djnz 	B,$
	clr	SDA	;start
	mov 	B,#CEKANI
	djnz 	B,$
	clr	SCL
	mov 	B,#CEKANI
	djnz 	B,$
	setb	SDA
	ret
	
STOP:	MOV B,#CEKANI		;stop
	DJNZ B,$
	CLR SDA
	MOV B,#CEKANI
	DJNZ B,$
	SETB SCL
	MOV B,#CEKANI
	DJNZ B,$
	SETB SDA
	ret
;Zapis do SMD500

ZAPISSMD:
	mov 	R7,#8	
DALSBIT:rlc 	A		;rotuj bit do C
	mov	SDA,C		;vystav bit na SDA
	mov 	B,#CEKANI
	djnz 	B,$
	setb 	SCL		;SCL=1 Hodiny
	mov 	B,#CEKANI	;5us
	djnz 	B,$
	clr 	SCL		;SCL=0
	mov 	B,#CEKANI
	djnz 	B,$
	djnz 	R7,DALSBIT	;další bit?
	mov 	B,#CEKANI
	djnz 	B,$
	setb 	SDA		;uvolneni sbernice pro ACK
	mov 	B,#CEKANI
	djnz 	B,$

	setb 	SCL		;ACK
	mov 	B,#CEKANI	;5us
	djnz 	B,$
	jb	SDA,$
	clr 	SCL		;čtení ACK
	RET

POSLECHSMD:
	mov 	R7,#8		
MSB:	mov 	C,SDA
	rlc 	A
	setb 	SCL
	mov 	B,#CEKANI	;5us
	djnz 	B,$
	clr 	SCL
	mov 	B,#CEKANI	;5us
	djnz 	B,$	
	djnz 	R7,MSB
	setb	SDA
	jb	poslednibyte,JEPOSL
	mov 	B,#CEKANI	;5us
	djnz 	B,$
	clr	SDA	;ACK
JEPOSL:	mov 	B,#CEKANI	;5us
	djnz 	B,$
	setb	SCL
	mov 	B,#CEKANI	;5us
	djnz 	B,$
	clr	SCL
	mov 	B,#CEKANI	;5us
	djnz 	B,$
	setb	SDA
	ret
vysliACK1:
	push 	ACC
	mov 	A,#65
	call 	ZData
	mov 	A,#49
	call 	ZData
	pop 	ACC
	RET
vysliACK2:
	push 	ACC
	mov 	A,#50
	call 	ZData
	pop 	ACC
	RET
vysliACK3:
	push 	ACC
	mov 	A,#51
	call 	ZData
	pop 	ACC
	RET

	
;Vypis BYTE na displej
PREVODB:push	ACC
	mov 	R4,#8
B1:	call 	PRE
	RL 	A
	DJNZ 	R4,B1
	pop	ACC
	ret	
;Test 8.bitu ACC
PRE:	PUSH 	acc
	jb 	ACC.7,POSLI11
	mov 	A,#00110000b
	call 	ZData
	JMP	SK
POSLI11:call 	POSLI1
SK:	pop 	ACC
	ret
	
POSLI1:	push 	acc
	mov 	A,#00110001b
	call 	ZData
	pop 	acc
	ret
; ***	SUBRUTINY LCD *********************
INICIAL:push acc
	call Cek15ms ;Inicializace LCD displeje
        anl P1,#0F0h
        orl P1,#3h
        call Imp
        call Cek4ms
	call Cek400us
        call Imp
        call Cek100us
        call Imp
        call Cek40us
        anl P1,#0F0h
        orl P1,#2h
        call Imp
        call Cek40us
        mov A,#28h ;4bity,2l,5x8
        call PData
        mov A,#8h ;Disp off
        call PData
        call SmazD ;Smazat displej
        mov A,#6h ;autoinkr,shift ne
        call PData
        mov A,#0ch ;zapnout disp,blik,kurs-off
        call PData ;Konec inicializace LCD displeje
	pop acc
	ret
Imp: 	setb E
     	clr E
     	ret 
SmazD:	push acc
	mov A,#01h
	call PData
	call Cek2ms
	pop acc
	ret	
PData:	push acc
	push psw
	clr RS 
	jnb rs,PData1
ZData:	push acc
	push psw
	setb RS
PData1:	mov C,ACC.4
	mov DB4,C
	mov C,ACC.5
	mov DB5,C
	mov C,ACC.6
	mov DB6,C
	mov C,ACC.7
	mov DB7,C
	call Imp
	mov C,ACC.0
	mov DB4,C
	mov C,ACC.1
	mov DB5,C
	mov C,ACC.2
	mov DB6,C
	mov C,ACC.3
	mov DB7,C
	call Imp
	call Cek40us
	pop psw
	pop acc
	ret

Cek400us:     
	push    acc             ; 2 uS
	mov     a,#200
        djnz    acc,$           ; 2 uS ,200 * 2 = 400
	JMP konec_us
Cek100us:     
	push    acc             ; 2 uS
	mov     a,#50
        djnz    acc,$           ; 2 uS ,50 * 2 = 100
	JMP konec_us
Cek40us:     
	push    acc             ; 2 uS
        mov     a,#15           ; 2 uS
        djnz    acc,$           ; 2 uS ,15 * 2 = 30
konec_us:
        pop     acc             ; 2 uS
        ret                     ; 2 uS	
;  Zpozdeni v ms

Cek35ms: push    acc             ; 2 uS
         push    b               ; 2 uS
 	 mov     a,#35		 ;
	 JMP	 SEM
Cek15ms: push    acc             ; 2 uS
         push    b               ; 2 uS
 	 mov     a,#15		 ;
	 JMP	 SEM
Cek11ms: push    acc             ; 2 uS
         push    b               ; 2 uS
 	 mov     a,#11		 ;
	 JMP	 SEM
Cek4ms:  push 	 acc
	 push    b
	 mov	 a,#4
	 JMP	 SEM
Cek2ms:  push 	 acc
	 push    b
	 mov	 a,#2
SEM:     mov     b,#241          ; 2 uS
dm:      djnz    b,$             ; 2 uS ,248 * 2 = 496
         mov     b,#249          ; 2 uS
         djnz    b,$             ; 2 uS ,249 * 2 = 498
         mov     b,#248          ; 2 uS
         djnz    acc,dm          ; 2 uS
	 pop     b               ; 2 uS
         pop     acc             ; 2 uS
         ret                     ; 2 uS

; ***	SUBROUTINES UNIVERSAL		****************
; ----	Wait 770ms ----------------
Cek770ms:
	push	06h
	push	07h
	mov	R7,#10d		
	mov	R6,#200d
Cek770ms1:
	call	Cek300us
	djnz	R6,Cek770ms1
	djnz	R7,Cek770ms1
	pop	07h
	pop	06h
	ret
; ----	Wait 300us ----------------
Cek300us:
	push	07h
	nop
	mov	R7,#148d
	djnz	R7,$
	pop	07h
	ret

	end
