;	name	start
	org	0


;****************************************
;*					*
;*    Nastaveni konstant, promennych	*
;*					*
;****************************************
lcd:
	
bajt1	equ	50H
bajt2	equ	51H
bajt3	equ	52H
bajt4	equ	53H
bajt5	equ	54H
bajt6	equ	55H
bajt7	equ	56H
bajt8	equ	57H

teplota	equ	62H
des	equ	63H
jedn	equ	64H

dalas 	bit	P1.0	;teplotni cidlo pripojene na PORT P1.0

disp	equ	P4
podsv	bit	P4.7
RS	bit	P4.5
E	bit	P4.4
D4	bit	P4.0
D5	bit	P4.1
D6	bit	P4.2
D7	bit	P4.3
NC	bit	P4.6
LCD	equ	20H

start:
	call	prodl1			;prodleva 15ms a vice
	mov	LCD,#00110000b		;8-bitova komunikace
	call	pos8bI			;posli horni bity instrukce
	call	prodl2			;prodleva 4,1ms a vice
	mov	LCD,#00110000b		;8-bitova komunikace
	call	pos8bI			;posli horni bity instrukce
	call	prodl3			;prodleva 100us a vice
	mov	LCD,#00110000b		;8-bitova komunikace
	call	pos8bI			;posli horni bity instrukce
	call	prodl4			;prodleva 40us a vice			
	mov	LCD,#00100000b		;4bitova komunikace
	call	pos8bI			;posli horni bity instrukce
	call	prodl4			;prodleva 40us a vice
	mov	LCD,#00101000b		;dva radky
	call	pos8bI			;posli instrukci
	call	smazdisp		;smaz displej
	mov	LCD,#00001100b		;zapni displej
	call	pos8bI			;posli instrukci
	mov	LCD,#00000110b		;inkrementace
	call	pos8bI			;posli instrukci
	clr	podsv

;--------------------------------------------------------------------konec definic

;************************************************
;*						*
;*		hlavni program			*
;*						*
;************************************************
hlavni:
	call	reset
	mov	A,#033H
	call	zapis

	call	cteni
	mov	bajt1,A
	call	cteni
	mov	bajt2,A
	call	cteni
	mov	bajt3,A
	call	cteni
	mov	bajt4,A
	call	cteni
	mov	bajt5,A
	call	cteni
	mov	bajt6,A
	call	cteni
	mov	bajt7,A
	call	cteni
	mov	bajt8,A
	
	
	mov	LCD,#80H
	call	pos8bI
	
	mov	R6,#8
	mov	R1,#50H
tocto:	
;-----bajt----	
	mov	A,@R1
	swap	A
	anl	A,#0FH
	add	A,#30H
	cjne	A,#3AH,loc_1
loc_1:	jc	loc_2
	add	A,#07H
loc_2:
	mov	LCD,A
	call	pos8bD
	mov	A,@R1
	anl	A,#0FH
	add	A,#30H
	cjne	A,#3AH,loc_3
loc_3:	jc	loc_4
	add	A,#07H
loc_4:
	mov	LCD,A
	call	pos8bD
	inc	R1

	djnz	R6,tocto
	ljmp	konec

;****************************************
;*					*
;*		podprogramy		*
;*					*
;****************************************
;_____________________________________reset pulz
reset:
	clr	dalas
	;----------------prodleva 480us
	mov	R7,#255
	djnz	R7,$
	mov	R7,#129
	djnz	R7,$
	;----------------konec prodlevy
	setb	dalas
	;----------------prodleva 70us
	mov	R7,#56
	djnz	R7,$
	;----------------konec prodlevy
	
	jnb	dalas,ano
	jmp	reset
ano:
	;----------------prodleva 410us
	mov	R7,#255
	djnz	R7,$
	mov	R7,#73
	djnz	R7,$
	;----------------konec prodlevy
ret

;_______________________________________zapis
zapis:	mov	R6,#008H	;8x budu rotovat		

sniz:	rrc	A
	jnc	je
	
	mov	R7,#5	;prodleva 6us
	clr	dalas
	djnz	R7,$
	setb	dalas
	mov	R7,#52	;prodleva 64us
	djnz	R7,$
	jmp 	jdidal
	
je:
	clr	dalas
	mov	R7,#48	;prodleva 60us
	djnz	R7,$	
	setb	dalas
	mov	R7,#8	;prodleva 10us
	djnz	R7,$
	sjmp	jdidal

jdidal:
	djnz	R6,sniz
	
	;----------------300us prodleva
	mov	R7,#240
	djnz	R7,$
	;----------------konec prodlevy
ret

;_____________________________________cteni
cteni:	mov	R6,#008H
	
opakuj1:
	clr	dalas
	;--------------prodleva	6us
	mov	R7,#5
	djnz	R7,$
	;--------------konec prodlevy
	setb	dalas
	;--------------prodleva 10us
	mov	R7,#8
	djnz	R7,$
	;--------------konec prodlevy
	mov	c,dalas
	rrc	A
	;--------------prodleva	60us
	mov	R7,#48
	djnz	R7,$
	djnz	R6,opakuj1
	;--------------prodleva	300us
	mov	R7,#240
	djnz	R7,$
	;--------------konec prodlevy
ret

;______________________________________LCD display
smazdisp:
	mov 	LCD,#00000001b
	call	pos8bI
	call	prodl5
	ret


pos8bI:
	clr	RS		;podprogram posle instrukci
	call	pos8b
	ret

pos8bD:				;podprogram posle data
	setb	RS
	call	pos8b
	ret

pos8b:				;data i instrukce se musi poslat nadvakrat
	push	acc
	mov	A,LCD
	swap	A
	call	posliSpol
	mov	A,LCD
	call	posliSpol
	call	prodl4
	pop	acc
	ret


posliSpol:			;nejdriv se posle horní 4bity a pak spodní 4bity
	anl	A,#00001111b	;pri posilání 4bitu na port se ostatni bity portu nesmi zmenit
	mov	B,A
	mov	A,disp
	anl	A,#11110000b
	add	A,B
	mov	disp,A
	setb	E
	nop
	nop
	clr	E
	ret

posRet:				;posle textovy retezec , ktery je ulozen v DPTR	
	mov	A,#00H
	movc	A,@A+DPTR
	cjne	A,#0D0H,vypis
		
	ret
vypis:	mov	LCD,A
	inc	DPTR
	call	pos8bD
	sjmp	posRet




prodl1:
	mov	R7,#0FFh
	mov	R6,#60
	djnz	R7,$
	djnz	R6,$-2
	ret

prodl2:
	mov	R7,#0FFh
	mov	R6,#17
	djnz	R7,$
	djnz	R6,$-2
	ret

prodl3:
	mov	R7,#100
	djnz	R7,$
	ret

prodl4:
	mov	R7,#40
	djnz	R7,$
	ret

prodl5:
	mov	R7,#0FFh
	mov	R6,#7
	djnz	R7,$
	djnz	R6,$-2
	ret
konec:
	jmp	konec
	nop
	end
	
