

							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
NULAREG		EQU		25H
NULAREG1	EQU		26H


		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
		MOVWF 	TXREG 			;presunout 8-bit hodnotu do registru TXREG (Bank0) => nacteni dat zacina vysilani

CEKEJ	
		BSF		STATUS,RP0		;BANKA1
		BTFSS	TXSTA,1			;TESTOVANI TMRG ABYCHOM SI TXREG NEPREPSALI 
		GOTO	CEKEJ			;
		BCF		STATUS,RP0		;BANKA0
		RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

VYSILEJH		
		
		MOVF	ADRESH,W		;do W je ulozena informace a MCU ji nasledne odesila pres UART ven
		MOVWF 	TXREG 			;presunout 8-bit hodnotu do registru TXREG (Bank0) => nacteni dat zacina vysilani

CEKEJ1
		BSF		STATUS,RP0		;BANKA1
		BTFSS	TXSTA,1			;TESTOVANI TMRG ABYCHOM SI TXREG NEPREPSALI 
		GOTO	CEKEJ			;
		BCF		STATUS,RP0		;BANKA0
		RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

DELAY							;ZPOZDOVACI SMYCKA 1ms
		MOVLW 0x07  ;7 DEC
        MOVWF TMP1
        MOVLW 0x7D  ;125 DEC
        MOVWF TMP0
        DECFSZ TMP0,F
        GOTO $-1
        DECFSZ TMP1,F
        GOTO $-3
		RETURN

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TEST0
		MOVWF	NULAREG1
		BTFSC	NULAREG1,0
		GOTO	NENINULA
		BTFSC	NULAREG1,1
		GOTO	NENINULA
		BTFSC	NULAREG1,2
		GOTO	NENINULA
		BTFSC	NULAREG1,3
		GOTO	NENINULA
		BTFSC	NULAREG1,4
		GOTO	NENINULA
		BTFSC	NULAREG1,5
		GOTO	NENINULA
		BTFSC	NULAREG1,6
		GOTO	NENINULA
		BTFSC	NULAREG1,7
		GOTO	NENINULA
		GOTO	JENULA

NENINULA
		BSF		NULAREG,1
		RETURN
JENULA
		BCF		NULAREG,1
		RETURN

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SYNCHRO							;SYNCHRONIZACE (POSLI ZPET 0xAA) (POKUD 0x55 PRIJATO A BEZ CHYB)
		MOVLW	0xAA
		MOVWF	TXREG
		
		BSF		STATUS,RP0		;BANKA1
		BTFSS	TXSTA,1			;TESTOVANI TMRG ABYCHOM SI TXREG NEPREPSALI 
		GOTO	$-1				;
		BCF		STATUS,RP0		;BANKA0
		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 PORTU
		MOVWF	CMCON			;
		MOVLW	B'10000000'		;INICIALIZACE AD PREVODNIKU
		MOVWF	ADCON1			;
		MOVLW	B'00111111'		;INICIALIZACE PORTU
		MOVWF	TRISA			;
		MOVLW	B'00000111'		;
		MOVWF	TRISE			;
		MOVLW	B'00000000'		;
		MOVWF	TRISB			;
		
		CLRF	TRISC			;PortC 7,6 pro USART
		MOVLW	B'11000000'		;
		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'10010000' 	;zapnuti UART portu a prijmu
		MOVWF 	RCSTA
								
;-------------------------------------------------------------------------------------------------------------------------------		

START	BTFSC	RCSTA,OERR		;TEST CHYBY PRIJMU
		GOTO	RESTARTPRIJMU	;	
		BTFSC	RCSTA,FERR		;TEST CHYBY PRIJMU
		GOTO	RESTARTPRIJMU	;
		BTFSS	PIR1,RCIF		;ceka na prichozi data (dokud neni naplnen RCREG)
		GOTO	START			;
		MOVF	RCREG,0
		MOVWF	VYBER
		MOVWF	PORTB			;zobrazeni prichozich dat na LED
		GOTO	MENU

RESTARTPRIJMU
		BCF		RCSTA,4
		BSF		RCSTA,4
		GOTO	START

MENU	
		MOVLW	0X55
		XORWF	VYBER,0
		CALL	TEST0
		BTFSC	NULAREG,1
		CALL	SYNCHRO
		MOVLW	B'00000001'
		XORWF	VYBER,0
		CALL	TEST0
		BTFSC	NULAREG,1
		GOTO	PRENOSK0
		MOVLW	B'00000010'
		XORWF	VYBER,0
		CALL	TEST0
		BTFSC	NULAREG,1
		GOTO	PRENOSK1
		GOTO	START
	



PRENOSK0

		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	DELAY
		CALL	VYSILEJH		;zavolej vysilani MsB
		GOTO	START

PRENOSK1

		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	DELAY
		CALL	VYSILEJH		;zavolej vysilani MsB
		GOTO	START


	


PRENOSK2
PRENOSK3
PRENOSK4	

		GOTO	START


		
		END


