MASTER and SLAVE PICs using SPI in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,840
Hi J,
My question:
Hi J,
Are you saying that this section of the SLAVE program is not needed?
C.
================================================
Yes and set the pins used in SPI slave as input/output as needed (TRISC, TRISCD)
=========================================================
Here is a snippet from the D/S:
SPI mode allows 8 bits of data to be synchronously transmitted and received simultaneously. To accomplish communication, typically three pins are used: • Serial Data Out (SDO) • Serial Data In (SDI) • Serial Clock (SCK) Additionally, a fourth pin may be used when in a Slave mode of operation: • Slave Select (SS)

Can you verify that I don't need SPI set-up in SLAVE, please?
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,840
Master reading slave's data or slave reading master's request data byte ?
Hi J2,

Here is the SLAVE CODE:
---------------------------------------------------------------------------------------------------
SLAVE:
get_count:
'If SSPSTAT.BF = 1 Then

azi.LB = CAP2BUFL 'Read QUAD cntr, range 0000 through 3581

SSPBUF = azi.LB

Hserout "SSPBUF = ", SSPBUF, CrLf 'TEST<<<<<<<<<<<<<<
Hserout "#SSPBUF = ", #SSPBUF, CrLf 'TEST<<<<<<<<<<<<<<
Hserout "AZI = ", azi.LB, CrLf 'TEST<<<<<<<<<<<<<<
Hserout "#AZI =", #azi.LB, CrLf 'TEST<<<<<<<<<<<<<<"

'Endif
Goto get_count
---------------------------------------------------------------------------------------------------
I am receiving SSPBUF and AZI from the HSEROUT on a computer terminal, so I think the DATA is in the SLAVE registers.

Now I need the MASTER CODE to READ it, please.
C
NOTE: ( ' ) means commented out. (It was a test)
 
Last edited:

jjw

Joined Dec 24, 2013
823
Hi J,
My question:
Hi J,
Are you saying that this section of the SLAVE program is not needed?
C.
================================================

=========================================================
Here is a snippet from the D/S:
SPI mode allows 8 bits of data to be synchronously transmitted and received simultaneously. To accomplish communication, typically three pins are used: • Serial Data Out (SDO) • Serial Data In (SDI) • Serial Clock (SCK) Additionally, a fourth pin may be used when in a Slave mode of operation: • Slave Select (SS)

Can you verify that I don't need SPI set-up in SLAVE, please?
C
Of course you have to setup the slave, but not with Oshonsoft commands
, which define the io pins and their directions for the master.
To setup HW slave you set SSPSTAT, SSPCON and the i/o directions of the SPI pins, which you have already done.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Are you trying to send slave's CAP2BUFL value to Master ? If yes,

Why

Code:
azi.LB = CAP2BUFL
SSPBUF = azi.LB
?

Why not

Code:
SSPBUF = CAP2BUFL    'SSPBUF and CAP2BUFL are both 8 bits registers.
Do you want slave to send data to master when master request's data from slave or do you want slave to continuously send data to master ?
 

Thread Starter

camerart

Joined Feb 25, 2013
3,840
Are you trying to send slave's CAP2BUFL value to Master ? If yes,

Why

Code:
azi.LB = CAP2BUFL
SSPBUF = azi.LB
?

Why not

Code:
SSPBUF = CAP2BUFL    'SSPBUF and CAP2BUFL are both 8 bits registers.
Do you want slave to send data to master when master request's data from slave or do you want slave to continuously send data to master ?
Hi J2,
I've been trying different combinations of VARIABLES, REGISTERS etc, trying to get results, this was one of them.

All I want is the Quadrature encoder count 0 to 359, held in CAP2BUFL and CAP2BUFH to get into the MASTER via SPI.
C.
 

jjw

Joined Dec 24, 2013
823
Hi J2,

Here is the SLAVE CODE:

I am receiving SSPBUF and AZI from the HSEROUT on a computer terminal, so I think the DATA is in the SLAVE registers.

Now I need the MASTER CODE to READ it, please.
C
NOTE: ( ' ) means commented out. (It was a test)
-----------------------------------------------------------------------------------------------------
get_count:
'If SSPSTAT.BF = 1 Then

azi.LB = CAP2BUFL 'Read QUAD cntr, range 0000 through 3581

SSPBUF = azi.LB

Hserout "SSPBUF = ", SSPBUF, CrLf 'TEST<<<<<<<<<<<<<<
Hserout "#SSPBUF = ", #SSPBUF, CrLf 'TEST<<<<<<<<<<<<<<
Hserout "AZI = ", azi.LB, CrLf 'TEST<<<<<<<<<<<<<<
Hserout "#AZI =", #azi.LB, CrLf 'TEST<<<<<<<<<<<<<<"

'Endif
Goto get_count
---------------------------------------------------------------------------------------------------
Once again.
To read SSPBUF of the slave in the master:
SPIreceive data.
 

jjw

Joined Dec 24, 2013
823
Hi J2,
I've been trying different combinations of VARIABLES, REGISTERS etc, trying to get results, this was one of them.

All I want is the Quadrature encoder count 0 to 359, held in CAP2BUFL and CAP2BUFH to get into the MASTER via SPI.
C.
What do you get with SPIreceive azi?
In the slave program check the BF bit. If it is 1, read the SSPBUF and then write again.
Maybe the overflow flag has to be checked also.
Test first with a constant known value of azi.
 

jjw

Joined Dec 24, 2013
823
Hi J,
Ok, thanks.

Am I correct that this is the same for both PICs?

C.
You can use the Oshonsoft Basic setup commands for the master or setup HW spi also for the master.
When the master has been working before, I would not change it now, because it might cause new errors.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,840
You can use the Oshonsoft Basic setup commands for the master or setup HW spi also for the master.
When the master has been working before, I would not change it now, because it might cause new errors.
Hi J,
Am I correct that this is:
Oshonsoft Basic setup commands for the master?
'SPI
''Define SPI_CS_REG = PORTD [USED WHEN IN SLAVE MODE]
''Define SPI_CS_BIT = 4[USED WHEN IN SLAVE MODE]
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC 'not used, but left in so OSH does not burp
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
SPIPrepare

And this is: HW spi also for the master?
TRISB = %00000000 'OUT 1=YLED 2=RLED
TRISC = %01000000 '6-C/S=IN
TRISD = %00001100 '3=SCK=IN,2=SDI=IN

'SPI
SSPCON = %00100100 'SPI Control register: 5=SPI EN,3210= 0100 (SPI SLAVE,SCK PIN,SS PIN EN)
SSPSTAT = %00000000
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,840
Hi,
Perhaps it would be better if someone could EDIT the SLAVE program a #45

and this MASTER section:
-----------------------------------------------------------------------------------------------------------------
Dim azi As Word
Dim data As Byte

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

SPICSOn
azimuth = 0 'CHIP SELECT 18F4431 AZI ON

'''''''SPISend %00000000
SPIReceive data

Hserout "DATA ", #data, CrLf
azimuth = 1 'CHIP SELECT 18F4431 AZI OFF
SPICSOff

Goto main
---------------------------------------------------------------------------------------------------------------
then post them, and I will test them.
What do you think?
C.
 

jjw

Joined Dec 24, 2013
823
Hi J,
Am I correct that this is:
Oshonsoft Basic setup commands for the master?
'SPI
''Define SPI_CS_REG = PORTD [USED WHEN IN SLAVE MODE]
''Define SPI_CS_BIT = 4[USED WHEN IN SLAVE MODE]
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC 'not used, but left in so OSH does not burp
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
SPIPrepare

And this is: HW spi also for the master?
TRISB = %00000000 'OUT 1=YLED 2=RLED
TRISC = %01000000 '6-C/S=IN
TRISD = %00001100 '3=SCK=IN,2=SDI=IN

'SPI
SSPCON = %00100100 'SPI Control register: 5=SPI EN,3210= 0100 (SPI SLAVE,SCK PIN,SS PIN EN)
SSPSTAT = %00000000
C.
HW setup is wrong.
It is better to forget HW SPI now until the sw master works.
You can use the old working sw master, but check the pins which are wired to slave.

You can make a simple test to get one value slave->master
In slave write a value to SSPBUF eg. 0xAA or whatever and then stop the program
stop:
goto stop

In master cs slave
wait (1000)
SPIreceive data
Hserout data
stop: goto stop
 

Thread Starter

camerart

Joined Feb 25, 2013
3,840
HW setup is wrong.
It is better to forget HW SPI now until the sw master works.
You can use the old working sw master, but check the pins which are wired to slave.

You can make a simple test to get one value slave->master
In slave write a value to SSPBUF eg. 0xAA or whatever and then stop the program
stop:
goto stop

In master cs slave
wait (1000)
SPIreceive data
Hserout data
stop: goto stop
Hi J,
Regarding my previous question in #73:
Is either one or both of those statements true?
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,840
Use the exact setup for sw master which has worked before in compass and BMP280.
Hi J,
My question was literally to find out what 'Oshonsoft Basic setup commands for the master' and 'setup HW spi also for the master' mean?
C.
 

jjw

Joined Dec 24, 2013
823
Hi J,
My question was literally to find out what 'Oshonsoft Basic setup commands for the master' and 'setup HW spi also for the master' mean?
C.
This has been mentioned before.
Oshonsoft Basic setup is only for master and is implemented in software by "bitbanging" the pins of ioports. The function and direction of the pins are from the define commands.

HW spi is using the unit inside the PIC and is defined by SSPSTAT and SSPCON and TRIS registers. The used pins are fixed ( SDI, SDO, SCK
 

Thread Starter

camerart

Joined Feb 25, 2013
3,840
This has been mentioned before.
Oshonsoft Basic setup is only for master and is implemented in software by "bitbanging" the pins of ioports. The function and direction of the pins are from the define commands.

HW spi is using the unit inside the PIC and is defined by SSPSTAT and SSPCON and TRIS registers. The used pins are fixed ( SDI, SDO, SCK
Hi J,
I'm sorry you have to repeat yourself, but the problem is my lack of understanding about terms, e,g, 'bitbanging' and HW SPI.
C.
 
Top