PDA

View Full Version : SPI Problem?



NatureTech
- 9th January 2007, 10:46
Hi everyone.I have some problem with SPI which i wanna send data from Master PIC (16F877) with no SS to Slave PIC.I tried transfering data from slave pic to master pic and it worked but when i try to send data from master pic to slave + when i try to read SSPBUF,it shows only "0" or "63" which the symbol "!".How to do that?

NatureTech
- 11th January 2007, 07:56
Nobody answer?.

Actually, i tried and it worked when i use slave to send data to master and i know about the clock's and other setup as well from the datasheet which specific in "registers".But when i try to send data from master pic to slave pic,dang!it won't work?.

sougata
- 12th January 2007, 04:39
Hi,

I didn't get your query? Are you doing a Shiftout or directly setting up your hardware peripheral ?

NatureTech
- 12th January 2007, 09:57
Hi Soug,

Yes,i use the hardware setting.Shiftin/out ...yeah...it can work and used it before but plan not to do it again because i really love to know and learn the hardware version itself.You tried it before?.

NatureTech
- 14th January 2007, 08:43
The MASTER PIC CODE look like this:

@_config_HS_OSC



DEFINE OSC 16


TRISA = %00000000 'Set All Output
TRISB = %00000000 'Set All Output
TRISC = %00010000 'Set PORT.C 4 as Input (SDI)
TRISD = %00000000 'Set All Output
TRISE = %00000000 'Set All Output


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 'SPI interrupt flag


i VAR BYTE 'loop counter
j VAR byte[8] 'Another 1 for testing
a VAR byte[6] ' Holds 6 character [reserved]

PORTB.7 = 0:PORTB.6 = 0


j[0] = 1 ' For testing only.Presets.
j[1] = 2
j[2] = 3


ADCON1 = 7 'All PORT Digital.


SSPEN = 1 'enable SPI pins
CKP = 0 'clock idle low
CKE = 0 'transmit on idle to active transition
SSPIF = 0 'clear buffer full status
SMP = 0 'sample in middle of data
loop:


gosub senddata 'initiate conversion and send data.
pause 10
goto loop



senddata:


SSPBUF = "?"
GOSUB letclear
IF (SSPBUF <> "!") THEN senddata '"?" Received

For i = 0 to 5 'loop for 6 characters
SSPBUF = 0 'write to SSPBUF to start clock
GoSub letclear 'wait for receipt
SSPBUF = j[i] 'store received character in array

Next i 'get next character


return





letclear:

IF SSPIF = 0 Then letclear 'wait for SPI interupt flag
pauseus 25 'wait for settle and fudge factor
SSPIF = 0 'reset flag


Return


The SLAVE PIC CODE look like this:


@_config_HS_OSC

define OSC 16

TRISA = %00000000
TRISB = %00000000
TRISC = %00011000 'set PORTC I/O
TRISD = %00000000
TRISE = %00000000


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


a VAR BYTE[6] 'Holds 6 characters of data.
i VAR BYTE 'loop counter


LCD var PORTB.0 'LCD serial output.

PORTB.7 = 0 'LED 1
PORTB.6 = 0 'LED 2

LCd = 0

ADCON1 = 7 'All PORT Digital.
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






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
gosub done
gosub getdata

getdata:

SSPBUF = "!"

for i = 0 to 5
gosub letclear
a[i] = SSPBUF
gosub display

next i

return


letclear:

IF SSPIF = 0 Then letclear 'wait for interrupt flag SSPIF = 0 'reset flag

Return



Display:

lcd = 1
pause 100

serout2 lcd,396,[12] 'Refresh LCD.
serout2 lcd,396,[":",dec SSPBUF]
pause 500

goto loop



done:

PORTB.7 = 1:pause 500:PORTB.7 = 0
PORTB.6 = 0:pause 500:PORTB.6 = 1
pause 200

goto loop




SO WHAT WOULD BE THE PROBLEM??...

WAUWEEE....