;*************************************************************************
;sync_ser_demo
;Program to demonstrate MSSP serial output.
;Program sends same two digits repeatedly from serial port, with delay.
;serial data appears on Port C bit 5, serial clock on Port C bit 3. 
;2.7.05. TJW										Tested 2.7.05
;*************************************************************************
;Clock is 4MHz
;
	#include p16f873A.inc
      list p=16F873A
      
;Set Configuration Word: crystal oscillator HS, WDT off, 
;     power-up timer on, code protect off, LV Program off.
	__CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _LVP_OFF

;Define memory locations
delcntr1    equ 20

      org  0000
;Initialise
	bcf	status, rp1
	bsf	status,rp0 	;select memory bank 1
	movlw	B'00001010'	;set port bits according to their function, although most not used
	movwf	trisa		
	movlw	B'11001000'
	movwf	trisb		;also port B bits
	movlw	B'10000000' ;Set port C bits, SDO and SCK set as op
	movwf	trisc		  ;(SDI controlled by SPI module, so leave)
	movlw	B'01000000'	;Set SMP = 0, CKE = 1
	movwf	sspstat		
	movlw	B'00000110'
	movwf	adcon1		;set port A for digital function
;
	bcf		status,rp0 	;select bank 0
	movlw	B'00110001' ;enable serial port, no write collision detect, no receive overflow, 
	movwf	sspcon		  ;clock idles high, & is fosc/16, master mode
							
;Switch all outputs off
	clrf 	porta
	clrf 	portb
	clrf 	portc
loop	movlw	B'11010101'
	movwf	sspbuf	
	call	delay40u
	movlw	B'00101010'
	movwf	sspbuf	
	call	delay40u
	goto loop
;
;**********************************************
;SUBROUTINES
;**********************************************
;
;introduces delay of 40us approx
delay40u  movlw 	D'10'	;15 cycles called, 
;			each taking 4us
        movwf  	delcntr1
del1    nop	;4 inst cycles in this loop, ie 4us
        decfsz 	delcntr1,1
        goto 	del1
        return
	end
