;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;	SPIM.ASM	MPB	Ver:1.0		13-9-05
;...............................................................
;
;	SPI Master program
;	
;	Outputs clock to slave transmitter, receives BCD data 
;	and sends it to slave receiver for display
;	
;	
;	
;	
;	
;	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	PROCESSOR 16F877	; define MPU
	__CONFIG 0x3731		; XT clock (4MHz)

;	LABEL EQUATES	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	INCLUDE "P16F877.INC"	; Standard register labels 

Store	EQU	020

; Initialise ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	ORG	0		; Place machine code 
	NOP			; Required for ICD mode

	BANKSEL	TRISC		
	BCF	TRISC,5		; Serial data (SDO) output
	BCF	TRISC,3		; Serial clock (SCK) output
	BCF	TRISC,0		; Slave select (SS) output

	CLRW	SSPSTAT		; Default clock timing

	BANKSEL	PORTD
	BSF	PORTC,0		; Reset slave transmitter
	CLRF	SSPCON		; SPI master mode, clock = 1MHz


; MAIN LOOP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	
	BSF	SSPCON,SSPEN	; Enable SPI mode
again	BCF	PORTC,0		; Enable slave transmitter

	MOVWF	SSPBUF		; Rewrite buffer to start clock
waitin	BTFSS	PIR1,SSPIF	; wait for SPI interrupt
	GOTO	waitin		; for data recieved

	BCF	PIR1,SSPIF	; clear interrupt flag
	MOVF	SSPBUF,W	; read SPI buffer
	MOVWF	Store		; store BCD value

	BSF	PORTC,0		; Disable slave transmitter
	MOVWF	SSPBUF		; Reload SPI buffer

waits	BTFSS	PIR1,SSPIF	; wait for SPI interrupt
	GOTO	waits		; for data sent
	BCF	PIR1,SSPIF	; clear interrupt flag

	GOTO	again		; repeat main loop



	END 	;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

