Datasheet

PIC18F97J94 FAMILY
DS30575A-page 370 2012 Microchip Technology Inc.
EXAMPLE 20-2: 512-BYTE SPI MASTER MODE INIT AND TRANSFER
;For this example, let's use RP3(RA3) for SCK1,
;RP1(RA1) for SDO1, and RP0(RA0) for SDI1
;Let’s use SPI master mode, CKE = 0, CKP = 0,
;without using slave select signalling.
InitSPIPins:
movlb 0x0E ;Select bank 14, for access to ODCON1 register
bcf ODCON1, SSP1_OD ;Let’s not use open drain outputs in this example
bcf LATA, RA3 ;Initialize our (to be) SCK1 pin low (idle).
bcf LATA, RA1 ;Initialize our (to be) SDO1 pin to an idle state
bcf TRISA, RA1 ;Make SDO1 output, and drive low
bcf TRISA, RA3 ;Make SCK1 output, and drive low (idle state)
bsf TRISA, RA0 ;SDI2 is an input, make sure it is tri-stated
;Now we should unlock the PPS-Lite registers, so we can
;assign the MSSP2 functions to our desired I/O pins.
movlb 0x0F ;Select bank 15 for access to PPS-Lite registers
bcf INTCON, GIE ;I/O Pin unlock sequence will not work if CPU
;services an interrupt during the sequence
movlw 0x55 ;Unlock sequence consists of writing 0x55
movwf EECON2 ;and 0xAA to the EECON2 register.
movlw 0xAA
movwf EECON2
bcf OSCCON2, IOLOCK ;We may now write to RPINRx and RPORx registers
bsf INTCON, GIE ;May now turn back on interrupts if desired
movlw 0x00 ;RP0 will be SDI1
movwf RPINR8-9 ;Assign the SDI1 function to pin RP0
movlw 0x30 ;Let’s assign SCK1 output to pin RP3
movwf RPOR2_3 ;RPOR2_3 maps output signals to RP3 pin
movlw 0x00 ;
SCK1 also needs to be configured as an input on the same pin
movwf RPINR8_9 ;SCK1 input function taken from RP3 pin
movlw 0x40 ;0x40 is SDO1 output
movwf RPOR0_1 ;Assign SDO1 output signal to the RP1 (RA1) pin
movlb 0x0F ;Done with PPS-Lite registers, bank 15 has other SFRs
InitMSSP2:
clrf SSP1STAT ;CKE = 0, SMP = 0 (sampled at middle of bit)
movlw b'00000000' ;CKP = 0, SPI Master mode, Fosc/4
movwf SSP1CON1 ;MSSP2 initialized
bsf SSP1CON1, SSPEN ;Enable the MSSP2 module
InitSPIDMA:
movlw b'00111010' ;Full duplex, RX/TXINC enabled, no SSCON
movwf DMACON1 ;DLYINTEN is set, so DLYCYC3:DLYCYC0 = 1111
movlw b'11110000' ;Minimum delay between bytes, interrupt
movwf DMACON2 ;only once when the transaction is complete
;Somewhere else in our project, lets assume we have
;allocated some RAM for use as SPI receive and
;transmit buffers.