;!!! skontrolovat casovanie WAIT200uS  zda sa mi zle zpocitane konstanty
; 



      LIST P=16F627
      INCLUDE<P16F627.INC>
      __CONFIG _PWRTE_ON & _WDT_OFF & _MCLRE_OFF & _BODEN_OFF & _LVP_OFF & _HS_OSC


   
TEMP      	EQU   23H
TEMP1      	EQU   24H
K0L         EQU   25H
K0H         EQU   26H
K1L         EQU   27H
K1H         EQU   28H
K2L         EQU   29H
K2H         EQU   30H
ZNAMENKO   	EQU   31H
TMP0      	EQU   32H
TMP1      	EQU   33H
PRIJIMA	  	EQU		34H

#DEFINE      RXTX   PORTB,3
#DEFINE      ENABLE   PORTB,0


      ORG      	00H
      GOTO   	INIT
;-------------------------------ZACATEK PODPROGRAMU---------------------------------------------------------


PocitadloMinusJedna
               
        DECFSZ 	TMP0,F
        RETLW 	0XFF          ;este nepadol cas
        DECFSZ 	TMP1,F
        RETLW 	0XFF          ; este nepadol
        RETLW 	0             ;je zle padol cas

;--------------------------------------------------------------------

RXTOTX
      BTFSC   	RXTX
      RETURN
      BSF      	RXTX
      CALL   	WAIT3mS
      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      ;   -,,-
      
      BCF      	RCSTA,CREN
      
      BCF      	RXTX
      CALL   	WAIT40uS
      BCF      	ENABLE
      CALL   	WAIT20uS
      BSF      	ENABLE
      
      CALL   	WAIT200uS

;     CALL   WAIT5mS		;takto nie!! cakat sa bude s kontrolou
;     BSF      RCSTA,CREN       ; CREN sa zapne az po detekcii prijimaca  nie TU"!!

      
      RETURN

;----------------------------------------------------------------------

WAIT20uS
      MOVLW 	0x22  ;34 DEC
      MOVWF 	TMP0
      DECFSZ 	TMP0,F
      GOTO 		$-1
      RETURN

;----------------------------------------------------------------------

WAIT40uS
      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
      MOVLW 	0x03  ;3 DEC
      MOVWF 	TMP1
      MOVLW 	0x0DD  ;221 DEC
      MOVWF 	TMP0
      DECFSZ 	TMP0,F
      GOTO 		$-1
      DECFSZ 	TMP1,F
      GOTO 		$-5
      RETURN


WAIT3mS
		MOVLW 0x14  ;20 DEC
        MOVWF TMP1
        MOVLW 0x7A  ;122 DEC
        MOVWF TMP0
        DECFSZ TMP0,F
        GOTO $-1
        DECFSZ TMP1,F
        GOTO $-3
		
		RETURN
;-------------------------------KONEC PODPROGRAMU-----------------------------------------------------------
INIT      
      MOVLW   	B'00000111'
      MOVWF   	CMCON
      BSF      	STATUS,RP0      ;BANKA1
            
      MOVLW   	B'00000000'      ;VYSTUPNI PORT A
      MOVWF   	TRISA
      
      MOVLW   	B'00000110'      ;REZERVACE PORTU B,1,2 PRO SERIOVY PRENOS
      MOVWF   	TRISB
      
      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
      
      BSF      	RCSTA,SPEN      ;POVOLENI SERIOVEHO PORTU
      BCF      	RCSTA,CREN      ;POVOLENI PRIJMU ....teraz este bez prijmu

;INICIAKZACE BEZDRATOVEHO MODULU
      BSF      	ENABLE
      CALL   	WAIT20uS
      BSF      	RXTX
      CALL   	WAIT400uS

;--------------------------------------------------------------------------------------------
MOVF   RCREG
MOVF   RCREG

;---------------------------------SYNCHRONIZACE S DRUHYM PROCESOREM---------------------------
SYNCHRO
      CALL   	RXTOTX
      MOVLW   	0x55         	;POSLI 55H, TESTUJ NECHTENE PREPSANI TXREG
      BTFSS   	PIR1,TXIF       ;   -,,-
      GOTO    	$-1             ;   -,,-
      MOVWF   	TXREG           ;   -,,-
      CALL   	TXTORX
      
      
ZaciatokPrijmu0xAA

		MOVLW    0x03  ;6 DEC   ; cca 13ms?
        MOVWF    TMP1
        MOVLW    0x38  ;59 DEC
        MOVWF    TMP0
		
		CLRF	PRIJIMA	;pomocny reg


StartW	
		BTFSC	PORTB,1			; je HI na RX?
 		DECF	PRIJIMA,F		; dekremenruj 
		BTFSC	STATUS,Z		; je nula
		GOTO	Vysielac_Ide0xAA		; ano je 256x RX=HI
    
	nop				; predlzit cakacku nop-mi
	nop				; cca 34 instr. x0,5us??
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop 
	nop
	nop
	nop
	nop
	nop
	nop
	nop
        CALL    PocitadloMinusJedna      	;ODECTI 1 Z INTERVALU
        ANDLW   0XFF                      	;test W, NA RETLW
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO                		;CAS UPLYNUL neprisiel BLANK restartuj 
        GOTO    StartW                 		;CAS NEUPLYNUL



Vysielac_Ide0xAA						;dost dlho je signal v stave Hi..t.j. je zapnuty vysielac 627

		MOVLW   0x21  					;33 DEC   ;CEKEJ 50MS
        MOVWF   TMP1
        MOVLW   0x76  					;118 DEC
        MOVWF   TMP0

		BSF		RCSTA,CREN				; zapne prijem do RCREG
		MOVF	RCREG,w
CykSynchro
        BTFSC   PIR1,RCIF               
        GOTO    TestujNa0XAA         	;PRISLO ZPET AAH ?
        CALL    PocitadloMinusJedna      ;ODECTI 1 Z INTERVALU 50MS
        ANDLW   0XFF                      ;test W, NA RETLW
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO                	;CAS UPLYNUL neprisiel syncro restartuj 
        GOTO    CykSynchro            	;CAS NEUPLYNUL

TestujNa0XAA

       BTFSC   	RCSTA,OERR      			;TEST CHYBY PRIJMU
       GOTO    	RESTARTPRIJMU   			;jdi na opravu chyby prijmu
       BTFSC   	RCSTA,FERR
       GOTO    	CHYBASTOPBITU

        MOVF    RCREG,0
        MOVWF   TEMP
        MOVLW   0XAA
        SUBWF   TEMP,0
        BTFSS   STATUS,Z
        GOTO    SYNCHRO			;
        CALL   	RXTOTX
        GOTO   	PRENOSK0
      
         
RESTARTPRIJMU
      
		BCF      RCSTA,4
;      	BSF      RCSTA,4	;zapne sa ked bude treba
      	GOTO   SYNCHRO

CHYBASTOPBITU
      
		MOVF   RCREG,W
      	GOTO   SYNCHRO

;-------------------------------------------------------------------------------------------------------------------------
      
PRENOSK0
      
   
        MOVLW   B'00000001'      ;ve W je ulozena informace a MCU ji odesila pres UART ven (KANAL 0)
        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
        CALL   	TXTORX
 
ZaciatokPrenos0

		MOVLW    0x03  			;6 DEC   ; cca 13ms?
        MOVWF    TMP1
        MOVLW    0x76  			;118 DEC
        MOVWF    TMP0
		
		clrf	PRIJIMA			;pomocny reg

Pre0W	
		BTFSC	PORTB,1					; je HI na RX?
 		DECF	PRIJIMA,F				; dekremenruj 
		BTFSC	STATUS,Z				; je nula
		GOTO	Vysielac_Ide0			; ano je 256x RX=H
        
	nop									; predlzit cakacku nop-ma
	nop									; cca 34 instr. x0,5us??
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop 
	nop
	nop
	nop
	nop
	nop
	nop
	nop
        
		CALL    PocitadloMinusJedna      	;ODECTI 1 Z INTERVALU
        ANDLW   0XFF                      	;test W, NA RETLW
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO                		;CAS UPLYNUL neprisiel BLANK restartuj 
        GOTO    Pre0W                 		;CAS NEUPLYNUL

Vysielac_Ide0

        MOVLW    0x21  ;33 DEC
        MOVWF    TMP1
        MOVLW    0x76  ;118 DEC
        MOVWF    TMP0

		BSF		RCSTA,CREN	; zapne prijem do RCREG
		MOVF	RCREG,w  

CYKPRIJEM1
        BTFSC   PIR1,RCIF     
        GOTO    PRIJEM1
        CALL    PocitadloMinusJedna
        ANDLW   0XFF                   ;test W
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO              ; podol cas
        GOTO    CYKPRIJEM1        ; este nepadol
      
PRIJEM1
        BTFSC  	RCSTA,OERR      ;TEST CHYBY PRIJMU
        GOTO   	RESTARTPRIJMU   ;jdi na opravu chyby prijmu
        BTFSC  	RCSTA,FERR   
        GOTO	CHYBASTOPBITU
        MOVF   	RCREG,0
        MOVWF  	K0L            ;DAT A JEJICH ULOZENI
   
CYKPRIJEM2
        BTFSC   PIR1,RCIF     
        GOTO    PRIJEM2
        CALL    PocitadloMinusJedna
        ANDLW   0XFF                   ;test W
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO              ; podol cas
        GOTO    CYKPRIJEM2        ; este nepadol


PRIJEM2
      BTFSC   	RCSTA,OERR      ;TEST CHYBY PRIJMU
      GOTO    	RESTARTPRIJMU   ;jdi na opravu chyby prijmu
      BTFSC   	RCSTA,FERR
      GOTO   	CHYBASTOPBITU
      
      MOVF   	RCREG,0
      MOVWF   	K0H            ;DAT A JEJICH ULOZENI
      CALL   	RXTOTX
;-------------------------------------------------------------------------------------------------------------------------

PRENOSK1
      
   
      MOVLW   	B'00000010'      ;ve W je ulozena informace a MCU ji odesila pres UART ven (KANAL 0)
      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
      CALL   	TXTORX

; tu opravit podla prijmu znaku 0XAA

ZaciatokPrenos1

		MOVLW    0x03  ;6 DEC   ; cca 13ms?
        MOVWF    TMP1
        MOVLW    0x76  ;118 DEC
        MOVWF    TMP0
		
		clrf	PRIJIMA	;pomocny reg

Pre1W	btfsc	PORTB,1			; je HI na RX?
 		decf	PRIJIMA,F		; dekremenruj 
		btfsc	STATUS,Z		; je nula
		goto	Vysielac_Ide1		; ano je 256x RX=HI
    
	nop				; predlzit cakacku nop-mi
	nop				; cca 34 instr. x0,5us??
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop 
	nop
	nop
	nop
	nop
	nop
	nop
	nop
        CALL    PocitadloMinusJedna      	;ODECTI 1 Z INTERVALU
        ANDLW   0XFF                      	;test W, NA RETLW
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO                		;CAS UPLYNUL neprisiel BLANK restartuj 
        GOTO    StartW                 		;CAS NEUPLYNUL



Vysielac_Ide1
               
        MOVLW    0x21  ;33 DEC
        MOVWF    TMP1
        MOVLW    0x76  ;118 DEC
        MOVWF    TMP0

		BSF		RCSTA,CREN	; zapne prijem do RCREG
		MOVF	RCREG,w   

CYKPRIJEM3

        BTFSC   PIR1,RCIF     
        GOTO    PRIJEM3
        CALL    PocitadloMinusJedna
        ANDLW   0XFF                   ;test W
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO              ; podol cas
        GOTO    CYKPRIJEM3        ; este nepadol
      
PRIJEM3
      
		BTFSC   RCSTA,OERR      ;TEST CHYBY PRIJMU
      	GOTO    RESTARTPRIJMU   ;jdi na opravu chyby prijmu
        BTFSC   RCSTA,FERR
      	GOTO   	CHYBASTOPBITU
      
      	MOVF   	RCREG,0
      	MOVWF   K1L            ;DAT A JEJICH ULOZENI
   
CYKPRIJEM4
      	
		BTFSC   PIR1,RCIF     
        GOTO    PRIJEM4
        CALL    PocitadloMinusJedna
        ANDLW   0XFF                   ;test W
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO              ; podol cas
        GOTO    CYKPRIJEM4        ; este nepadol


PRIJEM4
      
		BTFSC   RCSTA,OERR      ;TEST CHYBY PRIJMU
      	GOTO    RESTARTPRIJMU   ;jdi na opravu chyby prijmu
      	BTFSC   RCSTA,FERR
      	GOTO   	CHYBASTOPBITU
      
      	MOVF   	RCREG,0
      	MOVWF   K1H            ;DAT A JEJICH ULOZENI
      	CALL   	RXTOTX
;-------------------------------------------------------------------------------------------------------------------------------

PRENOSK2
      
   
      	MOVLW   B'00000100'      ;ve W je ulozena informace a MCU ji odesila pres UART ven (KANAL 0)
      	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
      	CALL   	TXTORX


ZaciatokPrenos2


		MOVLW    0x03  ;6 DEC   ; cca 13ms?
        MOVWF    TMP1
        MOVLW    0x76  ;118 DEC
        MOVWF    TMP0
		
		CLRF	PRIJIMA	;pomocny reg

Pre2W	
	
		BTFSC	PORTB,1			; je HI na RX?
 		DECF	PRIJIMA,F		; dekremenruj 
		BTFSC	STATUS,Z		; je nula
		GOTO	Vysielac_Ide2		; ano je 256x RX=HI
        
	
	nop				; predlzit cakacku nop-mi
	nop				; cca 34 instr. x0,5us??
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop
	nop 
	nop
	nop
	nop
	nop
	nop
	nop
	nop
        
		CALL    PocitadloMinusJedna      	;ODECTI 1 Z INTERVALU
        ANDLW   0XFF                      	;test W, NA RETLW
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO                		;CAS UPLYNUL neprisiel BLANK restartuj 
        GOTO    Pre2W                 		;CAS NEUPLYNUL

Vysielac_Ide2

              
        MOVLW    0x21  ;33 DEC
        MOVWF    TMP1
        MOVLW    0x76  ;118 DEC
        MOVWF    TMP0
   
CYKPRIJEM5
      	
		BTFSC   PIR1,RCIF     
        GOTO    PRIJEM5
        CALL    PocitadloMinusJedna
        ANDLW   0XFF                   ;test W
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO              ; podol cas
        GOTO    CYKPRIJEM5        ; este nepadol
      
PRIJEM5
      	BTFSC   RCSTA,OERR      ;TEST CHYBY PRIJMU
      	GOTO    RESTARTPRIJMU   ;jdi na opravu chyby prijmu
        BTFSC   RCSTA,FERR
      	GOTO   	CHYBASTOPBITU      

      	MOVF   	RCREG,0
      	MOVWF  	K2L            ;DAT A JEJICH ULOZENI
   
CYKPRIJEM6
      	
		BTFSC   PIR1,RCIF     
        GOTO    PRIJEM6
        CALL    PocitadloMinusJedna
        ANDLW   0XFF                   ;test W
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO              ; podol cas
        GOTO    CYKPRIJEM6        ; este nepadol


PRIJEM6
      	BTFSC   RCSTA,OERR      ;TEST CHYBY PRIJMU
        GOTO    RESTARTPRIJMU   ;jdi na opravu chyby prijmu
        BTFSC   RCSTA,FERR
      	GOTO   	CHYBASTOPBITU      

      	MOVF   	RCREG,0
      	MOVWF   K2H            ;DAT A JEJICH ULOZENI
      

CYKPRIJEM7
      	
		BTFSC   PIR1,RCIF     
        GOTO    PRIJEM7
        CALL    PocitadloMinusJedna
        ANDLW   0XFF                   ;test W
        BTFSC   STATUS,Z   
        GOTO    SYNCHRO              ; podol cas
        GOTO    CYKPRIJEM7        ; este nepadol


PRIJEM7
      BTFSC   	RCSTA,OERR      ;TEST CHYBY PRIJMU
      GOTO    	RESTARTPRIJMU   ;jdi na opravu chyby prijmu
      BTFSC   	RCSTA,FERR
      GOTO   	CHYBASTOPBITU      

      MOVF   	RCREG,0
      MOVWF   	ZNAMENKO            ;DAT A JEJICH ULOZENI
      CALL   	RXTOTX



      GOTO   	PRENOSK0


      




      END 




