;  SATFINDER 
;    verze 1.0
        LIST P = 16F877A, R = DEC
        INCLUDE <P16F877A.INC>
        __config _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC & _DEBUG_OFF & _CPD_OFF & _LVP_OFF
        

 cblock          0x20                 

 HODNOTA            
 MASKA               
 FIXDELAY              
 EE_ADR                
 EE_DATA               
 N       
 ADRESA_STV 
 DATAIN 
 DATA_STV  
 D1
 D2
 D3
 D4
 D5
 D6
 D7
 D8
 F1        ;31
 F2
 F3
 F4
 F5
 SR
 DATA1_TSA
 FEC
 DATA2_TSA
 DATA_LNB      ;3a
 CNT
 FNT                                     
 SAT_IDENT
 FR_IDENT
 POCT
 LOCK  
 ERROR_CNT1
 ERROR_CNT2
 SNR_VALUE 
 LV
 OT1
 OT2
 RO
 CNTPR
 DISEQ1           ; 1 byte diseq dat
 DISEQ2           ; 2 byte diseq dat
 DISEQ3           ; 3 byte diseq dat
 DISEQ4           ; 4 byte diseq dat
 FIFO             ; fifo registr diseq dat
 DISEQDRV         ; nastavení diseq a 22khz v stv0299b
 DISEQSTAT        ; Fifo plné nebo prázdné  
 CNT2             ; počítadlo vstupu diseq 
 DATA_LNB2    
              endc


              org	0x2100		; tabulka v pameti EEPROM (256 bytu)

	          de	0x31, 0x32, 0x35, 0x32, 0x35, 0x50, 0x3C, 0x04  ;00-07
	          de	0x28, 0x24, 0xff, 0x31, 0x32, 0x35, 0x36, 0x35  ;08-0f
	          de	0x50, 0x3D, 0x02, 0x68, 0x2C, 0xff, 0x31, 0x32  ;10-17
              de	0x36, 0x38, 0x35, 0x50, 0x41, 0x04, 0x28, 0x24  ;18-1f
	          de	0xff, 0x31, 0x31, 0x39, 0x39, 0x32, 0x50, 0x2B  ;20-27
	          de	0x04, 0x80, 0x2c, 0xff, 0x31, 0x32, 0x35, 0x37  ;28-2f
	          de	0x35, 0x40, 0x3d, 0x08, 0xb8, 0x2c, 0xff, 0x31  ;30-37
	          de	0x32, 0x36, 0x34, 0x30, 0x40, 0x3f, 0x08, 0xc0  ;38-3f
;		----------------------------------------------
	          de	0x24, 0xff, 0x31, 0x32, 0x36, 0x39, 0x32, 0x40  ;40-47
	          de	0x41, 0x08, 0x60, 0x2c, 0xff, 0x31, 0x31, 0x38  ;48-4f
	          de	0x35, 0x36, 0x50, 0x27, 0x04, 0x40, 0x24, 0xff  ;50-57
	          de	0x31, 0x32, 0x32, 0x38, 0x35, 0x50, 0x34, 0x04  ;58-5f
	          de	0xa8, 0x2c, 0xff, 0x31, 0x30, 0x38, 0x39, 0x32  ;60-67
	          de	0x50, 0x23, 0x04, 0xd0, 0x0c, 0xff, 0x31, 0x32  ;68-6f
	          de	0x30, 0x33, 0x34, 0x50, 0x2c, 0x04, 0xd0, 0x24  ;70-77
	          de	0xff, 0x31, 0x30, 0x39, 0x33, 0x30, 0x50, 0x24  ;78-7f
;		----------------------------------------------
              de	0x04, 0xe0, 0x0c, 0xff, 0x31, 0x32, 0x36, 0x38  ;80-87
	          de	0x38, 0x50, 0x41, 0x04, 0x40, 0x2c, 0xff, 0x31  ;88-8f
	          de	0x32, 0x36, 0x34, 0x33, 0x50, 0x7f, 0x04, 0xd8  ;90-97
	          de	0x2c, 0xff, 0x31, 0x32, 0x37, 0x32, 0x33, 0x50  ;98-9f
	          de	0x42, 0x04, 0x58, 0x2c, 0xff, 0x31, 0x32, 0x35  ;a0-a7
	          de	0x32, 0x37, 0x50, 0x3c, 0x04, 0x38, 0x2c, 0xff  ;a8-af
	          de	0x31, 0x31, 0x32, 0x32, 0x32, 0x50, 0x2e, 0x02  ;b0-b7
	          de	0x00, 0x0c, 0xff, 0x31, 0x31, 0x34, 0x38, 0x38  ;b8-bf
;		----------------------------------------------
	          de	0x50, 0x36, 0x02, 0x50, 0x04, 0xff, 0x31, 0x31  ;c0-c7
	          de	0x36, 0x30, 0x34, 0x50, 0x39, 0x02, 0xf0, 0x04  ;c8-cf
	          de	0xff, 0x31, 0x31, 0x35, 0x32, 0x37, 0x50, 0x37  ;d0-d7
	          de	0x02, 0x88, 0x04, 0xff, 0xff, 0xff, 0xff, 0xff
	          de	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
	          de	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
	          de	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
              de	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff



                      ORG 0x00
                      GOTO INIT


INIT    BCF     STATUS,RP0       ; BANKA 0
        BCF     STATUS,RP1       ; BANKA 0
        CLRF    PORTA            ; Vynulování portu A
        CLRF    PORTB            ; Vynulování portu B
        CLRF    PORTC            ; vynulování portu C
        CLRF    PORTD            ; vynulování portu D
        CLRF    PORTE
        CLRF    EE_ADR
        CLRF     CCP1CON
        CLRF     CCP2CON
        BSF     STATUS,RP0       ; přepnutí do banky 1
        BCF     STATUS,RP1
        MOVLW   B'00000'        
        MOVWF   TRISA            ; NASTAVENÍ RA<4:0> výstupy  
        BCF     ADCON1,PCFG0
        BSF     ADCON1,PCFG1     ; stále v bance 1
        BSF     ADCON1,PCFG2
        BCF     ADCON1,PCFG3     ; vypnutí vstupů A/D převodníku
        MOVLW   B'00000000'        
        MOVWF   TRISB            ; nastav  RB0-RB7 jako výstupy
        MOVLW   B'10001111'      ; nastavení předděliče portb off,1:256 Watchdog
        MOVWF   OPTION_REG
        BSF     CMCON,CM0
        BSF     CMCON,CM1
        BSF     CMCON,CM2        ; komparátory vypnout
        MOVLW   B'000'
        MOVWF   TRISE
        ADDLW   B'00000000'      ; všechny vývody  jako výstupy
        MOVWF   TRISD
        MOVLW   B'11111111'      ; všechny vývody PORTU C vstupy
        MOVWF   TRISC
        MOVLW   B'01100000'
        MOVWF    SSPCON2
        MOVLW   0x06
        MOVWF   SSPADD           ; nastavení 400khz I2C pro krystal 10Mhz
        BCF     SSPSTAT,CKE      ; nastavení specifikace I2C
        BSF     SSPSTAT,SMP      ; data jsou vzorkována po odeslání výstupních dat
        BCF     STATUS,RP0       ; banka 0
        MOVLW   0x38
        MOVWF   SSPCON           ; nastavení I2C master mode
        BCF     PIR1,PSPIF       ; vynulovat příznak čtení-zápisu
        BCF     PIR1,SSPIF       ; vynulovat příznak dokončení
        BCF     PIR2,BCLIF       ; vynulovat příznak kolize
        CLRF    INTCON           ; žádné přerušení ani timery
        MOVLW	0xE6		     ; Call for 46ms delay
		CALL 	NDELAY	         ; Počkej na napětí VCC pro LCD až dosáhne 5V
        MOVLW   0xE6 
        CALL    NDELAY
        MOVLW   0xE6
        CALL    NDELAY
        MOVLW   0xE6
        CLRF    HODNOTA
        CLRF    MASKA
        MOVLW   0x01
        MOVWF   CNT2
        BSF     PORTA,3         ; pošli reset pro STV0299B
        MOVLW   0x16
        CALL    NDELAY          ; bude trvat asi 5 ms           
        BCF     PORTA,3         ; ukonči reset
        MOVLW   0x01
        MOVWF   CNT
        MOVWF   FNT
        BCF     PORTE,2
        BCF     PORTA,5
        BCF     PORTE,0
        BCF     PORTE,1



INITLCD	BCF		PORTA, 0	; Clear RS to select Instruction Reg.
		BCF		PORTA, 1	; Clear R/W to write
		
		MOVLW	B'00111111'	; Function Set to 8 bit
		MOVWF	PORTB
		CALL	ENABLEPULSE
		MOVLW	0x15		; Call for 4.2ms delay
		CALL 	NDELAY

		MOVLW	B'00111111'	; Function Set to 8 bit again
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY200	; Call for 200us delay

		MOVLW	B'00111111'	; Function Set to 8 bit again, to make sure it's 8 bit, really
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY200	; Call for 200us delay

		MOVLW	B'00111011'	; Function Set to 8 bits, 2 lines and 5x7 dot matrix
		MOVWF 	PORTB
		CALL	ENABLEPULSE
		CALL	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

		MOVLW	B'00001000'	; Display OFF
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

		MOVLW	B'00000001'	; Clear Display
		MOVWF	PORTB
		CALL	ENABLEPULSE
		MOVLW	0x09		; Call 1.8ms delay and wait for instruction completion				
		CALL	NDELAY		

		MOVLW	B'00000010'	; Cursor Home
		MOVWF	PORTB
		CALL	ENABLEPULSE
		MOVLW	0x09		; zavolej čekací smyčku 1.8ms pro dokončení instrukce				
		CALL	NDELAY
		
		MOVLW	B'00001100'	; Display ON, Cursor OFF, Blinking OFF
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

		MOVLW 	B'00000110'	; Entry Mode Set, Increment & No display shift
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

; End of Initialization of LCD.
               

MAIN            GOTO    SAT0
VALUE           MOVF    DATA_LNB,W
                MOVWF   DATA_LNB2
                CALL    ZAPIS_LNBP
                MOVLW   0x40
                MOVWF   DISEQDRV
DSQR1           MOVLW   0x10
                CALL    NDELAY
                BCF     STATUS,RP0
                BSF     PORTA,3         ; pošli reset pro STV0299B
                MOVLW   0x02
                CALL    NDELAY          ; bude trvat asi 400 us           
                BCF     PORTA,3         ; ukonči reset                
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                CALL    FULL_STV         ; naplň registry STV0299B
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0x1B
                MOVWF   ADRESA_STV               
                CALL    CTENI_STV
                MOVF    DATAIN,W
                MOVWF   LOCK
                MOVLW   0x24
                MOVWF   ADRESA_STV
                CALL    CTENI_STV
                MOVF    DATAIN,W
                MOVWF   SNR_VALUE
                MOVLW   0x1d
                MOVWF   ADRESA_STV
                CALL    CTENI_STV
                MOVF    DATAIN,W
                MOVWF   ERROR_CNT1
                INCF    ADRESA_STV,F
                CALL    CTENI_STV
                MOVF    DATAIN,W
                MOVWF   ERROR_CNT2
                CALL    DISP_HO
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                CALL    LADENI
                BTFSC   PORTC,0
                GOTO    FORW
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                BTFSS   PORTC,0
                GOTO    DISEQ
FORW            MOVLW   0xFF
                CALL    NDELAY 
                MOVLW   0x01
                MOVWF   CNT
                MOVWF   FNT
                MOVWF   SAT_IDENT
                GOTO    VALUE            

SAT0            BSF     SAT_IDENT,0
SAT             CALL    DISP_SAT         
                BTFSC   PORTC,0
                GOTO    AS
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                BTFSS   PORTC,0
                INCF    CNT,F
                MOVLW   0xFF
                CALL    NDELAY                
AS              BTFSC   PORTC,2
                GOTO    PLP
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY 
                BTFSC   PORTC,2
                GOTO    SAT
                GOTO    FR
PLP             MOVLW   0x06
                SUBWF   CNT,W
                BTFSS   STATUS,C
                GOTO    DD
                MOVLW   0x01
                MOVWF   CNT 
DD              MOVLW   0x01
                SUBWF   CNT,W
                BTFSS   STATUS,Z
                GOTO    DAL
                MOVLW   B'00000001'
                MOVWF   SAT_IDENT
                GOTO    SAT1 
DAL             MOVLW   0x02
                SUBWF   CNT,W
                BTFSS   STATUS,Z
                GOTO    DAL2
                MOVLW   B'00000010'
                MOVWF   SAT_IDENT
                GOTO    SAT2
DAL2            MOVLW   0x03
                SUBWF   CNT,W
                BTFSS   STATUS,Z
                GOTO    DAL3
                MOVLW   B'00000100'
                MOVWF   SAT_IDENT
                GOTO    SAT3
DAL3            MOVLW   0x04
                SUBWF   CNT,W
                BTFSS   STATUS,Z
                GOTO    DAL4
                MOVLW   B'00001000'
                MOVWF   SAT_IDENT
                GOTO    SAT4
DAL4            MOVLW   0x04
                SUBWF   CNT,W
                BTFSS   STATUS,Z
                MOVLW   B'00010000'
                MOVWF   SAT_IDENT
                GOTO    SAT5     
                GOTO    SAT

SAT1            MOVLW   0x41
                MOVWF   D1
                MOVLW   0x53
                MOVWF   D2
                MOVLW   0x54
                MOVWF   D3
                MOVLW   0x52
                MOVWF   D4
                MOVLW   0x41
                MOVWF   D5
                MOVLW   0x20
                MOVWF   D6
                MOVWF   D8
                MOVLW   0x33
                MOVWF   D7
                GOTO    SAT

SAT2            MOVLW   0x41
                MOVWF   D1
                MOVLW   0x53
                MOVWF   D2
                MOVLW   0x54
                MOVWF   D3
                MOVLW   0x52
                MOVWF   D4
                MOVLW   0x41
                MOVWF   D5
                MOVLW   0x20
                MOVWF   D6
                MOVWF   D8
                MOVLW   0x31
                MOVWF   D7
                GOTO    SAT
                

SAT3            MOVLW   0x48
                MOVWF   D1
                MOVLW   0x4f
                MOVWF   D2
                MOVLW   0x54
                MOVWF   D3
                MOVLW   0x42
                MOVWF   D4
                MOVLW   0x49
                MOVWF   D5
                MOVLW   0x52
                MOVWF   D6
                MOVLW   0x20
                MOVWF   D8
                MOVLW   0x44
                MOVWF   D7
                GOTO    SAT

SAT4            MOVLW   0x49
                MOVWF   D1
                MOVLW   0x4e
                MOVWF   D2
                MOVLW   0x54
                MOVWF   D3
                MOVLW   0x45
                MOVWF   D4
                MOVLW   0x4c
                MOVWF   D5
                MOVLW   0x53
                MOVWF   D6
                MOVLW   0x41
                MOVWF   D7
                MOVLW   0x54
                MOVWF   D8
                GOTO    SAT

SAT5            MOVLW   0x45
                MOVWF   D1
                MOVLW   0x55
                MOVWF   D2
                MOVLW   0x52
                MOVWF   D3
                MOVLW   0x4f
                MOVWF   D4
                MOVLW   0x42
                MOVWF   D5
                MOVLW   0x49
                MOVWF   D6
                MOVLW   0x52
                MOVWF   D7
                MOVLW   0x44
                MOVWF   D8
                GOTO    SAT


FR              CALL    DISP_FR
                BTFSC   PORTC,1
                GOTO    KD
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                BTFSS   PORTC,1
                INCF    FNT,F
KD              BTFSC   PORTC,2
                GOTO    VA
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                BTFSC   PORTC,2
                GOTO    VA
                GOTO    VALUE
VA              MOVLW   0x05
                SUBWF   FNT,W
                BTFSS   STATUS,C
                GOTO    LA
                MOVLW   0x01
                MOVWF   FNT 
LA              MOVLW   0x01
                SUBWF   FNT,W
                BTFSS   STATUS,Z
                GOTO    NT
                BTFSC   SAT_IDENT,0
                CALL    S1.1
                BTFSC   SAT_IDENT,1
                CALL    S2.1
                BTFSC   SAT_IDENT,2
                CALL    S3.1
                BTFSC   SAT_IDENT,3
                CALL    S4.1
                BTFSC   SAT_IDENT,4
                CALL    S5.1
NT              MOVLW   0x02
                SUBWF   FNT,W
                BTFSS   STATUS,Z
                GOTO    MT
                BTFSC   SAT_IDENT,0
                CALL    S1.2
                BTFSC   SAT_IDENT,1
                CALL    S2.2
                BTFSC   SAT_IDENT,2
                CALL    S3.2
                BTFSC   SAT_IDENT,3
                CALL    S4.2
                BTFSC   SAT_IDENT,4
                CALL    S5.2 
MT              MOVLW   0x03
                SUBWF   FNT,W
                BTFSS   STATUS,Z
                GOTO    OT
                BTFSC   SAT_IDENT,0
                CALL    S1.3
                BTFSC   SAT_IDENT,1
                CALL    S2.3
                BTFSC   SAT_IDENT,2
                CALL    S3.3
                BTFSC   SAT_IDENT,3
                CALL    S4.3
                BTFSC   SAT_IDENT,4
                CALL    S5.3
OT              MOVLW   0x04
                SUBWF   FNT,W
                BTFSS   STATUS,Z
                GOTO    FR
                BTFSC   SAT_IDENT,0
                CALL    S1.4
                BTFSC   SAT_IDENT,1
                CALL    S2.4
                BTFSC   SAT_IDENT,2
                CALL    S3.4
                BTFSC   SAT_IDENT,3
                CALL    S4.4
                BTFSC   SAT_IDENT,4
                CALL    S5.4
                GOTO    FR
      

S1.1            MOVLW   0x00
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN

S2.1            MOVLW   0x2C
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN
          

S3.1            MOVLW   0x58
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN              

S4.1            MOVLW   0x84
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN 

S5.1            MOVLW   0xb0
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN             
 
S1.2            MOVLW   0x0B
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN

S2.2            MOVLW   0x37
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN
          

S3.2            MOVLW   0x63
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN              

S4.2            MOVLW   0x8f
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN             

S5.2            MOVLW   0xbb
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN             


S1.3            MOVLW   0x16
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN

S2.3            MOVLW   0x42
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN
          

S3.3            MOVLW   0x6e
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN              

S4.3            MOVLW   0x9a
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN

S5.3            MOVLW   0xc6
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN  

S1.4            MOVLW   0x21
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN

S2.4            MOVLW   0x4D
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN
          

S3.4            MOVLW   0x79
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN              

S4.4            MOVLW   0xa5
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN

S5.4            MOVLW   0xd1
                MOVWF   EE_ADR
                CALL    VYCTI
                MOVLW   0x10
                CALL    NDELAY 
                RETURN             
           

                
VYCTI           MOVLW   0x0A
                MOVWF   POCT
                MOVLW   0x31
                MOVWF   FSR
HH              CALL    EEPR
                MOVWF   INDF
                INCF    FSR,F
                INCF    EE_ADR,F
                DECFSZ  POCT,F
                GOTO    HH
                RETURN
                
DISEQ           CALL     DISP_DISEQ
                BTFSC   PORTC,0
                GOTO    AS2
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                BTFSS   PORTC,0
                INCF    CNT2,F
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0x05
                SUBWF   CNT2,W
                BTFSS   STATUS,C
                GOTO    AS2
                MOVLW   0x01
                MOVWF   CNT2  
AS2             BTFSC   PORTC,2
                GOTO    DISEQ
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY
                MOVLW   0xFF
                CALL    NDELAY 
                BTFSC   PORTC,2
                GOTO    DISEQ
                MOVLW   0x05
                SUBWF   CNT2,W
                BTFSS   STATUS,C
                GOTO    DD22
                MOVLW   0x01
                MOVWF   CNT2 
DD22            MOVLW   0x01
                SUBWF   CNT2,W
                BTFSS   STATUS,Z
                GOTO    DALE
                GOTO    DISEQ_RAW1 
DALE            MOVLW   0x02
                SUBWF   CNT2,W
                BTFSS   STATUS,Z
                GOTO    DALE2
                GOTO    DISEQ_RAW2
DALE2           MOVLW   0x03
                SUBWF   CNT2,W
                BTFSS   STATUS,Z
                GOTO    DALE3
                GOTO    DISEQ_RAW3
DALE3           MOVLW   0x04
                SUBWF   CNT2,W
                BTFSS   STATUS,Z
                GOTO    DISEQ
                GOTO    DISEQ_RAW4
                 
DISEQ_RAW1     MOVLW    0x08
               MOVWF    ADRESA_STV
               MOVLW    0x40
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    0x09
               MOVWF    ADRESA_STV
               CLRF     FIFO
               MOVF     FIFO,W
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    60
               CALL     NDELAY
               MOVLW    0xE0
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x10
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x38
               MOVWF    FIFO
               CALL     DISEQ_RAW 
               MOVLW    0xF0
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0xFF
               MOVWF    FIFO
               MOVLW    90
               CALL     NDELAY
               CALL     DISEQ_RAW  
               MOVLW    150
               CALL     NDELAY
               MOVLW    0x01
               MOVWF    CNT2
               GOTO     VALUE


DISEQ_RAW2     MOVF     DATA_LNB,W
               ANDLW    0x0F
               MOVWF    DATA_LNB2
               CALL     ZAPIS_LNBP
               MOVLW    0x08
               MOVWF    ADRESA_STV
               MOVLW    0x40
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    0x09
               MOVWF    ADRESA_STV
               CLRF     FIFO
               MOVF     FIFO,W
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    60
               CALL     NDELAY
               MOVLW    0xE0
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x10
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x38
               MOVWF    FIFO
               CALL     DISEQ_RAW 
               MOVLW    0xF1
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0xFF
               MOVWF    FIFO
               MOVLW    90
               CALL     NDELAY
               CALL     DISEQ_RAW  
               MOVLW    150
               CALL     NDELAY
               MOVLW    0x01
               MOVWF    CNT2
               GOTO     VALUE

DISEQ_RAW3     MOVLW    0x08
               MOVWF    ADRESA_STV
               MOVLW    0x40
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    0x09
               MOVWF    ADRESA_STV
               CLRF     FIFO
               MOVF     FIFO,W
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    60
               CALL     NDELAY
               MOVLW    0xE0
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x10
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x38
               MOVWF    FIFO
               CALL     DISEQ_RAW 
               MOVLW    0xF2
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0xFF
               MOVWF    FIFO
               MOVLW    90
               CALL     NDELAY
               CALL     DISEQ_RAW  
               MOVLW    150
               CALL     NDELAY
               MOVLW    0x01
               MOVWF    CNT2
               GOTO     VALUE


DISEQ_RAW4     MOVLW    0x08
               MOVWF    ADRESA_STV
               MOVLW    0x40
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    0x09
               MOVWF    ADRESA_STV
               CLRF     FIFO
               MOVF     FIFO,W
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    60
               CALL     NDELAY
               MOVLW    0xE0
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x10
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0x38
               MOVWF    FIFO
               CALL     DISEQ_RAW 
               MOVLW    0xF3
               MOVWF    FIFO
               CALL     DISEQ_RAW
               MOVLW    0xFF
               MOVWF    FIFO
  ;            MOVLW    90
  ;             CALL     NDELAY
               CALL     DISEQ_RAW  
               MOVLW    150
               CALL     NDELAY
               MOVLW    0x01
               MOVWF    CNT2
               GOTO     VALUE

                        

DISEQ_RAW      MOVLW    0x08
               MOVWF    ADRESA_STV
               MOVLW    0x06
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
               MOVLW    0x09
               MOVWF    ADRESA_STV
               MOVF     FIFO,W
               MOVWF    DATA_STV
               CALL     ZAPIS_STV
TESTKZ         MOVLW    0x0A
               MOVWF    ADRESA_STV
               CALL     CTENI_STV 
               BTFSC    DATAIN,0
TEST2J5        GOTO     TESTKZ
               BTFSS    DATAIN,1
               GOTO     TEST2J5               
               RETURN

DISP_SAT	BSF		PORTA, 0	; Set RS to select Data Reg.
		    BCF		PORTA, 1	; Clear R/W to write

		MOVLW	A's'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVLW	A'a'		; načti znak pro zobrazení na LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVLW	A't'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL   	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

	
		MOVLW	A'e'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.
         
        MOVLW   A'l'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

		
		MOVLW	A'i'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.
        
        MOVLW	A't'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A':'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A' '		; Send char to be displayed on LCD
	   	MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	

        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	
   
        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        CALL   NEXTLINE

        BSF    PORTA,0
        BCF    PORTA,1
        
        MOVF   D1,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVF   D2,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50 
        
        MOVF   D3,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVF   D4,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVF   D5,W 
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF   D6,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF   D7,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF   D8,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW   A' '
        MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW   A' ' 
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A' '		; 11 znak
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		; 13 znak
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50
        
        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		;16 znak
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50  	


      
        BCF     PORTA, 0
        BCF     PORTA, 1
        MOVLW	B'00000010'	; Cursor Home
	    MOVWF	PORTB
    	CALL	ENABLEPULSE
		MOVLW	0x09		; Call 1.8ms delay and wait for instruction completion				
		CALL	NDELAY

        RETURN

DISP_DISEQ	BSF		PORTA, 0	; Set RS to select Data Reg.
	        BCF		PORTA, 1	; Clear R/W to write

		MOVLW	A'D'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVLW	A'i'		; načti znak pro zobrazení na LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVLW	A's'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL   	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

	
		MOVLW	A'e'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.
         
        MOVLW   A'q'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

		
		MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.
        
        MOVLW	A'v'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A's'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A't'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A'u'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A'p'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A':'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; Wait for instruction completion.

        MOVLW	A' '		; Send char to be displayed on LCD
	   	MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	

        MOVF    CNT2,W		; Send char to be displayed on LCD
        CALL    PJZH
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	
   
        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        CALL   NEXTLINE

        BSF    PORTA,0
        BCF    PORTA,1
        
        MOVLW   A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW   A' ' 
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50 
        
        MOVLW   A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW   A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW   A' ' 
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW   A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW   A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW   A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW   A' '
        MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW   A' ' 
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A' '		; 11 znak
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		; 13 znak
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50
        
        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVLW	A' '		;16 znak
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50  	


      
        BCF     PORTA, 0
        BCF     PORTA, 1
        MOVLW	B'00000010'	; Cursor Home
	    MOVWF	PORTB
    	CALL	ENABLEPULSE
		MOVLW	0x09		; Call 1.8ms delay and wait for instruction completion				
		CALL	NDELAY

        RETURN

PJZH        ANDLW   0x0F
            ADDLW   0x30
            RETURN              

ENABLEPULSE	BCF	PORTA, 2
			CALL DELAY50
			BSF	PORTA, 2	; 50us E-Pulse
			CALL DELAY50
			BCF PORTA, 2
			RETURN

NEXTLINE BCF PORTA,0
         BCF PORTA,1
         MOVLW b'11000000'
         MOVWF PORTB
         CALL ENABLEPULSE
         RETURN

PREVOD      ANDLW   0x0F
            MOVWF   MASKA
            SUBLW   0x0A
            BTFSC   STATUS,Z
            GOTO    PRICTI2
            BTFSS   STATUS,C
            GOTO    PRICTI2
            MOVF    MASKA,W
            ADDLW   0x30
            RETURN           

PRICTI2     MOVF    MASKA,W 
            ADDLW   0x37
            RETURN

;***********************************************************************************
; N x 200us
;***********************************************************************************

NDELAY
			MOVWF N				; načti N do akumulátoru
NOTOVER		CALL DELAY200		
			DECFSZ N, 1			
			GOTO NOTOVER	
			RETURN
	
;***********************************************************************************
; 200us
;***********************************************************************************

DELAY200	
			MOVLW 0xA6			; A6 hexa = 166 smyček cca 200us pro krystal 10Mhz
			MOVWF FIXDELAY		
NOTDONE200	DECFSZ FIXDELAY, 1 	
			GOTO NOTDONE200		
			RETURN				

;***********************************************************************************
; 50us
;***********************************************************************************

DELAY50	
			MOVLW 0x2A			; 2A hexa = 42 smyček cca 50us pro krastal 10Mhz
			MOVWF FIXDELAY		
NOTDONE50	DECFSZ FIXDELAY, 1 	
			GOTO NOTDONE50		
			RETURN				


ZAPIS_LNBP     CALL   START_I2C       ; pošli START bit
               CALL   I2C_TEST        ; počkej na dokončení podmínky start
               MOVLW  0x10            ; adresa obvodu LNBP21PD
               MOVWF  SSPBUF          ; přesuň do bufferu a pošli
               CALL   I2C_TEST        ; data vyslána?
               CALL   I2C_ACKIN       ; čekej na ACK impuls od LNBP21PD
               MOVF   DATA_LNB2,W      ; načti data do W
               MOVWF  SSPBUF          ; pošli data do LNBP21PD
               CALL   I2C_TEST        ; už jsou data poslána?
               CALL   I2C_ACKIN       ; čekej na ACK od LNBP
               CALL   STOP_I2C        ; ukonči přenos
               RETURN

LADENI         BSF    PORTE,2
               BCF    PORTA,5
               MOVF   DATA1_TSA,W
               MOVWF  PORTD
               MOVLW   0x50
               CALL   NDELAY
               BCF    PORTE,2
               BCF    PORTA,5
               MOVLW   0x10
               CALL   NDELAY
               BSF    PORTA,5
               MOVF   DATA2_TSA,W
               MOVWF  PORTD
               MOVLW  0x50
               CALL   NDELAY
               BCF    PORTA,5
               MOVLW  0x10
               CALL   NDELAY
               BSF    PORTE,2
               BSF    PORTA,5
               MOVF   SNR_VALUE,W
               MOVWF  PORTD
               MOVLW  0x50
               CALL   NDELAY
               BCF    PORTE,2
               BCF    PORTA,5
               RETURN

ZPROC         MOVLW   0x27
              SUBWF   SNR_VALUE,W
              BTFSC   STATUS,C
              GOTO    LOWSIGNAL
              MOVLW   0x1D
              SUBWF   SNR_VALUE,W
              BTFSC   STATUS,C
              GOTO    ROZSAH1
              MOVLW   0x13
              SUBWF   SNR_VALUE,W
              BTFSC   STATUS,C
              GOTO    ROZSAH2
              MOVLW   0x09
              SUBWF   SNR_VALUE,W
              BTFSC   STATUS,C
              GOTO    ROZSAH3
              

ROZSAH1       MOVLW   0x37
              MOVWF   OT1
              MOVLW   0x0A
              MOVWF   CNTPR
              MOVLW   0x1D
              MOVWF   RO
              GOTO    HODN
              

ROZSAH2       MOVLW   0x38
              MOVWF   OT1
              MOVLW   0x0A
              MOVWF   CNTPR
              MOVLW   0x13
              MOVWF   RO
              GOTO    HODN
              

ROZSAH3       MOVLW   0x39
              MOVWF   OT1
              MOVLW   0x0A
              MOVWF   CNTPR
              MOVLW   0x09
              MOVWF   RO
              GOTO    HODN
              

HODN          MOVF    RO,W
              SUBWF   SNR_VALUE,W
              BTFSC   STATUS,Z
              GOTO    VH
              INCF    RO,F
              DECFSZ  CNTPR,F
              GOTO    HODN
              

VH            DECF    CNTPR,W
              ADDLW   0x30
              MOVWF   OT2
              GOTO    KJL

LOWSIGNAL     MOVLW   0x4C
              MOVWF   OT1
              MOVLW   0x4F
              MOVWF   OT2
              GOTO    KJL
               

I2C_GET        BSF    STATUS,RP0      ; banka 1
               BSF    SSPCON2,RCEN    ; nastav příjem dat
               BCF    STATUS,RP0      ; banka 0
               CALL   I2C_TEST        ; čekej na provedení
               MOVF   SSPBUF,W        ; přesuň data z reg SSPBUF do W
               BSF    STATUS,RP0      ; banka 1
               BCF    SSPCON2,RCEN    ; nastav odesílání dat
               BCF    STATUS,RP0      ; zpět na banku 0
               RETURN


I2C_ACKOUT     BSF   STATUS,RP0       ; banka 1
               BSF   SSPCON2,ACKEN    ; vyšli ACK pro stv0299b
               BCF   STATUS,RP0       ; banka 0
               RETURN                   

I2C_TEST       BCF    STATUS,RP0      ; banka 0
STBP           BTFSS  PIR1,SSPIF      ; testuj dokončení vyslání dat -9 sestupnou hranu
               GOTO   STBP            ; není hotovo,znova testuj
               BCF    PIR1,SSPIF      ; vynuluj příznak dokončení operace
               RETURN

I2C_ACKIN      BSF		STATUS,RP0		 ; banka 1
	           BTFSC    SSPCON2,ACKSTAT  ; testuj přijetí pulsu ACK od STV0299B
               GOTO     FAULT            ; chyba na sběrnici,nepřijatý ACK
               BCF      STATUS,RP0       ; banka 0
               RETURN

FAULT          BSF    PORTE,0
               GOTO   MAIN
               
                             
I2C_REPSTART   CALL  NECINNOST_I2C
               BSF   STATUS,RP0
               BCF   STATUS,RP1
               BSF   SSPCON2,RSEN
               BCF   STATUS,RP0
               RETURN                     

START_I2C      CALL  NECINNOST_I2C
               BSF   STATUS,RP0     
               BCF   STATUS,RP1      ; banka 1
               BSF   SSPCON2,SEN     ; pošli START
               BCF   STATUS,RP0      ; banka 0
               RETURN

STOP_I2C       CALL  NECINNOST_I2C
               BSF   STATUS,RP0      ; banka 1
               BCF   STATUS,RP1     
               BSF   SSPCON2,PEN     ; vyšli STOP bit
               BCF   STATUS,RP0
               RETURN




NECINNOST_I2C  BSF    STATUS,RP0
               BCF    STATUS,RP1     ; banka 1
ST             BTFSC  SSPCON2,SEN    ; testuj zda není na sběrnici posílán START 
               GOTO   ST
OPST           BTFSC  SSPCON2,RSEN   ; testuj zda není na sběrnici posílán opakovací START
               GOTO   OPST
STOP           BTFSC  SSPCON2,PEN    ; testuj zda není na sbšrnici posílán STOP
               GOTO   STOP
PRIJ           BTFSC  SSPCON2,RCEN   ; testuj zda není povolen na sběrnici příjem
               GOTO   PRIJ
AC             BTFSC  SSPCON2,ACKEN  ; testuj zda není odesílán ACK
               GOTO   AC
ZNCT           BTFSC  SSPSTAT,2      ; testuj zda neprobíhá zápis nebo čtení
               GOTO   ZNCT
               BCF    STATUS,RP0     ; banka 0
               RETURN

CTENI_STV      BCF    STATUS,RP1      ; banka 0
               BCF    STATUS,RP0
               CALL   START_I2C       ; pošli START bit
               CALL   I2C_TEST        ; čekej na dončení START bitu
               MOVLW  0xD0            ; načti adresu zařízení pro STV0299B- zápis 
               MOVWF  SSPBUF          ; načti do bufferu
               CALL   I2C_TEST        ; čekej na dokončení-9 sestupnou hranu
               CALL   I2C_ACKIN       ;čekej na potvrzení přijetí ACK pulsu od STV0299B
               MOVF   ADRESA_STV,W        ; načti adresu ze které se bude z STV0299B číst
               MOVWF  SSPBUF          ; přesuň do bufferu
               CALL   I2C_TEST        ; čekej na dokončení vyslání dat
               CALL   I2C_ACKIN       ; testuj potvrzení přijetí adresy
               CALL   STOP_I2C        ; pošli STOP bit
               BCF    STATUS,RP0      ; banka 0
               CALL   I2C_TEST        ; čekej na dokončení poslání STOP bitu
               MOVLW  0x19
               CALL   NDELAY          ; počkej 5 ms
               CALL   START_I2C       ; pošli START bit
               CALL   I2C_TEST        ; čekej na dokončení START bitu
               MOVLW  0xD1            ; načti device adress pro stv0299b-čtení
               MOVWF  SSPBUF          ; načti adresu do bufferu 
               CALL   I2C_TEST        ; čekej na dokončení poslání adresy stv0299b
               CALL   I2C_ACKIN       ; testuj přijetí ACK pulsu od STV0299B
               CALL   I2C_GET         ; přijmi data od stv0299b z adresy ADRESA
               MOVWF  DATAIN          ; přesuň data z W do reg DATAIN
               CALL   I2C_ACKOUT      ; vyšli ACK impuls pro stv0299b
               CALL   STOP_I2C        ; vyšli STOP bit - konec komunikace
               RETURN


ZAPIS_STV      CALL   START_I2C       ; pošli START bit
               CALL   I2C_TEST        ; čekej na dokončení START bitu
               MOVLW  0xD0            ; načti adresu obvodu stv0299b-zápis
               MOVWF  SSPBUF          ; načti do bufferu
               CALL   I2C_TEST        ; čekej na dokončení operace
               CALL   I2C_ACKIN       ; čekej na potvrzení ACK od stv0299
               MOVF   ADRESA_STV,W        ; načti adresu kam se bude zapisovat do W
               MOVWF  SSPBUF          ; přesuň do bufferu
               CALL   I2C_TEST        ; testuj dokončení vyslání adresy
               CALL   I2C_ACKIN       ; čekej na ACK odpověď
               MOVF   DATA_STV,W      ; načti data do W
               MOVWF  SSPBUF          ; načti data k odeslání do bufferu
               CALL   I2C_TEST        ; testuj dokončení odeslání dat
               CALL   I2C_ACKIN       ; testuj přijetí impulsu ACK od STV0299b
               CALL   STOP_I2C        ; pošli STOP bit a konec zápisu                               
               RETURN

FULL_STV        MOVLW   0x01
                MOVWF   ADRESA_STV
                MOVLW   0x15                 ; K = 0 ,DIRclk = 0,M = 0x15
                MOVWF   DATA_STV             
                CALL    ZAPIS_STV            ; pošli do reg 01 stv0299b
                INCF    ADRESA_STV,F           
                MOVLW   0x00                 ; STBY = 0,VCO on,SERCLK = 0,P = 0,Fvco = 352Mhz
                MOVWF   DATA_STV   
                CALL    ZAPIS_STV            ; pošli do reg 02
                INCF    ADRESA_STV,F
                CLRF    DATA_STV             ; auxiliary clock not used
                CALL    ZAPIS_STV            ; pošli do reg 03 0x00
                INCF    ADRESA_STV,F
                MOVLW   0x7D                 ; f22 = 22khz
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; pošli do reg 04 0x7b
                MOVLW   0x06
                MOVWF   ADRESA_STV           ; registr 06
                MOVLW   0x00
                MOVWF   DATA_STV             ; DAC off
                CALL    ZAPIS_STV            ; pošli do reg 06 0x40
                INCF    ADRESA_STV,F         
                CLRF    DATA_STV
                CALL    ZAPIS_STV            ; reg 07 DAC LSB
                INCF    ADRESA_STV,F
                MOVF    DISEQDRV,W
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 08 nastavení diseq,tone burst
                MOVLW   0x0C
                MOVWF   ADRESA_STV
                MOVLW   0xF1
                MOVWF   DATA_STV
                CALL    ZAPIS_STV
                MOVLW   0x09
                MOVWF   ADRESA_STV
                MOVF    FIFO,W
                MOVWF   DATA_STV 
                CALL    ZAPIS_STV            ; reg 09 fifo
                MOVLW   0x0d
                MOVWF   ADRESA_STV
                MOVLW   0x82
                MOVWF   DATA_STV            
                CALL    ZAPIS_STV            ; reg 0d agc1
                MOVLW   0x28
                MOVWF   ADRESA_STV
                CLRF    DATA_STV
                CALL    ZAPIS_STV            ; FEC normal,paralel,reg 28
                INCF    ADRESA_STV,F
                MOVLW   0x28
                MOVWF   DATA_STV
                CALL    ZAPIS_STV
                MOVLW   0x0E
                MOVWF    ADRESA_STV
                MOVLW   0x44
                MOVWF   DATA_STV
                CALL    ZAPIS_STV          ;reg 0e
                INCF    ADRESA_STV,F
                MOVLW   0x92
                MOVWF   DATA_STV
                CALL    ZAPIS_STV         ; reg 0f
                INCF   ADRESA_STV,F
                MOVLW   0x34
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 10 agc2 konstanty
                INCF    ADRESA_STV,F
                MOVLW   0x84
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 11  potvrzovací sekvence 0x84
                INCF    ADRESA_STV,F
                MOVLW   0xb9
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 12  lock detect -64 ,carrier frekv.on
                INCF    ADRESA_STV,F
                MOVLW   0xb6
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 13   derot on,noise est.256
                INCF    ADRESA_STV,F
                MOVLW   0x93
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 14  phase detect algo 1
                INCF    ADRESA_STV,F
                MOVLW   0xc9
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 15 lock detector treshold
                MOVLW   0x32
                MOVWF   ADRESA_STV
                MOVLW   0x19
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 32 manual mode,Viterbi and synchro konstanty
                INCF    ADRESA_STV,F
                MOVLW   0xFC
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg 33   rs control
                INCF    ADRESA_STV,F
                MOVLW   0x13
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; reg34   error konstanty
                MOVLW   0x1F
                MOVWF   ADRESA_STV
                MOVF    SR,W                 
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; zapiš symbolovou rychlost 1 byte
                MOVLW   0x20
                MOVWF   ADRESA_STV
                MOVLW   0x00
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; zapiš symbolovou rychlost 2 byte
                INCF    ADRESA_STV,F
                MOVLW   0x00
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; zapiš symbolovou rychlost 3 byte 
                MOVLW   0x31
                MOVWF   ADRESA_STV
                MOVF    FEC,W
                MOVWF   DATA_STV
                CALL    ZAPIS_STV            ; zapiš FEC
                RETURN

EEPR       BCF     STATUS,RP0     ;banka 0
           BCF     STATUS,RP1
           MOVF    EE_ADR,W
           BSF     STATUS,RP1
           MOVWF   EEADR 
           BSF     STATUS,RP0  ; banka3
           BCF     EECON1,EEPGD
           BSF     EECON1,RD
           BTFSC   EECON1,0
           goto	$-1 
           BCF     STATUS,RP0  ; banka 2
           MOVF    EEDATA,W
           BCF     STATUS,RP0  ; banka 0
           BCF     STATUS,RP1
           RETURN


DISP_FR	BSF		PORTA, 0	; Set RS to select Data Reg.
	    BCF		PORTA, 1	; Clear R/W to write

		MOVLW   A'f'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVLW	A'r'		; načti znak pro zobrazení na LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVLW	A'e'		; Send char to be displayed on LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL   	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

	
		MOVLW	A'q'	
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		
         
        MOVLW   A'.'		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

		
		MOVLW	A'='		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	
        
        MOVF    F1,W		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVF    F2,W		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVF    F3,W		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVF    F4,W
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVF    F5,W
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW  A'M'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW  A'H'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW  A'z'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW  A' '            ; 16 znak 1 řádek
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

   

        CALL   NEXTLINE

        BSF    PORTA,0
        BCF    PORTA,1
        
        MOVLW  A's'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW  A'a'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50 
        
        MOVLW  A't'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW  A':'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW  A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF   D1,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF   D2,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

 
        MOVF   D3,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF    D4,W
        MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVF    D5,W 
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		


        MOVF    D6,W
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVF    D7,W
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        MOVF    D8,W
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	

        MOVLW  A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
 
        MOVLW  A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW  A' '               ; 16 znak 2 řádek
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50	


      
        BCF     PORTA, 0
        BCF     PORTA, 1
        MOVLW	B'00000010'	; Cursor Home
	    MOVWF	PORTB
    	CALL	ENABLEPULSE
		MOVLW	0x09		; Call 1.8ms delay and wait for instruction completion				
		CALL	NDELAY

        RETURN

DISP_HO	CALL    ZPROC
KJL     BSF		PORTA, 0	; Set RS to select Data Reg.
	    BCF		PORTA, 1	; Clear R/W to write

		MOVLW	A'f'		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVLW	A'='		; načti znak pro zobrazení na LCD
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce


		MOVF    F1,W		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL   	DELAY50		; zavolej čekací smyčku 50us pro dokončení instrukce

	
		MOVF    F2,W		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		
         
        MOVF    F3,W		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

		
		MOVF    F4,W		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		
        
        MOVF    F5,W	
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A' '		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A'L'		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A'O'		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A'C'		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW   A'K'	
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50	       ; 12 pozice	

        MOVLW  A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW  A'='
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW  A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        CALL   LOCK_DETECT
        MOVF   LV,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
   

        CALL   NEXTLINE

        BSF    PORTA,0
        BCF    PORTA,1
        
        MOVLW  A'S'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW  A'N'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50 
        
        MOVLW  A'R'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
        MOVLW  A'='
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50
        
 
        MOVF   OT1,W  
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF   OT2,W
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW  A'%'
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

 
        MOVLW  A' '
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVLW   A'B'
        MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW   A'E' 
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		


        MOVLW	A'R'		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50		

        MOVLW	A'='		
		MOVWF	PORTB
		CALL	ENABLEPULSE
		CALL 	DELAY50

        SWAPF  ERROR_CNT1,W
        CALL   PREVOD
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50		 ; 13 hodnota

        MOVF   ERROR_CNT1,W
        CALL   PREVOD
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        SWAPF  ERROR_CNT2,W
        CALL   PREVOD
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50

        MOVF   ERROR_CNT2,W
        CALL   PREVOD
        MOVWF  PORTB
        CALL   ENABLEPULSE
        CALL   DELAY50


      
        BCF     PORTA, 0
        BCF     PORTA, 1
        MOVLW	B'00000010'	; Cursor Home
	    MOVWF	PORTB
    	CALL	ENABLEPULSE
		MOVLW	0x09		; Call 1.8ms delay and wait for instruction completion				
		CALL	NDELAY

        RETURN

LOCK_DETECT    MOVLW   0x99
               SUBWF   LOCK,W
               BTFSC   STATUS,Z
               GOTO    LOCK_YES
               MOVLW   0x98
               SUBWF   LOCK,W
               BTFSC   STATUS,Z
               GOTO    LOCK_YES
               MOVLW   0x9A
               SUBWF   LOCK,W
               BTFSC   STATUS,Z
               GOTO    LOCK_YES
               MOVLW   0x30
               MOVWF   LV
               BCF     PORTE,1
               RETURN

LOCK_YES       MOVLW   0x31
               MOVWF   LV
               BSF     PORTE,1
               RETURN

			END
