ME Labs, Inc.
719-520-5323
 
Home:
  Developer Resources:

Programming Clues
    Sample Programs
   
    PICBASIC PRO™
Compiler Manual
    PICBASIC™ Compiler
Manual
    Serin2/Serout2 Modes
    ASCII Character Set
    Number Conversion
    Floating Point
Routines
    PBP Debug Monitor
    Articles and Tutorials

Hardware Clues
    Parts / Vendor List
    PICPROTO™ Boards
    LAB-X1 Docs
    LAB-X2 Docs
    LAB-X20 Docs
    LAB-X3 Docs
    LAB-X4 Docs
    LAB-XUSB Docs
    LAB-XT Docs
     
 

ME Labs, Inc. | 1-719-520-5323 | Example Program - SPISLAVE.pbp

PicBasic Pro program SPI slave program (see spimast.pbp for connections) Common ground is required.
01' Name        : SPISLAVE.pbp
02' Compiler    : PICBASIC PRO Compiler 2.6
03' Assembler   : PM or MPASM
04' Target PIC  : 40-pin 16F or 18F
05' Hardware    : Lab-X1, Lab-X2 or similar
06' Oscillator  : 4MHz internal or external
07' Keywords    : ADCIN
08' Description : PicBasic Pro program SPI slave program (see spimast.pbp for connections)
09' Common ground is required.
10'
11 
12' Allocate RAM
13dataout VAR BYTE[8]   ' Data out array
14SSPEN   VAR SSPCON.5  ' SSP Enable bit
15CKP     VAR SSPCON.4  ' Clock Polarity Select
16SMP     VAR SSPSTAT.7 ' Data input sample phase
17CKE     VAR SSPSTAT.6 ' Clock Edge Select bit
18SSPIF   VAR PIR1.3    ' interrupt flag - last bit set
19i       VAR BYTE      ' loop counter
20a       VAR BYTE[6]   ' Holds 6 characters of data
21 
22   TRISC = %11011111  ' set PORTC I/O
23   SSPCON = %00000101 ' configure SPI slave, no SS           
24   CKP = 0            ' clock idle low
25   CKE = 0            ' transmit on idle to active transition
26   SSPIF = 0          ' clear SPI interrupt
27   SMP = 0            ' sample in middle of data
28 
29   ADCON1 = $0e       ' PORTA.0 analog, rest PORTA and PORTE pins to digital
30 
31   dataout[0] = "A"   ' Preset output data to "ADC= "
32   dataout[1] = "D"
33   dataout[2] = "C"
34   dataout[3] = "="
35   dataout[4] = " "
36 
37mainloop:
38   SSPEN = 0          ' disable/enable SSP to reset port
39   SSPEN = 1
40   GoSub letclear     ' wait for byte received
41   IF (SSPBUF <> "?") Then mainloop ' wait for ? to start conversion
42 
43   ADCIN 0, dataout[5]' Read ADC channel 0, store in 6th position of string
44   GoSub senddata     ' send "!" and string of data
45   GoTo mainloop      ' do it forever
46                 
47senddata:
48   GoSub letclear     ' wait until buffer ready
49   SSPBUF = "!"       ' send reply
50 
51   For i = 0 to 5     ' loop for 6 array locations
52       GoSub letclear ' wait until buffer ready
53       SSPBUF = dataout[i] ' send array variable
54   Next i             ' next location
55   Return
56                 
57letclear:
58   IF SSPIF = 0 Then letclear ' wait for interrupt flag
59   SSPIF = 0                  ' reset flag
60   Return
61 
62   End

Download the program file.