
;SPI komunikace 2xPIC16F690(Master)

;****************************************************************
		include	"P16F690.INC"
		__CONFIG _CP_OFF & _PWRTE_ON & _WDTE_OFF & _INTRC_OSC_NOCLKOUT
;*******************************************************************
;			Definice 
;*****************************************************************
#define SS PORTC,6

tmp1 equ 20h
tmp2 equ 21h
;***************************************************************
	org 000h
	goto Start
;*******************************************************************
; Podprogramy
;******************************************************************
delay1ms			;fosc=4MHz T = 1/(fosc/4) = 1us
;delay 1000cy(1ms)
	movlw .2
	movwf tmp2
	movwf .75		
	movwf tmp1
	decfsz tmp1,F
	goto $-1
	decfsz tmp2,F
	goto $-5
	return
;*******************************************************************
Start 
	bsf SS				;SS = 1
	bsf STATUS,RP0		;bank1
	movlw b'01100001'	;vnitřní osc 4MHz
	movwf OSCCON
	movlw b'00111111'	;SS-výstup,SDO-výstup
	movwf TRISC			;TRISC
	movlw b'10111111'	;SCK-výstup,SDI-vstup
	movwf TRISB			;TRISB
	bsf SSPSTAT,SMP		;vzorkování bitů SPI(Master)
	bcf SSPSTAT,BF
	bcf STATUS,RP0		;bank0
	bsf	SSPCON,SSPEN	;zapnutí SPI
	bsf	SSPCON,SSPM1	;fosc/64

	bsf STATUS,IRP		;bank2
	bsf STATUS,RP1
	clrf ANSEL			;vypnutí
	clrf ANSELH			;analogových stupů
	bcf STATUS,IRP
	bcf STATUS,RP1		;bank0
	
	
Opak	
	bcf	SS				;výběr Slave negSS = 0

	bsf STATUS,RP0		;bank1
	btfsc SSPSTAT,BF	;test BF = 0
	goto $-1			;BF = 1
	bcf STATUS,RP0		;bank0

	movf PORTC,W		;načti stav portu
	andlw b'00111111'	;maskuj bity 6 a 7
	movwf SSPBUF		;ulož stav do 

	bsf STATUS,RP0		;bank1
	btfss SSPSTAT,BF	;test BF = 1
	goto $-1			;BF = 0
	bcf STATUS,RP0		;bank0

	bsf SS				; negSS = 1
	movf SSPBUF,W		;nulování bitu BF

	call	delay1ms	;čas na zpracování přijatých dat v obvodu Slave

	goto	Opak
;*******************************************************************
	end