Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4787

SPI and example spislave.pbp

$
0
0
We have two pics talking using hardware SPI based on the spislave.pbp example.

Code:

' SETUP CHIP CLOCK SPEED
DEFINE OSC 20

' Set all analog ports on 18F2420 to digital operation
ADCON1 = 15                                       

' Allocate RAM
SSPEN      VAR SSPCON1.5  ' SSP Enable bit
CKP        VAR SSPCON1.4  ' Clock Polarity Select
SSPOV      VAR SSPCON1.6  ' Receive Overflow Indicator bit
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
SPIdata    var byte        ' data sent via SPI

TRISC = %11011111          ' set PORTC I/O
SSPCON1 = %00000101        ' configure SPI slave, no SS           
CKP = 0                    ' clock idle low
CKE = 0                    ' transmit on idle to active transition
SMP = 0                    ' sample in middle of data
SSPIF = 0                  ' clear SPI interrupt
SSPEN = 0                  ' disable/enable SSP to reset port
SSPEN = 1                  ' enable SSP
SPIdata = 0                ' zero out sending variable data

SPIdata = 121              ' test data

mainloop:
  GoSub senddata          ' send test string of data
  GOSUB DELAY              ' test delay
GoTo mainloop              ' do it forever

delay:
  pause 20                ' pause for too long here causes problems with SPIdata
return
               
senddata:
  gosub letclear          ' wait until buffer ready
  SSPBUF = SPIdata        ' send SPIdata
Return
               
letclear:
  IF SSPIF = 0 Then letclear ' wait for interrupt flag
  SSPIF = 0                ' reset flag
Return

End

Ref...
http://melabs.com/samples/PBP-mixed/spislave.htm

The slave pic simply needs to send data, whenever the master asks for it. But we noticed if you have too long of a delay in the slave pic then the data sent can be 255. Any ideas why the longer delay would cause this (see delay in above example)? The master PIC poles the Slave pic 25 times a second.

Thank you,

Rodney

Viewing all articles
Browse latest Browse all 4787

Trending Articles