' SPI slave program (see SPImast.bas for connections) Include "PROTON_4.INC" ' Allocate RAM Dataout Var Byte [8] 'Data out array I VAR BYTE 'loop counter SSPEN VAR SSPCON.5 'SSP Enable bit CKP VAR SSPCON.4 'Clock Polarity Select SMP VAR SSPSTAT.7 'Data input sample phase CKE VAR SSPSTAT.6 'Clock Edge Select bit SSPIF VAR PIR1.3 'interrupt flag - last bit set TRISC = %11011111 'set PORTC I/O SSPCON = %00000101 'configure SPI slave, no SS CKP = 0 'clock idle low CKE = 0 'transmit on idle to active transition SSPIF = 0 'clear SPI interrupt SMP = 0 'sample in middle of data ADCON1 = $0E 'PORTA.0 analog, rest PORTA and PORTE pins to digital Dataout[0] = "A" 'Preset output data to "ADC= " Dataout[1] = "D" Dataout[2] = "C" Dataout[3] = "=" Dataout[4] = " " Loop: SSPEN = 0 'disable/enable SSP to reset port SSPEN = 1 Gosub Letclear 'wait for byte received If SSPBUF != "?" Then Loop 'wait for ? to start conversion Adcin 0, Dataout[5] 'Read ADC channel 0, store in 6th position of string Gosub Senddata 'send "!" and string of data Goto Loop 'do it forever Senddata: Gosub Letclear 'wait until buffer ready SSPBUF = "!" 'send reply For I = 0 To 5 'loop for 6 array locations Gosub Letclear 'wait until buffer ready SSPBUF = Dataout[I] 'send array variable Next 'next location Return Letclear: If SSPIF = 0 Then Letclear 'wait for interrupt flag SSPIF = 0 'reset flag Return