MASTER and SLAVE PICs using SPI in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,838
Yes it is wrong.

Code:
SSPCON = AZIL
It should be

Code:
SSPBUF = AZIL
In my code when PIC18F4620 is master, PIC18F4421 will be slave and when PIC18F4431 is master, PIC18F4620 will be slave. I made master (PIC18F4620) send 0xFF to slave and request data.
Hi J2,
Well spotted :)
C.
 

jjw

Joined Dec 24, 2013
823
Hi J,
As already mentioned, I think I have the SLAVE set-up as the D/S and E's , msg, and it is outputting SSPCON by using a TEST HSEROUT, and I can see it in a RECEIVING Terminal.

I am trying to SEND a WORD (QEI) that is held in CAP2BUFL and CAP2BUFH
At the moment I have SSPBUF=CAP2BUFL, which is what I see in the Terminal. e,g, as I change the numbers they are RECEIVED.

--------------SLAVE--------------------------
From your message here, is this correct? [Just for the LOW BYTE TEST]
DIM AZIL as BYTE
DIM AZIH as BYTE

AZIL = CAP2BUFL
SSPBUF = AZIL

----------------MASTER------------------------------
Dim AZIL as Byte
SPIreceive AZIL

C.
Looks ok.
Show the slave setup, SSPSTAT, SSPCON
 

jayanthd

Joined Jul 4, 2015
945
I am trying to write OshonSoft Basic PIC18 code for slave (PIC18F4431) but Compiles tells that SSPCON1 is a constant and it cannot be used. I can configure SSPCON but not sure if configuring SSPCON configures SSPCON1 register.

I have to configure SSPSTAT and SSPCON1 register to set as slave or master when required.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,838
I am trying to write OshonSoft Basic PIC18 code for slave (PIC18F4431) but Compiles tells that SSPCON1 is a constant and it cannot be used. I can configure SSPCON but not sure if configuring SSPCON configures SSPCON1 register.

I have to configure SSPSTAT and SSPCON1 register to set as slave or master when required.
Hi J2,
18F4431 doesn't have SSPCON1. It does have SSPCON.
C
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
@ C,

Why have you used PORTD for SPI. To use PORTD for hardware SPI you have to remap the pins. I don't see any pin remapping code.
 

jjw

Joined Dec 24, 2013
823
Hi J,
Here is the SLAVE program:
C
These Spi commands in Oshonsoft, like SPIPrepare etc. are for the master.
You can just make TRISD = , TRISC= with correct values for input/output.
Test the communication first by writing the same known value to SSPBUF and try to read it in the master.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,838
Hi,
The Incremental encoder has just arrived:)

Does this make sense?
From the Terminal, while STEPPING using the SLAVE test buttons QEA and QEB. First upward, then downward.
C.
Hi I have figured it out.
I've changed the CAP3BUFL-H
Now it READs ok. 0 to 359
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,838
These Spi commands in Oshonsoft, like SPIPrepare etc. are for the master.
You can just make TRISD = , TRISC= with correct values for input/output.
Test the communication first by writing the same known value to SSPBUF and try to read it in the master.
Hi J,
Are you saying that this section of the SLAVE program is not needed?
C.
--------------------------------------------------------------------------------------
Define SPI_CS_REG = PORTC '[used when in slave mode]
Define SPI_CS_BIT = 6 '[used when in slave mode]
Define SPI_SCK_REG = PORTD
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTD 'not used, but left in so OSH does not burp
Define SPI_SDI_BIT = 2
Define SPI_SDO_REG = PORTD
Define SPI_SDO_BIT = 1
WaitMs 100
SPIPrepare
----------------------------------------------------------------------------------------
 

Thread Starter

camerart

Joined Feb 25, 2013
3,838
Yes and set the pins used in SPI slave as input/output as needed (TRISC, TRISCD)
Hi J,
TRIS's set as program in #45?

I'm pretty sure the SLAVE is working ok.

Now the MASTER:

I've tried lost of ideas, but none have worked so far.
C.
 

jjw

Joined Dec 24, 2013
823
TRIS's in msg#45 look ok.
How do you know that the slave works?
The master has worked with BMP280 and compass before.Have you changed something?
Check the connections and the i/o directions.
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,838
TRIS's in msg#45 look ok.
How do you know that the slave works?
The master has worked with BMP280 and compass before.Have you changed something?
Check the connections and the i/o directions.
Hi J,
I've always used the ADDRESS of the REGISTER so far, I've never used SPI with no ADDRESS
C.
Here is a section from the MASTER:

--------------------------------------------------------------------------------------------------------------------
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
 

Thread Starter

camerart

Joined Feb 25, 2013
3,838
Final project files attached.

PIC18F4431 SPI MASTER-SLAVE Code. XT 4 MHz.

mikroBasic PRO PIC Codes. See attached video. I have used PIC18F4431 for both master and slave. If you use mikroBasic PRO PIC then you can easily change it.

Master Code

Code:
program Master

' Declarations section
dim Chip_Select as sbit at RC0_bit
dim Chip_Select_Direction as sbit at TRISC0_bit

dim flagRegister as byte
dim data_ as byte
dim spiDataReceived as sbit at flagRegister.0

sub procedure interrupt
    if(SSPIF_bit) then
       SSPIF_bit = 0
  
       if(BF_bit) then
          data_ = SSPBUF
          spiDataReceived = 1
       end if
    end if

    SSPOV_bit = 0
end sub

main:
'   Main program

    TRISA = 0xC0
    TRISB = 0x00

    PEIE_bit = 1
    GIE_bit = 1

    spiDataReceived = 0

    while(1)
         if(spiDataReceived = 0) then
            TRISC = 0x10
            Chip_Select_Direction = 0
            Chip_Select = 1
            SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
            SSPIE_bit = 0
            Chip_Select = 0
            SPI1_Write(0xFF)
            Chip_Select = 1
            TRISC = 0x30
            Chip_Select_Direction = 1
            SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
            SSPIE_bit = 1
            Delay_ms(20)
         else
            LATB = data_
            spiDataReceived = 0
         end if
    wend
end.
SLAVE code

Code:
program Slave

' Declarations section
dim Chip_Select as sbit at RC0_bit
dim Chip_Select_Direction as sbit at TRISC0_bit

dim flagRegister as byte
dim data_ as byte
dim spiDataReceived as sbit at flagRegister.0

sub procedure interrupt
    if(SSPIF_bit) then
       SSPIF_bit = 0
  
       if(BF_bit) then
          data_ = SSPBUF
    
          if(data_ = 0xFF) then
              spiDataReceived = 1
          end if
    
          data_ = 0
       end if
    end if

    SSPOV_bit = 0
end sub

main:
'   Main program

    TRISA = 0xC0
    TRISB = 0x00

    TRISC = 0x30
    Chip_Select_Direction = 1
    SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
    spiDataReceived = 0
    SSPIE_bit = 1

    PEIE_bit = 1
    GIE_bit = 1

    while(1)
         if(spiDataReceived = 1) then
            SSPIE_bit = 0
            TRISC = 0x10
            Chip_Select_Direction = 0
            Chip_Select = 1
            SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
            Chip_Select = 0
            SPI1_Write(PORTB)
            Chip_Select = 1
            TRISC = 0x30
            Chip_Select_Direction = 1
            SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
            SSPIE_bit = 1
            spiDataReceived = 0
         end if
    wend
end.

View attachment 168555
Hi J and J2,
J, SLAVE CODE at #45.

So far I've only used the ADDRESS method, where others may reply with alternative ideas.
J2,
I notice your code uses 0xff as an ADDRESS, which is of course a BYTE. SSPBUF is in REGISTER FC9h, which I think is a WORD. The SPI way that Is at #56, only works with BYTEs, is there a way of putting the SSPBUF DATA into a BYTE ADDRESS.
C.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Hi J and J2,
J, SLAVE CODE at #45.

So far I've only used the ADDRESS method, where others may reply with alternative ideas.
J2,
I notice your code uses 0xff as an ADDRESS, which is of course a BYTE. SSPBUF is in REGISTER FC9h, which I think is a WORD. The SPI way that Is at #56, only works with BYTEs, is there a way of putting the SSPBUF DATA into a BYTE ADDRESS.
C.
Why are you worring about SSPBUF register's address or any byte variable or word variable's address.

For sending data you place the data (a byte) in SSPBUF
and for receiving data you read SSPBUF in to a byte variable.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,838
Why are you worring about SSPBUF register's address or any byte variable or word variable's address.

For sending data you place the data (a byte) in SSPBUF
and for receiving data you read SSPBUF in to a byte variable.
Hi J2,
You have answered my worry!
What does READ SSPBUF CODE look like in Oshonsoft SPI?
C.
 
Top