		INCLUDE		"P16C73.INC"
		LIST 		P=16C73, 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 čekání
byte	EQU		48h		;
c5		EQU		49h		;
cas1	EQU		4Ah		;
cas2	EQU		4Bh		;
cislo	EQU		4Ch		;
amp		EQU		4Dh		;
U_1		EQU		4Eh		;
U_2		EQU		4Fh		;

;-----------------------------------------------------------------------------------
;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'
		MOVWF		TRISC


		BCF			STATUS,RP0
;--------------------------------------- a/d převodník

		BSF			STATUS,RP0
		MOVLW		b'11111100'	;A0,A1 analog.
		MOVWF		ADCON1
		BCF			STATUS,RP0
		MOVLW		b'11000001'	;zapnutí A/D
		MOVWF		ADCON0
;--------------------------------------- 


		
		

		BCF			PA,2
		BCF			PA,5





start
;--------------------------------------- 

		CLRWDT
;		BSF			PA,2
;
;		CALL		B_0000
;		MOVWF		PB
;		CALL		cekani2
;		CALL		B_0001
;		MOVWF		PB
;		CALL		cekani2
;		CALL		B_0010
;		MOVWF		PB
;		CALL		cekani2		
;		CALL		B_0011
;		MOVWF		PB
;		CALL		cekani2		
;		CALL		B_0100
;		MOVWF		PB
;		CALL		cekani2		
;		CALL		B_0101
;		MOVWF		PB
;		CALL		cekani2		
;		CALL		B_0110
;		MOVWF		PB
;		CALL		cekani2		
;		CALL		B_0111
;		MOVWF		PB
;		CALL		cekani2		
;		CALL		B_1000
;		MOVWF		PB
;		CALL		cekani2		
;		CALL		B_1001
;		MOVWF		PB
;		CALL		cekani2
;
;
;		GOTO		start
		
		
		
		
		

		
		
		CALL		adc_vstup_0
		CALL		adc
		
		CLRF		U_2
		CLRF		U_1

		INCF		cislo			;jinak by to ukazovalo o 1 míň...
		CALL		vysledek


		MOVF		U_2,w
		CALL		zobraz_C
		MOVWF		PC
		BCF			PC,4			;zobrazení desetinné tečky
		BSF			PA,2
		CALL		cekani3
		BCF			PA,2


		MOVF		U_1,w
		CALL		zobraz_C
		MOVWF		PC
		BSF			PA,5
		CALL		cekani3
		BCF			PA,5
		
		
		MOVLW		b'11111111'		;zhasnutí PC
		MOVWF		PC
		
		
		
		


		CALL		adc_vstup_1
		CALL		adc
		
		CLRF		U_2
		CLRF		U_1

		INCF		cislo
		CALL		vysledek
		

		MOVF		U_2,w
		CALL		zobraz_B
		MOVWF		PB
		BCF			PB,7
		BSF			PA,2
		CALL		cekani3
		BCF			PA,2


		MOVF		U_1,w
		CALL		zobraz_B
		MOVWF		PB
		BSF			PA,5
		CALL		cekani3
		BCF			PA,5
		
		MOVLW		b'11111111'
		MOVWF		PB





		
		GOTO		start
		



;-----------------------------------------------------------------------------------
; A/D přepnutí vstupu
;-----------------------------------------------------------------------------------

adc_vstup_0
		BCF			ADCON0,3
		CALL		cekani3
		RETURN
		
adc_vstup_1
		BSF			ADCON0,3
		CALL		cekani3
		RETURN


;-----------------------------------------------------------------------------------
; A/D převod
;-----------------------------------------------------------------------------------

adc
	BSF			ADCON0,2		; A/D převod
prevod
	BTFSC		ADCON0,2
	GOTO		prevod

	MOVF		ADRES,w
	MOVWF		cislo

	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		20
		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		100
		MOVWF		c2
cek5	MOVLW		200
		MOVWF		c1
cek4	DECFSZ		c1,1
		GOTO		cek4
		DECFSZ		c2,1
		GOTO		cek5
		CLRWDT
		RETURN

cekani3
		MOVLW		100
		MOVWF		c1
cek7	DECFSZ		c1,1
		GOTO		cek7
		CLRWDT
		RETURN
		
cekani4
		MOVLW		10
		MOVWF		c1
cek8	DECFSZ		c1,1
		GOTO		cek8
		CLRWDT
		RETURN



		END
