MASTER and SLAVE PICs using SPI in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,730
Hi, EDITED: title changed
I am trying to use an 18F4620 as MASTER and an 18F4431 as SLAVE in Oshonsoft using SPI.

There are other SLAVES connected to the MASTER e,g, BMP280 and a Compass module, all working ok and each has a C/S (Chip select). I have added a C/S for 18F4431 to the MASTER.

Now I'm trying to set up the 18F4431, as SLAVE.
Here is a link to some setup information: https://openlabpro.com/guide/spi-module-in-pic18f4550/

Any help welcome.

Camerart
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,730
Hi,
I have a test program on the SLAVE (It maybe working at some level?) Still working on understanding the settings.

Here is a section of the MASTER program showing the REGISTER READ of the 18F4431 PIC, showing an error.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
azimuth = 0 'CHIP SELECT 18F4431 ON
SPICSOn
SPISend SSPBUF '0xfc9, 0xfc9.LB
SPIReceive SSPBUF '.LB
Hserout "SSPBUF ", #SSPBUF, CrLf '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
azimuth = 1 'CHIP SELECT 18F4431 OFF
SPICSOff
NOTE: ( ' ) = commented out.
NOTE: The HSEROUT is for checking only.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Up to now, I have READ each BYTE from a single BYTE REGISTER ADDRESS. It looks like the SSPBUF needs READing from a double BYTE REGISTER ADDRESS.
How is this made possible?
C.
 

Attachments

Last edited:

jayanthd

Joined Jul 4, 2015
945
SPISend() is to send byte value. Why are you sending address of SSPBUF register. You have to send the byte you want to send to the slave.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
SPISend() is to send byte value. Why are you sending address of SSPBUF register. You have to send the byte you want to send to the slave.
Hi J, [MASTER]
You can see that I had tried earlier tests shown after the SSPBUF test.
In the Oshonsoft protocol the SEND is for sending the REGISTER ADDRESS that is READ on a particular module. Here is a working example in the same program where the compass REGISTER is read:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''READ COMPASS 0X01 REGB
''compss = 0 'CHIP SELECT COMPASS ON
''SPICSOn
''SPISend 0x03 'COMPX ADDR READ
''SPIReceive compdata
''compss = 1 'CHIP SELECT COMPASS OFF
''compss = 0 'CHIP SELECT COMPASS ON
''SPISend 0x8e 'st2addr ADDR READ
''SPIReceive st2addr 'CHECK ST1 (COMPULSARY CLEAR)
''SPICSOff
''compss = 1 'CHIP SELECT COMPASS OFF
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
NOTE: The the 1st BIT of second SEND is ( 1 ) making it a WRITE.

I'm trying to figure it all out, so hopefully my answer is correct!
C.
 

jjw

Joined Dec 24, 2013
823
As has been said before no address is used with PIC to PIC communication.
The slave writes data, a byte variable to SSPBUF and the master reads it, SPIreceive data.

Some transducers use the addresses for different values eg. calibration values etc.
 

jayanthd

Joined Jul 4, 2015
945
@camerart

From which sensors you want to read data ? Mention all sensor part names.

For PIC to PIC SPI communication you don't need any address. You need SS or CS pin to select slave.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
As has been said before no address is used with PIC to PIC communication.
The slave writes data, a byte variable to SSPBUF and the master reads it, SPIreceive data.

Some transducers use the addresses for different values eg. calibration values etc.
Hi I,
Do you mean like this:
--------------------------------------------------------------------------------
SPICSOn
azimuth = 0 'CHIP SELECT 18F4431 ON
SPIReceive DATA
azimuth = 1 'CHIP SELECT 18F4431 OFF
SPICSOff
--------------------------------------------------------------------------------
I'm puzzled! How does the SLAVE know which kind of DATA the MASTER wants to RECEIVE from it?
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
@camerart

From which sensors you want to read data ? Mention all sensor part names.

For PIC to PIC SPI communication you don't need any address. You need SS or CS pin to select slave.
Hi J,
See #1
I have C/S PIN, called AZIMUTH in the examples.
This is PIC to PIC. In the thread there are other MODULES mentioned as examples, these are all working.
C.
 

jayanthd

Joined Jul 4, 2015
945
I had written this mikroC PRO PIC code sometime ago. If you understand c then you can refer it else I will port my code as an example to OshonSoft PIC18 Basic and post it here.

Actually my code is multi-master but if you take it one way then it is master-slave.

SPI Master Slave.png
 

Attachments

jjw

Joined Dec 24, 2013
823
Hi I,
Do you mean like this:
--------------------------------------------------------------------------------
SPICSOn
azimuth = 0 'CHIP SELECT 18F4431 ON
SPIReceive DATA
azimuth = 1 'CHIP SELECT 18F4431 OFF
SPICSOff
--------------------------------------------------------------------------------
I'm puzzled! How does the SLAVE know which kind of DATA the MASTER wants to RECEIVE from it?
C
I assumed that the 18F4431 sends only the encoder data. Does it send also something else?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
I had written this mikroC PRO PIC code sometime ago. If you understand c then you can refer it else I will port my code as an example to OshonSoft PIC18 Basic and post it here.

Actually my code is multi-master but if you take it one way then it is master-slave.

View attachment 168501
Hi J,
I don't read C, but I'll see if I can extract enough information from it, before anything else.
Thanks.
C.
 

ericgibbs

Joined Jan 29, 2010
18,840
hi C,
As I said, its the responsibility of the Slave program to load the SSPBUF with the correct data, the Master will read whatever the Slave has loaded into its SSPBUF.
E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
I assumed that the 18F4431 sends only the encoder data. Does it send also something else?
Hi J,
When I used SPI SEND RECEIVE for other modules (Not PIC) I had to send the address of the REGISTER of the DATA I wanted.
I'm not sure with PIC to PIC, but as the SSPBUF is in a particular REGISTER I assumed the MASTER program had to let the SLAVE know what it wants to RECEIVE. ???

C.
 

jayanthd

Joined Jul 4, 2015
945
Hi J,
When I used SPI SEND RECEIVE for other modules (Not PIC) I had to send the address of the REGISTER of the DATA I wanted.
I'm not sure with PIC to PIC, but as the SSPBUF is in a particular REGISTER I assumed the MASTER program had to let the SLAVE know what it wants to RECEIVE. ???

C.
I am watching T.V. now. (30 minutes). After that I will make a PIC18 Simulator IDE Basic Compiler code for you.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi C,
As I said, its the responsibility of the Slave program to load the SSPBUF with the correct data, the Master will read whatever the Slave has loaded into its SSPBUF.
E
Hi E,
I think I have programmed the SLAVE to put the Data INTO SSPBUF ok, as I also have; [ Hserout "SSPBUF = ", #SSPBUF, CrLf 'TEST<<<<<<<<<<<<<<] that I can see with a TERMINAL. (The program still needs work!)

What form does the MASTER request the DATA from the SSPBUF?
C.
 

ericgibbs

Joined Jan 29, 2010
18,840
Hi E,
I think I have programmed the SLAVE to put the Data INTO SSPBUF ok, as I also have; [ Hserout "SSPBUF = ", #SSPBUF, CrLf 'TEST<<<<<<<<<<<<<<] that I can see with a TERMINAL. (The program still needs work!)

What form does the MASTER request the DATA from the SSPBUF?
C.
hi,
D/s Pages #219 and #220 cover the Slave operation of the PIC 18F4431 d/s
Its 8bit serial transfer
E
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
I am watching T.V. now. (30 minutes). After that I will make a PIC18 Simulator IDE Basic Compiler code for you.
Hi J2,
I can only read BASIC, so if you planned to send a compiled program, I won't be able to use it, sorry.
Wait for a while.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,730
hi,
D/s Pages #219 and #220 cover the Slave operation of the PIC 18F4431 d/s
Its 8bit serial transfer
E
Hi E,
Ok, thanks, I'll give it a read.
EDIT: This appears to be USART , is it also for SPI DATA transfer?
C.
 
Last edited:
Top