# Transfering DATA between two pics via SPI OSHONSOFT

#### camerart

##### Active Member
Is this from the master?
You have not set TRISCD for SDO in the slave.
Hi J,
Yes, from the MASTER
If you revisit #25 SLAVE I have clarified the comments, you should see that SDO is set correctly.
C.

#### jjw

##### Member
Hi J,
Yes, from the MASTER
If you revisit #25 SLAVE I have clarified the comments, you should see that SDO is set correctly.
C.
The Define SPI... override the TRIS's and now:
Trisc.6 = 0 should be input
Trisd.3 = 0 should be input
Trisd.1 = 0
Trisd.2 = 1
Remove all Spi defines and SpiPrepare from the slave program!

Last edited:

#### camerart

##### Active Member
The Define SPI... override the TRIS's and now:
Trisc.6 = 0 should be input
Trisd.3 = 0 should be input
Trisd.1 = 0
Trisd.2 = 1
Remove all Spi defines and SpiPrepare from the slave program!
Hi J,
True this is how SLAVE is set.
I have mild dyslexia, and no matter how many times I check, I see the settings as correct.

Here they are again.
NOTE (') = Commented out, so not in the program.

Perhaps, you could edit the program and I'll try your settings.

C

#### Attachments

• 2.1 KB Views: 2

#### jjw

##### Member
Remove also SPIPrepare.
The Tris's look now ok in the program #43
Check the hw pins in portc and portd.

Last edited:

#### Pommie

##### Well-Known Member
The way I remember TRISS settings is that 1 looks like I (start of Input) and so I know that setting TRISS to 1 makes it an input. You seem to be setting pins to 0 for an input.

Mike.

#### camerart

##### Active Member
Hi J and M,
I'm not sure what I'm doing regarding INs and OUTs, but I always think I'm putting 1=IN and 0=OUT.

Anyway, here's the result of the test. [HSEROUT from the MASTER] READ on a Terminal]
C.

#### Attachments

• 27.7 KB Views: 1
Last edited:

#### jjw

##### Member
Hi J and M,
I'm not sure what I'm doing regarding INs and OUTs, but I always think I'm putting 1=IN and 0=OUT.

Anyway, here's the result of the test. [HSEROUT from the MASTER] READ on a Terminal]
C.
Ok, the master and slave are working!
The ins and outs were correct in the Tris's, but the define SPI's messed them.
Now to read the 16bit azimuth, write azimuth.HB to SSBUF,
read it in the master, then azimuth. LB
Some conversation between master and slave is needed for this.
For example master could send 1 and when slave reads 1 it puts the highbyte into SSBUF, master reads it and then the same with 2 and the lowbyte

Last edited:

#### camerart

##### Active Member
Ok, the master and slave are working!
The ins and outs were correct in the Tris's, but the define SPI's messed them.
Now to read the 16bit azimuth, write azimuth.HB to SSBUF,
read it in the master, then azimuth. LB
Some conversation between master and slave is needed for this.
For example master could send 1 and when slave reads 1 it puts the highbyte into SSBUF, master reads it and then the same with 2 and the lowbyte
Hi J,
These tests are only between 18F4620 and 18F4431 when I put them into the bigger program with more SLAVEs, there needs to be a C/S for the AZIMUTH SLAVE (18F4431)

Perhaps I'll try one BYTE first. LB

NOTE: In tests I edited out SSPCON. Should it be in?
C.

Last edited:

#### jjw

##### Member
Hi J,
These tests are only between 18F4620 and 18F4431 when I put them into the bigger program with more SLAVEs, there needs to be a C/S for the AZIMUTH SLAVE (18F4431)

Perhaps I'll try one BYTE first. LB

NOTE: In tests I edited out SSPCON. Should it be in?
C.
SSPCON is needed in the slave program.

#### camerart

##### Active Member
SSPCON is needed in the slave program.
Hi J,
I'll try 2x BYTEs tomorrow.

As mentioned it will need a C/S for the 18F4431

Thanks,
C.

#### camerart

##### Active Member
Ok, the master and slave are working!
The ins and outs were correct in the Tris's, but the define SPI's messed them.
Now to read the 16bit azimuth, write azimuth.HB to SSBUF,
read it in the master, then azimuth. LB
Some conversation between master and slave is needed for this.
For example master could send 1 and when slave reads 1 it puts the highbyte into SSBUF, master reads it and then the same with 2 and the lowbyte
Hi J,
I've tried various variations, of what you suggest, but so far only 1x BYTE received.

MASTER:
If I SPISEND a number (BYTE) to the slave, so the SLAVE can act accordingly, then SPIRECEIVE 'say' AZIMUTHLB I get the number not the expected AZIMUTHLB. This is puzzling. (Can you explain how your test #24 works please.)

C.

#### jjw

##### Member
Hi J,
I've tried various variations, of what you suggest, but so far only 1x BYTE received.

MASTER:
If I SPISEND a number (BYTE) to the slave, so the SLAVE can act accordingly, then SPIRECEIVE 'say' AZIMUTHLB I get the number not the expected AZIMUTHLB. This is puzzling. (Can you explain how your test #24 works please.)

C.
In the slave read the SSPBUF until you get the number
( for example 1) then write the azi. LB into SSPBUF.
Something like this.
It reads the azi.lb once a second.

Code:
' In the master:
loop:
spisend 1    ' I dont know  if 1 should be a variable with a value 1
waitus 100 ' wait for the slave to act
hserout #azi.lb,crlf
waitms 1000
goto loop

'In the slave
loop:
' get the azi.lb from QEI here
d= SSPBUF
if d=1 then
SSBUF=azi.lb
else
goto loop
endif

#### camerart

##### Active Member
Hi j,
Will you explain what actually happens in your CODE in #24 please?
C.

#### jjw

##### Member
Hi j,
Will you explain what actually happens in your CODE in #24 please?
C.
It is a simple program to test that the data transfers between master and slave.
The master sends the numbers from 1 to 10 to slave and reads them back from the slave. The numbers coming back are shown on the terminal.

#### camerart

##### Active Member
Hi J,
following #52: Because I always seem to receive a 1 if I SPISEND 1, I used a VARIABLE (LBHB) I also avoided 1, as I could RECEIVE 1 as a COUNT, so I chose 5 for LB and 7 for HB.

Using this:
SLAVE:
get_count:
WaitMs 1000
Toggle rled
''If SSPSTAT.BF = 1 Then

lbhb = SSPBUF
If lbhb = 5 Then
SSPBUF = azi.LB
Else
Goto loop
Endif

Hserout "SSPBUF=", #SSPBUF, " ", "AZI.LB", #azi.LB, CrLf

Goto get_count

AND MASTER:

EDIT:
Dim azi As Word
Dim lbhb As Byte
lbhb = 5

SPICSOn
SPISend lbhb
WaitMs 100
Hserout "AZI.LB ", #azi.LB, CrLf
WaitMs 1000
'azim = 1 'CHIP SELECT slave OFF
SPICSOff

I got this
C.

#### Attachments

• 52.7 KB Views: 1
Last edited:

#### camerart

##### Active Member
It is a simple program to test that the data transfers between master and slave.
The master sends the numbers from 1 to 10 to slave and reads them back from the slave. The numbers coming back are shown on the terminal.
Hi j,
So far I think I have only received whatever is sent to the SLAVE, but no actual information from the SLAVE.
Thanks, C.

#### jjw

##### Member

You have goto loop, but no label loop.
The master sends lbhb, but its value is unknown.
Show both programs, master and slave.

#### camerart

##### Active Member

You have goto loop, but no label loop.
The master sends lbhb, but its value is unknown.
Show both programs, master and slave.
Hi J,
Here is the full SLAVE program: (attached)

The MASTER is a section of a long program (I'll post it if asked)
Here:
_________________________________________________________________________________________
Dim azi As Word
Dim lbhb As Byte
lbhb = 5

main: '\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
rled = 1
WaitMs 1000
rled = 0

SPICSOn
SPISend lbhb
WaitMs 100
Hserout "AZI.LB ", #azi.LB, CrLf
WaitMs 1000
'azim = 1 'CHIP SELECT slave OFF
SPICSOff

'Hserout "AZIMUTH=", #azimuth, " ", "AZIMUTH.LB=", #azimuth.LB, " ", "AZIMUTH.HB=", #azimuth.HB, CrLf

goto main
_____________________________________________________________________________________

#### Attachments

• 2.4 KB Views: 2

#### jjw

##### Member
Maybe it works, azi.lb is 0 in the slave program.

#### camerart

##### Active Member
Maybe it works, azi.lb is 0 in the slave program.
Hi J,
I'm not sure what you mean, but I added LBHB = 5 after the DIM, and nothing changed.
C.