		INCLUDE		"P16F73.INC"
		LIST 		P=16F73, R=DEC
		
    __CONFIG _CP_OFF & _PWRTE_ON & _WDT_ON & _RC_OSC

;--------------------------------------- proměnné

PA		EQU		05h		;porty A
PB		EQU		06h		;porty B
PC		EQU		07h		;porty C
PD		EQU		08h		;porty D
PE		EQU		09h		;porty E
c1		EQU		40h		;pomocná proměnná pro čekání
c2		EQU		41h		;pomocná proměnná pro čekání
c3		EQU		42h		;pomocná proměnná pro cykly
posunu	EQU		43h		;počet posunů RRF/RLF v jednom cyklu
W_temp	EQU		44h		;dočasné uložení W při přerušení
S_temp	EQU		45h		;dočasné uložení STATUS při přerušení
cas3	EQU		46h		;
c4		EQU		47h		;pomocná proměnná pro cyklus
temp	EQU		48h		;
temp2	EQU		49h		;
cas1	EQU		4Ah		;
cas2	EQU		4Bh		;
cislo	EQU		4Ch		;
amp		EQU		4Dh		;
U_1		EQU		4Eh		;
U_2		EQU		4Fh		;


						;zapojení I2C
SCL		EQU		3
SDA		EQU		4

;-----------------------------------------------------------------------------------
;NASTAVENÍ MIKROKONTROLÉRU
;-----------------------------------------------------------------------------------

;--------------------------------------- org

		ORG			00h
		GOTO		zacatek


zacatek

;--------------------------------------- vstupy, výstupy

		BCF			STATUS,RP1
		BSF			STATUS,RP0

		MOVLW		b'00000011'
		MOVWF		TRISA

		MOVLW		b'00000000'
		MOVWF		TRISB

		MOVLW		b'00000000'		; SDA a SCL
		MOVWF		TRISC

		BCF			STATUS,RP0




		BSF			PC,SCL
		BSF			PC,SDA
		
		MOVLW		b'11111111'
		MOVWF		PB				; "nulování" portů PB displeje

		BCF			PA,2
		BCF			PA,5

		BSF			PC,6
		CALL		cekani1
		BCF			PC,6



;-----------------------------------------------------------------------------------

start_mereni

		CALL		TMP_I2C_cti

		RLF			temp		;korekce o 1 bit vlevo

		DECF		temp		;korekce -2st.

		BCF			temp,0
		BTFSC		temp2,7
		BSF			temp,0

;-------------------------------

		BTFSC		temp,7
		GOTO		vetrak_100
		BTFSC		temp,6
		GOTO		vetrak_100
		BTFSC		temp,5
		GOTO		vetrak_001xxxxx
		GOTO		vetrak_000xxxxx

;-------

vetrak_001xxxxx
		BTFSC		temp,4
		GOTO		vetrak_0011xxxx
		GOTO		vetrak_0010xxxx
vetrak_000xxxxx
		BTFSC		temp,4
		GOTO		vetrak_0001xxxx
		GOTO		vetrak_0000xxxx

;-------

vetrak_0011xxxx
		BTFSC		temp,3
		GOTO		vetrak_00111xxx
		GOTO		vetrak_00110xxx
vetrak_0010xxxx
		BTFSC		temp,3
		GOTO		vetrak_00101xxx
		GOTO		vetrak_00100xxx
vetrak_0001xxxx
		GOTO		vetrak_0
vetrak_0000xxxx
		GOTO		vetrak_0

;-------

vetrak_00111xxx
		BTFSC		temp,2
		GOTO		vetrak_80
		GOTO		vetrak_70
vetrak_00110xxx
		BTFSC		temp,2
		GOTO		vetrak_60
		GOTO		vetrak_50
vetrak_00101xxx
		BTFSC		temp,2
		GOTO		vetrak_40
		GOTO		vetrak_30
vetrak_00100xxx
		BTFSC		temp,2
		GOTO		vetrak_20
		GOTO		vetrak_10

;-------------------------------

vetrak_0
		MOVLW		1
		MOVWF		cas1
		MOVLW		99
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_10
		MOVLW		10
		MOVWF		cas1
		MOVLW		90
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_20
		MOVLW		20
		MOVWF		cas1
		MOVLW		80
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_30
		MOVLW		30
		MOVWF		cas1
		MOVLW		70
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_40
		MOVLW		40
		MOVWF		cas1
		MOVLW		60
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_50
		MOVLW		50
		MOVWF		cas1
		MOVLW		50
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_60
		MOVLW		60
		MOVWF		cas1
		MOVLW		40
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_70
		MOVLW		70
		MOVWF		cas1
		MOVLW		30
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_80
		MOVLW		80
		MOVWF		cas1
		MOVLW		20
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_90
		MOVLW		90
		MOVWF		cas1
		MOVLW		10
		MOVWF		cas2
		GOTO		vetrak_konec
vetrak_100
		MOVLW		99
		MOVWF		cas1
		MOVLW		1
		MOVWF		cas2
		GOTO		vetrak_konec


vetrak_konec

;-----------------------------------------------------------------------------------

start1
		DECFSZ		cas1
		GOTO		start1_
		GOTO		start2
start1_
		BSF			PC,6
		CALL		zobrazeni
		GOTO		start1

start2
		BCF			PC,6
		CALL		zobrazeni
		DECFSZ		cas2
		GOTO		start2

		GOTO		start_mereni
		


;-----------------------------------------------------------------------------------
; zobrazení na displej
;-----------------------------------------------------------------------------------

zobrazeni
		CLRF		U_2
		CLRF		U_1

		MOVF		temp,w			; ..............
		MOVWF		cislo
		CALL		vysledek

		MOVF		U_2,w
		CALL		zobraz_B
		MOVWF		PB
		BSF			PA,2
		CALL		cekani3			; zobrazuje 1. segmentovka
		BCF			PA,2

		MOVF		U_1,w
		CALL		zobraz_B
		MOVWF		PB
		BSF			PA,5
		CALL		cekani3			; zobrazuje 2. segmentovka
		BCF			PA,5
		
		MOVLW		b'11111111'
		MOVWF		PB

		RETURN


;-----------------------------------------------------------------------------------
; I2C TMP100
;-----------------------------------------------------------------------------------

TMP_I2C_cti



;-----------------------------------
		BSF			PC,SCL
		BSF			PC,SDA
		CALL		cekani2
		
		BCF			PC,SDA				;START přenosu
		NOP
		NOP
		NOP
		BCF			PC,SCL
		NOP
		NOP
		NOP
;-----------------------------------


		BSF			PC,SDA				;adresa 1
		CALL		I2C_clock		;1
		BCF			PC,SDA				;adresa 0
		CALL		I2C_clock		;2
		BCF			PC,SDA				;adresa 0
		CALL		I2C_clock		;3
		BSF			PC,SDA				;adresa 1	
		CALL		I2C_clock		;4
		BCF			PC,SDA				;adresa 0	
		CALL		I2C_clock		;5
		BSF			PC,SDA				;adresa 1	
		CALL		I2C_clock		;6
		BCF			PC,SDA				;adresa 0	
		CALL		I2C_clock		;7
		BCF			PC,SDA				;R/W_   0	write
		CALL		I2C_clock		;8

;-----------------------------------
		BSF			STATUS,RP0
		BSF			TRISC,SDA
		BCF			STATUS,RP0

		CALL		I2C_clock		;9
ack0	BTFSS		PC,SDA				;čeká na ACK od TMP100
		GOTO		ack0
		CALL		cekani2

		BSF			STATUS,RP0
		BCF			TRISC,SDA
		BCF			STATUS,RP0
;-----------------------------------

		BCF			PC,SDA				; 0
		CALL		I2C_clock		;1
		BCF			PC,SDA				; 0
		CALL		I2C_clock		;2
		BCF			PC,SDA				; 0
		CALL		I2C_clock		;3
		BCF			PC,SDA				; 0
		CALL		I2C_clock		;4
		BCF			PC,SDA				; 0	
		CALL		I2C_clock		;5
		BCF			PC,SDA				; 0	
		CALL		I2C_clock		;6
		BCF			PC,SDA				;P1 0	
		CALL		I2C_clock		;7
		BCF			PC,SDA				;P0 0	
		CALL		I2C_clock		;8

;-----------------------------------
		BSF			STATUS,RP0
		BSF			TRISC,SDA
		BCF			STATUS,RP0
		
		CALL		I2C_clock		;9
ack1	BTFSS		PC,SDA				;čeká na ACK od TMP100
		GOTO		ack1
		CALL		cekani2

		BSF			STATUS,RP0
		BCF			TRISC,SDA
		BCF			STATUS,RP0
;-----------------------------------


;-----------------------------------
		BSF			PC,SCL
		NOP
		NOP
		NOP
		BSF			PC,SDA
		CALL		cekani2
		
		BCF			PC,SDA				;START přenosu opakovaný
		NOP
		NOP
		NOP
		BCF			PC,SCL
		NOP
		NOP
		NOP
;-----------------------------------

		BSF			PC,SDA				;adresa 1
		CALL		I2C_clock
		BCF			PC,SDA				;adresa 0
		CALL		I2C_clock
		BCF			PC,SDA				;adresa 0
		CALL		I2C_clock
		BSF			PC,SDA				;adresa 1	
		CALL		I2C_clock
		BCF			PC,SDA				;adresa 0	
		CALL		I2C_clock
		BSF			PC,SDA				;adresa 1	
		CALL		I2C_clock
		BCF			PC,SDA				;adresa 0	
		CALL		I2C_clock
		BSF			PC,SDA				;R/W_   1	READ
		CALL		I2C_clock

;-----------------------------------
;		BSF			STATUS,RP0
;		BSF			TRISC,SDA
;		BCF			STATUS,RP0
;		
;		CALL		I2C_clock
;ack2	BTFSS		PC,SDA				;čeká na ACK od TMP100
;		GOTO		ack2
;		CALL		cekani3
;
;		BSF			STATUS,RP0
;		BCF			TRISC,SDA
;		BCF			STATUS,RP0
;-----------------------------------

		CALL		cekani2

;		BCF			PB,7				;desetinná tečka
;		BSF			PA,2



;-----------------------------------
		BSF			STATUS,RP0
		BSF			TRISC,SDA			;SDA IN
		BCF			STATUS,RP0
		CALL		cekani2
;-----------------------------------
; PŘÍJEM:
;-----------------------------------

		CLRF		temp

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,7
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,6
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,5
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,4
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,3
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,2
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,1
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp,0
		NOP
		BCF			PC,SCL



;		CALL		cekani2
;-----------------------------------
;		BSF			STATUS,RP0
;		BCF			TRISC,SDA			;SDA OUT
;		BCF			STATUS,RP0
;-----------------------------------
		
;		BSF			PC,SCL
;		NOP
;		BCF			PC,SCL
;		CALL		cekani2
;		BSF			PC,SDA				;vyšle ACK
;		NOP
;		BCF			PC,SDA
;		NOP

;-----------------------------------
;		BSF			STATUS,RP0
;		BSF			TRISC,SDA			;SDA IN
;		BCF			STATUS,RP0
;-----------------------------------
; PŘÍJEM:
;-----------------------------------

		CALL		cekani2

		CLRF		temp2

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,7
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,6
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,5
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,4
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,3
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,2
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,1
		NOP
		BCF			PC,SCL

		NOP
		NOP
		BSF			PC,SCL
		CALL		cekani2
		BTFSC		PC,SDA
		BSF			temp2,0
		NOP
		BCF			PC,SCL


;-----------------------------------
		BSF			STATUS,RP0
		BCF			TRISC,SDA			;SDA OUT
		BCF			STATUS,RP0
;-----------------------------------
		BSF			PC,SCL
		NOP
		BCF			PC,SCL
		NOP
		BSF			PC,SDA				;vyšle ACK
		NOP
		BCF			PC,SDA
		NOP
;-----------------------------------


		NOP
		NOP
		NOP


;-----------------------------------
		BSF			PC,SCL				;STOP přenosu
		NOP
		NOP
		BSF			PC,SDA
;-----------------------------------




		RETURN
		
	
	
	
	

I2C_clock
		NOP
		BSF			PC,SCL
		NOP
		CLRWDT
		NOP
		BCF			PC,SCL
		NOP
		RETURN

;-----------------------------------------------------------------------------------
; PREVOD BINAR-DEKADICKE
;-----------------------------------------------------------------------------------



;-------------------------------------------------

vysledek
		CLRWDT

		DECFSZ		cislo
		GOTO		incf_U_1
		RETURN

;-----

incf_U_1
		INCF		U_1

test_U_1							; kontrola přetečení jednotek
		BTFSC		U_1,1
		BTFSS		U_1,3
		GOTO		test_U_2
		INCF		U_2
		CLRF		U_1

test_U_2							; kontrola přetečení desítek
		BTFSC		U_2,1
		BTFSS		U_2,3
		GOTO		vysledek

		MOVLW		15				; při celkovém přetečení uloží do U_1 až 4 otazníky
		MOVWF		U_1
		MOVWF		U_2
		RETURN



;-----------------------------------------------------------------------------------
; KÓDOVÁNÍ BCD-SEGMENTY
;-----------------------------------------------------------------------------------
zobraz_C
		MOVWF		amp
		
C_xxxx	BTFSC		amp,0			;převod amp na 1 z 16.
		GOTO		C_xxx1
		GOTO		C_xxx0


C_xxx0	BTFSC		amp,1
		GOTO		C_xx10
		GOTO		C_xx00

C_xxx1	BTFSC		amp,1
		GOTO		C_xx11
		GOTO		C_xx01


C_xx00	BTFSC		amp,2
		GOTO		C_x100
		GOTO		C_x000

C_xx01	BTFSC		amp,2
		GOTO		C_x101
		GOTO		C_x001

C_xx10	BTFSC		amp,2
		GOTO		C_x110
		GOTO		C_x010

C_xx11	BTFSC		amp,2
		GOTO		C_x111
		GOTO		C_x011


C_x000	BTFSC		amp,3
		GOTO		C_1000
		GOTO		C_0000

C_x001	BTFSC		amp,3
		GOTO		C_1001
		GOTO		C_0001

C_x010	BTFSC		amp,3
		GOTO		C_1010
		GOTO		C_0010

C_x011	BTFSC		amp,3
		GOTO		C_1011
		GOTO		C_0011

C_x100	BTFSC		amp,3
		GOTO		C_1100
		GOTO		C_0100

C_x101	BTFSC		amp,3
		GOTO		C_1101
		GOTO		C_0101

C_x110	BTFSC		amp,3
		GOTO		C_1110
		GOTO		C_0110

C_x111	BTFSC		amp,3
		GOTO		C_1111
		GOTO		C_0111	

;-----------------------------------------------------------------------------------
		

		
C_0000	MOVLW		b'00010001'
		RETURN
C_0001	MOVLW		b'10111101'
		RETURN
C_0010	MOVLW		b'01011000'
		RETURN
C_0011	MOVLW		b'00111000'
		RETURN
C_0100	MOVLW		b'10110110'
		RETURN
C_0101	MOVLW		b'00110010'
		RETURN
C_0110	MOVLW		b'00010010'
		RETURN
C_0111	MOVLW		b'10111001'
		RETURN
C_1000	MOVLW		b'00010000'
		RETURN
C_1001	MOVLW		b'00110000'
		RETURN
C_1010	MOVLW		b'11111110'
		RETURN
C_1011	MOVLW		b'11111110'
		RETURN
C_1100	MOVLW		b'11111110'
		RETURN
C_1101	MOVLW		b'11111110'
		RETURN
C_1110	MOVLW		b'11111110'
		RETURN
C_1111	MOVLW		b'11111110'
		RETURN



;-----------------------------------------------------------------------------------
; KÓDOVÁNÍ BCD-SEGMENTY
;-----------------------------------------------------------------------------------
zobraz_B
		MOVWF		amp
		
B_xxxx	BTFSC		amp,0			;převod amp na 1 z 16.
		GOTO		B_xxx1
		GOTO		B_xxx0


B_xxx0	BTFSC		amp,1
		GOTO		B_xx10
		GOTO		B_xx00

B_xxx1	BTFSC		amp,1
		GOTO		B_xx11
		GOTO		B_xx01


B_xx00	BTFSC		amp,2
		GOTO		B_x100
		GOTO		B_x000

B_xx01	BTFSC		amp,2
		GOTO		B_x101
		GOTO		B_x001

B_xx10	BTFSC		amp,2
		GOTO		B_x110
		GOTO		B_x010

B_xx11	BTFSC		amp,2
		GOTO		B_x111
		GOTO		B_x011


B_x000	BTFSC		amp,3
		GOTO		B_1000
		GOTO		B_0000

B_x001	BTFSC		amp,3
		GOTO		B_1001
		GOTO		B_0001

B_x010	BTFSC		amp,3
		GOTO		B_1010
		GOTO		B_0010

B_x011	BTFSC		amp,3
		GOTO		B_1011
		GOTO		B_0011

B_x100	BTFSC		amp,3
		GOTO		B_1100
		GOTO		B_0100

B_x101	BTFSC		amp,3
		GOTO		B_1101
		GOTO		B_0101

B_x110	BTFSC		amp,3
		GOTO		B_1110
		GOTO		B_0110

B_x111	BTFSC		amp,3
		GOTO		B_1111
		GOTO		B_0111	

;-----------------------------------------------------------------------------------
		

		
B_0000	MOVLW		b'10010000'
		RETURN
B_0001	MOVLW		b'11010111'
		RETURN
B_0010	MOVLW		b'10100100'
		RETURN
B_0011	MOVLW		b'11000100'
		RETURN
B_0100	MOVLW		b'11001011'
		RETURN
B_0101	MOVLW		b'11001000'
		RETURN
B_0110	MOVLW		b'10001000'
		RETURN
B_0111	MOVLW		b'11010101'
		RETURN
B_1000	MOVLW		b'10000000'
		RETURN
B_1001	MOVLW		b'11000000'
		RETURN
B_1010	MOVLW		b'11101111'
		RETURN
B_1011	MOVLW		b'11101111'
		RETURN
B_1100	MOVLW		b'11101111'
		RETURN
B_1101	MOVLW		b'11101111'
		RETURN
B_1110	MOVLW		b'11101111'
		RETURN
B_1111	MOVLW		b'11101111'
		RETURN




;-----------------------------------------------------------------------------------
;ČEKÁNÍ
;-----------------------------------------------------------------------------------

;----------------------------------------



cekani1
		MOVLW		6
		MOVWF		c3
cek3	MOVLW		100
		MOVWF		c2
cek2	MOVLW		100
		MOVWF		c1
		CLRWDT
cek1	DECFSZ		c1,1
		GOTO		cek1
		DECFSZ		c2,1
		GOTO		cek2
		DECFSZ		c3,1
		GOTO		cek3
		RETURN

cekani2
		MOVLW		3
		MOVWF		c2
cek5	MOVLW		3
		MOVWF		c1
cek4	DECFSZ		c1,1
		GOTO		cek4
		DECFSZ		c2,1
		GOTO		cek5
		CLRWDT
		RETURN

cekani3
		MOVLW		255
		MOVWF		c1
cek7	DECFSZ		c1,1
		GOTO		cek7
		CLRWDT

		RETURN




		END
