;	name	start
	org	0
;********************************************************************************************************
;*  pro zpravnou funkcnost musite mit zapsane adresy jednotlivych dallasu v DPTR			*
;*  ktere najdete na konci programu. 									*
;*  Takze pomoci meho programu ADRESA.ASM si vygenerujete "64-bitove kody" primo v HEX na LCD display	*
;*  a temi pak prepisete moje										*
;*  program je napsan pro 3 cidla, ale predelat by to mohl kdokoli pro XY cidel. 			*
;*  preju hodne uspechu											*
;*  PS: vsem se omlouvam za popisky, ale usnadnuji mi praci, a vam snad take				*
;********************************************************************************************************

;****************************************
;*					*
;*    Nastaveni konstant, promennych	*
;*					*
;****************************************
lcd:
	
bajt1	equ	60H
bajt2	equ	61H
teplota	equ	62H
des	equ	63H
jedn	equ	64H

dalas 	bit	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
	

	mov	DPTR,#text1
	call	posRet
	mov	LCD,#0C0H
	call	pos8bI
	mov	DPTR,#text2
	call	posRet

;--------------------------------------------------------------------konec definic

;****************************************************************
;*								*
;*			hlavni program				*
;*								*
;****************************************************************
;nastaveni citlivosti mereni
	call	reset
	mov	A,#0ccH
	call 	zapis
	mov	A,#04eH
	call	zapis
	mov	A,#0ffH
	call 	zapis
	mov	A,#0ffH
	call 	zapis
	mov	A,#01fH
	call	zapis
;----------------------------------
	mov	R3,#0		;kvuli adresam
merim:		
	call	reset
	mov	A,#0ccH
	call	zapis	
	mov	A,#044H
	call 	zapis
	mov	R7,#255
	mov	R6,#255
	djnz	R7,$
	djnz	R6,$-2
	call	reset
	mov	A,#055H
	call	zapis
	call	adresa
	mov	A,#0beH
	call	zapis
	call	cteni
	mov	bajt1,A
	call	cteni
	mov	bajt2,A

	mov	R7,#4
	mov	R6,#4
		
;------------------------konec mereni	
prevod:
	mov	A,bajt1
rotp:	rrc	A
	djnz	R7,rotp
	anl	A,#00001111b
	mov	B,A
	mov	A,bajt2
rotl:	rl	A
	djnz	R6,rotl
	anl	A,#01110000b
	orl	A,B
	mov	teplota,A
	
	mov	A,teplota
	mov	B,#10
	div	AB
	add	A,#30H
	mov	des,A
	mov	A,B
	add	A,#30H
	mov	jedn,A
	
;----kontrola rovnosti 0,1,2, kvuli 3 dallasum	
	mov	A,R3
	subb	A,#0
	jz	dalik1	;je roven 0
	
	mov	A,R3
	subb	A,#1
	jz	dalik2	;je roven 1
	jnz	dalik3	;je roven 2
;----konec kontroly

;===========zobrazeni na display, dle adresy========	
dalik1:
	mov	LCD,#83H
	call	pos8bI
	inc	R3
	jmp	jdi
dalik2:	
	mov	LCD,#88H
	call	pos8bI
	inc	R3
	jmp	jdi
dalik3:	
	mov	LCD,#8DH
	call	pos8bI
	mov	R3,#0
	
jdi:	mov	LCD,des
	call	pos8bD
	mov	LCD,jedn
	call	pos8bD

	ljmp	merim

;********************************************************
;*							*
;*			podprogramy			*
;*							*
;********************************************************

;++++++++++++++++++++++++++++++++++++++++++++++++++
;_____________________________________adresovani
adresa:
	;----kontrola rovnosti 0,1,2 kvuli 3 dallasum	
	mov	A,R3
	subb	A,#0
	jz	dal1	;je roven 0
	
	mov	A,R3
	subb	A,#1
	jz	dal2	;je roven 1
	jnz	dal3	;je roven 2
	;-----------------konec kontroly

	ljmp	merim

   ;---------adresa dallasu 1-----------	
dal1:	mov	dptr,#dalas1
	clr	A
	
   	;----kvuli 8 bajtum	
	mov	R1,#8
	
		
skok1:	movc	A,@a+dptr
	call	zapis
	inc	DPTR
	clr	A
	djnz	R1,skok1
	ret
	
   ;---------adresa dallasu 2-----------
dal2:	mov	dptr,#dalas2
	clr	A
	
   	;----8x se provede zapis----	
	mov	R1,#8
		
skok2:	movc	A,@a+dptr
	call	zapis
	inc	DPTR
	clr	A
	djnz	R1,skok2
	;inc	R3		;zde je R1 v 1 a bude zvyseno na 2
	ret
	
   ;---------adresa dallasu 3------------	
dal3:	mov	dptr,#dalas3
	clr	A
	
	;----8x se provede zapis----	
	mov	R1,#8
		
skok3:	movc	A,@a+dptr
	call	zapis
	inc	DPTR
	clr	A
	djnz	R1,skok3
	ret

;++++++++++++++++++++++++++++++++++++++++++++++++	
;_____________________________________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


text1:	db	'D1:  D2:  D3:',0D0H
text2:	db	'S1:  S2:  S3:',0D0H

;===================adresy jednotlivych dallasu====================
dalas1:	db	028H, 098H, 071H, 0B4H, 001H, 000H, 000H, 09AH
dalas2:	db	028H, 02AH, 077H, 0B4H, 001H, 000H, 000H, 06FH
dalas3:	db	028H, 0A2H, 05BH, 0B4H, 001H, 000H, 000H, 0FDH

	nop
	end
	
