;upravene cakanie na prijem a nastaveny cas nx400uS pri vysielani aby sa pripravil 627
       LIST P=16F877A
       INCLUDE<P16F877A.INC>
  __CONFIG _PWRTE_ON & _WDT_OFF & _BODEN_OFF & _LVP_OFF & _HS_OSC & _CP_OFF & _DEBUG_OFF & _WRT_OFF & _CPD_OFF


TMP0      	EQU      22H
TMP1      	EQU      23H
VYBER      	EQU      24H
ZNAMENKO   	EQU      25H

#DEFINE      ENABLE   PORTC,1
#DEFINE      RXTX   PORTC,2


      ORG      00H
      GOTO   INIT

;----------------------------ZACATEK PODPROGRAMU--------------------------------------------------

ADPREVOD   
   
      	MOVLW    	0x05           ;CEKANI 750uS PRED ZAHAJENIM PREVODU
        MOVWF    	TMP1
        MOVLW    	0x0DD           ;
        MOVWF    	TMP0
        DECFSZ    	TMP0,1         ;
        GOTO    	$-1
        DECFSZ    	TMP1,1         ;
        GOTO    	$-3
      
      	BSF      	ADCON0,2      ;zahejni prevodu
      	BTFSC   	ADCON0,2      ;zjistovani dokonceni prevodu
      	GOTO  		$-1            ;...
      	RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


VYSILEJL
      	BSF         STATUS,RP0      ;BANKA1   (ADRESL JE V BANCE 1 !!! )     
        MOVF        ADRESL,W         ;do W je ulozena informace a MCU ji nasledne odesila pres UART ven
        BCF			STATUS,RP0      ;BANKA0
        BTFSS      	PIR1,TXIF       ;TESTOVANI  ABYCHOM SI TXREG NEPREPSALI
        GOTO        $-1            ;
        MOVWF       TXREG             ;presunout 8-bit hodnotu do registru TXREG (Bank0) => nacteni dat zacina vysilani
      	RETURN


;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

VYSILEJH     
         MOVF       ADRESH,W      ;do W je ulozena informace a MCU ji nasledne odesila pres UART ven
         BTFSS      PIR1,TXIF     ;TESTOVANI ABYCHOM SI TXREG NEPREPSALI  vzdy pred zapisom !
         GOTO       $-1            ;
         MOVWF      TXREG          ;presunout 8-bit hodnotu do registru TXREG (Bank0) => nacteni dat zacina vysilani
         RETURN

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

VYSILEJZNAMENKO   
         MOVF      ZNAMENKO,W
         BTFSS     PIR1,TXIF     ;TESTOVANI ABYCHOM SI TXREG NEPREPSALI  vzdy pred zapisom !
         GOTO      $-1            ;
         MOVWF     TXREG          ;presunout 8-bit hodnotu do registru TXREG (Bank0) => nacteni dat zacina vysilani
         RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

RXTOTX
      BSF      RXTX	; druha strana 877 sa pripravi na prijem 
      CALL   WAIT400uS	; 627 zacne vysielat BLANK t.j pin RC6 ma uroven HI
      CALL   WAIT400uS
      CALL   WAIT400uS
      CALL   WAIT400uS 
      CALL   WAIT400uS
      CALL   WAIT400uS
      CALL   WAIT400uS
     
      


    RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

TXTORX
      BSF      	STATUS,RP0   ;BANKA1      ;CEKA DOKUD TSR NENI PRAZDNY ABYCHOM SI NEUFIKLI ODCHOZI DATA
      BTFSS   	TXSTA,TRMT               ;   -,,-
      GOTO   	$-1                  ;   -,,-
      BCF      	STATUS,RP0   ;BANKA0      ;   -,,-
      CALL   	WAIT400uS
      BCF      	RXTX
      CALL   	WAIT40uS
      BCF      	ENABLE
      CALL   	WAIT20uS
      BSF      	ENABLE
      CALL   	WAIT200uS
      RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
WAIT20uS                     ; (34*3+4)=23us
      MOVLW 	0x22  ;34 DEC
      MOVWF 	TMP0
      DECFSZ 	TMP0,F
      GOTO 		$-1
      RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

WAIT40uS                    ; (67*3+4)0,2=41us
      MOVLW 	0x43  ;67 DEC
      MOVWF 	TMP0
      DECFSZ 	TMP0,F
      GOTO 		$-1
      RETURN

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

WAIT200uS                  ;.....
      MOVLW 	0x1B  ;27 DEC
      MOVWF 	TMP1
      MOVLW 	0x0B  ;11 DEC
      MOVWF 	TMP0
      DECFSZ 	TMP0,F
      GOTO 		$-1
      DECFSZ 	TMP1,F
      GOTO 		$-5
      RETURN

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

WAIT400uS                   ; cas=((256*(Ktemp1-1)+Ktemp0)*3 +6)iTakt=441us
      	MOVLW 0x03  ;3 DEC
        MOVWF TMP1
        MOVLW 0x0DD  ;221 DEC
        MOVWF TMP0
        DECFSZ TMP0,F
        GOTO $-1
        DECFSZ TMP1,F
        GOTO $-5

      RETURN

;+++++++++++++++++++++++++++++++++++KONEC PODPROGRAMU++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;-----------------------------------INICIALIZACE PERIFERII--------------------------------------------
      
INIT   
      BCF    	STATUS,RP0      ;BANKA0
      BCF      	STATUS,RP1      ;BANKA0
      CLRF   	PORTA
      CLRF   	PORTE
      CLRF   	PORTC
      CLRF   	PORTB
      BSF      	STATUS,RP0      ;BANKA 1
      MOVLW  	B'00000111'      ;INICIALZICAZE komparatoru
      MOVWF   	CMCON         ;
      MOVLW   	B'10000000'      ;INICIALIZACE AD PREVODNIKU
      MOVWF   	ADCON1         ;
      MOVLW   	B'00111111'      ;INICIALIZACE PORTUA RA0-RA5 AD VSTUPY
      MOVWF   	TRISA         ;
      MOVLW   	B'00000111'      ;INICIALIZACE PORTUE RE0-RE2 AD VSTUPY
      MOVWF   	TRISE         ;
      MOVLW   	B'00000000'      ;INICIALIZACE PORTUB VSE VYSTUP
      MOVWF   	TRISB         ;
      
      CLRF   	TRISC         ;PortC 7,6 pro USART, 0 pro detekci zaporneho napeti cidla teploty
      MOVLW   	B'11000001'      ;
      MOVWF   	TRISC         ;
                        
                        
      MOVLW    	D'129'         ;konstanta pro vypocet BAUD RATE
      MOVWF    	SPBRG          ;
                        ;
      
      MOVLW    	B'00100000'    ;zapnuti TXEN
      MOVWF    	TXSTA         ;
      CLRF    	PIE1          ;Vypnout preruseni na periferiich
      BCF     	STATUS,RP0      ;BANKA0

                        
      MOVLW    	B'10000000'    ;***zapnuti UART portu a BEZ prijmu
      MOVWF    	RCSTA

;INICIALIZACE BEZDRATOVEHO MODULU
   
      BSF      	ENABLE         
      CALL   	WAIT20uS
      BSF      	RXTX
      CALL   	WAIT200uS
      BCF      	RXTX
      CALL   	WAIT40uS
      BCF      	ENABLE
      CALL   	WAIT20uS
      BSF      	ENABLE
      CALL   	WAIT200uS

;------------------------------------------------KONEC INICIALIZACE-------------------------------------------------------------------------      

START
	
	BCF			RCSTA,CREN
	MOVLW		D'1'		;Sleduje sa RC7 caka sa na zapnutie vysielaca 627 
	MOVWF		VYBER		; docasne pouzity

StartW	
	
	BTFSS		PORTC,7
    GOTO		START
    CALL		WAIT400uS	
	DECFSZ		VYBER
	GOTO		StartW
	
	;dost dlho je signal v stave Hi..t.j. je zapnuty vysielac 627
	
	BSF			RCSTA,CREN	; zapne sa prijem do RCREG

	BTFSS   	PIR1,RCIF      ;ceka na prichozi data (dokud neni naplnen RCREG)               
	goto		$-1		;
	
	BTFSC   	RCSTA,OERR      ;TEST CHYBY PRIJMU
	GOTO   		RESTARTPRIJMU   	;   
	BTFSC   	RCSTA,FERR      ;TEST CHYBY PRIJMU
	GOTO   		CHYBARAMEC   	;



	MOVF   		RCREG,0
	MOVWF   	VYBER
	MOVWF   	PORTB         ;zobrazeni prichozich dat na LED
    GOTO   		MENU

START_R   
   BSF      RCSTA,CREN   ; zapne sa prijem do RCREG

   BTFSS      PIR1,RCIF      ;ceka na prichozi data (dokud neni naplnen RCREG)           
   goto      $-1      ;


   BTFSC      RCSTA,OERR      ;TEST CHYBY PRIJMU
   GOTO         RESTARTPRIJMU      ;   
   BTFSC      RCSTA,FERR      ;TEST CHYBY PRIJMU
   GOTO         CHYBARAMEC      ;



   MOVF         RCREG,0
   MOVWF      VYBER
   MOVWF      PORTB         ;zobrazeni prichozich dat na LED
       GOTO         MENU

RESTARTPRIJMU
      BCF         RCSTA,4
;     BSF         RCSTA,4         ; nastavi sa po START
      GOTO      START_R
CHYBARAMEC
      MOVF      RCREG,0
      GOTO      START_R 





MENU   
        MOVLW   0X55
        SUBWF   VYBER,0           ;porovnaj  ak W==VYBER>je zero v STATUS
        BTFSC   STATUS,Z       ; Zero indikator
        GOTO    SYNCHRO        ; nie call treba odpovedat 0xAA a ide START
        MOVLW   B'00000001'
        SUBWF   VYBER,0
        BTFSC   STATUS,Z
        GOTO    PRENOSK0
        MOVLW   B'00000010'
        SUBWF   VYBER,0
        BTFSC   STATUS,Z
        GOTO    PRENOSK1
      	MOVLW   B'00000100'
        SUBWF   VYBER,0
        BTFSC   STATUS,Z
        GOTO    PRENOSK2
      	GOTO   	START


   
SYNCHRO                        ;SYNCHRONIZACE (POSLI ZPET 0xAA) (POKUD 0x55 PRIJATO A BEZ CHYB)
      	CALL   RXTOTX
        BTFSS  PIR1,TXIF         ;TESTOVANI  ABYCHOM SI TXREG NEPREPSALI
        GOTO   $-1               ;
        MOVLW  0xAA            ;odpoved
        MOVWF  TXREG
        CALL   TXTORX
      	GOTO   START           ; bude sa cakat odznova povel alebo synchro



PRENOSK0
      CALL   RXTOTX
      MOVLW  B'11000001'      ; vyber vstupniho kanalu (0) , vyber zdroje CLOCK (INTRC), zapnuti AD
      MOVWF  ADCON0         ;...
      CALL   ADPREVOD      ;zavolej zahajeni AD prevodu
      CALL   VYSILEJL      ;zavolej vysilani LsB
      CALL   VYSILEJH      ;zavolej vysilani MsB
      CALL   TXTORX
      GOTO   START

PRENOSK1
      CALL   RXTOTX
      MOVLW  B'11001001'      ; vyber vstupniho kanalu (1) , vyber zdroje CLOCK (INTRC), zapnuti AD
      MOVWF  ADCON0         ;...
      CALL   ADPREVOD      ;zavolej zahajeni AD prevodu
      CALL   VYSILEJL      ;zavolej vysilani LsB
      CALL   VYSILEJH      ;zavolej vysilani MsB
      CALL   TXTORX
      GOTO   START


   


PRENOSK2
      CALL   RXTOTX
      BTFSC  PORTC,0         ;POKUD TEPLOTA KLADNA, TAK PREVOD S NORMALNIM VREF+. POKUD ZAPORNA TAK VREF+ -5V
      GOTO   MINUSHODNOTA   
      MOVLW  B'11010001'      ; vyber vstupniho kanalu (2) , vyber zdroje CLOCK (INTRC), zapnuti AD
      MOVWF  ADCON0         ;...
      CALL   ADPREVOD      ;zavolej zahajeni AD prevodu
      CALL   VYSILEJL      ;zavolej vysilani LsB
      CALL   VYSILEJH      ;zavolej vysilani MsB
      BCF    ZNAMENKO,0      ;TEPLOTA JE KLADNA
      CALL   VYSILEJZNAMENKO
      CALL   TXTORX
      GOTO   START
      
MINUSHODNOTA
      BSF     	STATUS,RP0      ;BANKA1
      MOVLW		B'10000001'      ;NASTAVENI VREF+ RA3
      MOVWF   	ADCON1
      BCF      	STATUS,RP0
      MOVLW   	B'11010001'      ; vyber vstupniho kanalu (2) , vyber zdroje CLOCK (INTRC), zapnuti AD
      MOVWF   	ADCON0         ;...
      CALL   	ADPREVOD      ;zavolej zahajeni AD prevodu
      CALL   	VYSILEJL      ;zavolej vysilani LsB
      CALL   	VYSILEJH      ;zavolej vysilani MsB
      BSF      	ZNAMENKO,0      ;teplota je zaporna
      CALL   	VYSILEJZNAMENKO   
      BSF      	STATUS,RP0      ;BANKA1
      BCF      	ADCON1,0      ;NASTAVENI VREF+ +5V
      BCF      	STATUS,RP0      ;BANKA0
      CALL   	TXTORX
      GOTO   	START

PRENOSK3
PRENOSK4   

      GOTO   START


      
      END
