MASTER and SLAVE PICs using SPI in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,842
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,
Does my reply in #65 answer this?
C.
 

jjw

Joined Dec 24, 2013
823
Hi J,
I see! With the correct TRIS's, you spell it out in the program, instead of using the Oshonsoft CODE.
C
Yes in Slave, no with SW Master.
In SW master the Oshonsoft defines set the TRIS's
like define SPI_SCK_REG, define SCK_SCK_BIT, define SPI_SDI_REG etc.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Yes in Slave, no with SW Master.
In SW master the Oshonsoft defines set the TRIS's
like define SPI_SCK_REG, define SCK_SCK_BIT, define SPI_SDI_REG etc.
Hi J,
MASTER:
Here are the SPI TRIS's:
C.
---------------------------------------------------------------------------------------------------------------------------------
'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
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
SPIPrepare
-----------------------------------------------------------------------------------------------------------------
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
What do you mean?
Hi J,
In #75 you wrote this:
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 #65 I wrote this:
I am receiving SSPBUF and AZI from the HSEROUT on a computer terminal, so I think the DATA is in the SLAVE registers.
-------------------------------------------------------------------------------------------------
Does this prove that the SLAVE is putting SSPBUF and AZI into MEMORY?

Once we agree that SLAVE is working, we can concentrate on MASTER.
C.
 

jayanthd

Joined Jul 4, 2015
945
I have ported by mikroBasic PRO PIC working SPI Master-Slave communication project to OshonSoft PIC18 Basic Compiler code but I am getting Logic Contention errors on CS/SS pins in Proteus. Here I attach the complete project files. Hope somebody can fix it.

On pressing Master's button master sends byte 0xff to slave and slave upon receiving the byte and validating for 0xff sends its PORTB data to master and master receives the slave's data and displays it on its PORTB.

OshonSoft PIC18 Basic Compiler Code follows

SPI Master - PIC18F4620 @ 8MHz HS

Code:
AllDigital

Define SPI_CS_REG = PORTA
Define SPI_CS_BIT = 5
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5

SPIPrepare

Symbol button = PORTD.0

Dim x As Byte

x = 1

CMCON = 0x07
ADCON1 = 0x0f

TRISA = 0xc0
TRISB = 0x00
TRISD = 0x01

TRISC = 0x10
TRISA.5 = 0
'SPICSOff
LATA.5 = 1
SSPSTAT = 0x40
SSPCON1 = 0x22
IPR1.SSPIP = 0
PIE1.SSPIE = 0

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If button = 0 Then
            WaitMs 50
            While button = 0
            Wend
      
            TRISC = 0x10
            TRISA.5 = 0
            'SPICSOff
            LATA.5 = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x22
            PIE1.SSPIE = 0
            'SPICSOn
            LATA.5 = 0
            SPISend 0xff
            'SPICSOff
            LATA.5 = 1
            TRISC = 0x18
            TRISA.5 = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x24
            PIE1.SSPIE = 1
        Endif
    Wend
End                                          

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            LATB = SSPBUF
        Endif
    Endif

    SSPCON1.SSPOV = 0
Resume
SPI-Slave - PIC18F4431 @ 8 MHz HS

Code:
AllDigital

Define SPI_CS_REG = PORTC
Define SPI_CS_BIT = 6
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 5
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 7

SPIPrepare

Dim data As Byte
Dim spidatareceived As Bit
Dim x As Byte

x = 1

ANSEL0 = 0x00
ANSEL1 = 0x00

TRISA = 0xc0
TRISB = 0x00

TRISC = 0x30
TRISC.6 = 1
SSPSTAT = 0x40
SSPCON = 0x24
spidatareceived = 0
IPR1.SSPIP = 0
PIE1.SSPIE = 1

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If spidatareceived = 1 Then
            PIE1.SSPIE = 0
            TRISC = 0x10
            TRISC.6 = 0
            'SPICSOff
            LATC.6 = 1
            SSPSTAT = 0x40
            SSPCON = 0x22
            'SPICSOn
            LATC.6 = 0
            SPISend PORTB
            'SPICSOff
            LATC.6 = 1
            TRISC = 0x30
            TRISC.6 = 1
            SSPSTAT = 0x40
            SSPCON = 0x24
            PIE1.SSPIE = 1
            spidatareceived = 0
        Endif
    Wend

End                                          

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
    Endif

    SSPCON.SSPOV = 0
Resume


mikroBasic PRO PIC Code follows

SPI Master - PIC18F4620 @ 4MHz HS

Code:
program Master

' Declarations section
dim Chip_Select as sbit at RA5_bit
dim Chip_Select_Direction as sbit at TRISA5_bit

dim Button as sbit at RD0_bit

sub procedure interrupt
    if(SSPIF_bit) then
       SSPIF_bit = 0
    
       if(BF_bit) then
          LATB = SSPBUF
       end if
    end if

    SSPOV_bit = 0
end sub

main:
'   Main program

    CMCON = 0x07
    ADCON1 = 0x0F

    TRISA = 0xC0
    TRISB = 0x00
    TRISD = 0x01

    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

    PEIE_bit = 1
    GIE_bit = 1

    while(1)
         if(Button = 0) then
            Delay_ms(50)
            while(Button = 0)wend
            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 = 0x18
            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
         end if
    wend
end.
SPI-Slave - PIC18F4431 @ 4 MHz HS

Code:
program Slave

' Declarations section
dim Chip_Select as sbit at RC6_bit
dim Chip_Select_Direction as sbit at TRISC6_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.
 

Attachments

Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,842
I have ported by mikroBasic PRO PIC working SPI Master-Slave communication project to OshonSoft PIC18 Basic Compiler code but I am getting Logic Contention errors on CS/SS pins in Proteus. Here I attach the complete project files. Hope somebody can fix it.

On pressing Master's button master sends byte 0xff to slave and slave upon receiving the byte and validating for 0xff sends its PORTB data to master and master receives the slave's data and displays it on its PORTB.

OshonSoft PIC18 Basic Compiler Code follows

SPI Master - PIC18F4620 @ 8MHz HS

Code:
AllDigital

Define SPI_CS_REG = PORTA
Define SPI_CS_BIT = 5
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5

SPIPrepare

Symbol button = PORTD.0

Dim x As Byte

x = 1

CMCON = 0x07
ADCON1 = 0x0f

TRISA = 0xc0
TRISB = 0x00
TRISD = 0x01

TRISC = 0x10
TRISA.5 = 0
'SPICSOff
LATA.5 = 1
SSPSTAT = 0x40
SSPCON1 = 0x22
IPR1.SSPIP = 0
PIE1.SSPIE = 0

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If button = 0 Then
            WaitMs 50
            While button = 0
            Wend
       
            TRISC = 0x10
            TRISA.5 = 0
            'SPICSOff
            LATA.5 = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x22
            PIE1.SSPIE = 0
            'SPICSOn
            LATA.5 = 0
            SPISend 0xff
            'SPICSOff
            LATA.5 = 1
            TRISC = 0x18
            TRISA.5 = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x24
            PIE1.SSPIE = 1
        Endif
    Wend
End                                           

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            LATB = SSPBUF
        Endif
    Endif

    SSPCON1.SSPOV = 0
Resume
SPI-Slave - PIC18F4431 @ 8 MHz HS

Code:
AllDigital

Define SPI_CS_REG = PORTC
Define SPI_CS_BIT = 6
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 5
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 7

SPIPrepare

Dim data As Byte
Dim spidatareceived As Bit
Dim x As Byte

x = 1

ANSEL0 = 0x00
ANSEL1 = 0x00

TRISA = 0xc0
TRISB = 0x00

TRISC = 0x30
TRISC.6 = 1
SSPSTAT = 0x40
SSPCON = 0x24
spidatareceived = 0
IPR1.SSPIP = 0
PIE1.SSPIE = 1

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If spidatareceived = 1 Then
            PIE1.SSPIE = 0
            TRISC = 0x10
            TRISC.6 = 0
            'SPICSOff
            LATC.6 = 1
            SSPSTAT = 0x40
            SSPCON = 0x22
            'SPICSOn
            LATC.6 = 0
            SPISend PORTB
            'SPICSOff
            LATC.6 = 1
            TRISC = 0x30
            TRISC.6 = 1
            SSPSTAT = 0x40
            SSPCON = 0x24
            PIE1.SSPIE = 1
            spidatareceived = 0
        Endif
    Wend

End                                           

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
    Endif

    SSPCON.SSPOV = 0
Resume


mikroBasic PRO PIC Code follows

SPI Master - PIC18F4620 @ 4MHz HS

Code:
program Master

' Declarations section
dim Chip_Select as sbit at RA5_bit
dim Chip_Select_Direction as sbit at TRISA5_bit

dim Button as sbit at RD0_bit

sub procedure interrupt
    if(SSPIF_bit) then
       SSPIF_bit = 0
     
       if(BF_bit) then
          LATB = SSPBUF
       end if
    end if

    SSPOV_bit = 0
end sub

main:
'   Main program

    CMCON = 0x07
    ADCON1 = 0x0F
 
    TRISA = 0xC0
    TRISB = 0x00
    TRISD = 0x01

    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

    PEIE_bit = 1
    GIE_bit = 1

    while(1)
         if(Button = 0) then
            Delay_ms(50)
            while(Button = 0)wend
            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 = 0x18
            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
         end if
    wend
end.
SPI-Slave - PIC18F4431 @ 4 MHz HS

Code:
program Slave

' Declarations section
dim Chip_Select as sbit at RC6_bit
dim Chip_Select_Direction as sbit at TRISC6_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.
Hi J2,
A much appreciated set of work, which I'll concentrate on after some more tests, in the direction we're going at the moment.

Are you working on a similar project, or is this to help my project?

Do you have the Oshonsoft programs and Simulator? It's free for a number of tests, then payment.
C.
 

jayanthd

Joined Jul 4, 2015
945
Hi J2,
A much appreciated set of work, which I'll concentrate on after some more tests, in the direction we're going at the moment.

Are you working on a similar project, or is this to help my project?

Do you have the Oshonsoft programs and Simulator? It's free for a number of tests, then payment.
C.
Hi, I am only trying to help you with Hardware SPI OshonSoft PIC18 Simulator IDE (Basic Compiler) and OshonSoft PIC18 Basic Compiler Code.

As I had already written a mikroBasic PRO PIC working project I wanted to implement it in OshonSoft Basic.

Yes, I have all the OshonSoft Softwares both Personal and Commercial licenses.

Just now, the OshonSoft Basic code started working somewhat, that is master sends 0xff to slave and slave responds to it by sending its PORTB value to master and master displays received slave's data on its PORTB but after 1st transaction the SPI is locking.

I will try to fix it.

Here is one time working OshonSoft PIC18 Basic Compiler code.

SPI Master - PIC18F4620 @ 8 MHZ HS

Code:
AllDigital

Define SPI_CS_REG = PORTA
Define SPI_CS_BIT = 5
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5

SPIPrepare

Symbol button = PORTD.0

Dim x As Byte

x = 1

CMCON = 0x07
ADCON1 = 0x0f

TRISA = 0xc0
TRISB = 0x00
TRISD = 0x01

TRISC = 0x10
TRISA.5 = 0
SPICSOff
SSPSTAT = 0x40
SSPCON1 = 0x22
IPR1.SSPIP = 0
PIE1.SSPIE = 0

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If button = 0 Then
            WaitMs 50
            While button = 0
            Wend
          
            TRISC = 0x10
            TRISA.5 = 0
            SPICSOff
            SSPSTAT = 0x40
            SSPCON1 = 0x22
            PIE1.SSPIE = 0
            SPICSOn
            SSPBUF = 0xff
            While SSPSTAT.BF = 0
            Wend
            SSPSTAT.BF = 0
            SPICSOff
            TRISC = 0x18
            TRISA.5 = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x24
            PIE1.SSPIE = 1
        Endif
    Wend
End                                              

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            LATB = SSPBUF
        Endif
    Endif

    SSPCON1.SSPOV = 0
Resume

SPI Slave - PIC18F4431 @ 8 MHz HS

Code:
AllDigital

Define SPI_CS_REG = PORTC
Define SPI_CS_BIT = 6
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 5
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 7

SPIPrepare

Dim data As Byte
Dim spidatareceived As Bit
Dim x As Byte

x = 1

ANSEL0 = 0x00
ANSEL1 = 0x00

TRISA = 0xc0
TRISB = 0x00

TRISC = 0x30
TRISC.6 = 1
SSPSTAT = 0x40
SSPCON = 0x24
spidatareceived = 0
IPR1.SSPIP = 0
PIE1.SSPIE = 1

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If spidatareceived = 1 Then
            PIE1.SSPIE = 0
            TRISC = 0x10
            SPICSOff
            SSPSTAT = 0x40
            SSPCON = 0x22
            SPICSOn
            SSPBUF = PORTB
            While SSPSTAT.BF = 0
            Wend
            SSPSTAT.BF = 0
            SPICSOff
            TRISC = 0x07
            SSPSTAT = 0x40
            SSPCON = 0x24
            PIE1.SSPIE = 1
            spidatareceived = 0
        Endif
    Wend

End                                              

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
    Endif

    SSPCON.SSPOV = 0
Resume
 

jayanthd

Joined Jul 4, 2015
945
Fixed OshonSoft PIC18 Basic Compiler / PIC18 Simulator IDE's Basic Compiler codes follow.

Now codes are working in Proteus. Still there is Logic Contention issue on CS/SS pins but SPI communication works fine.

Check the attached video.

SPI Master - PIC18F4620 @ 8 MHz HS

Code:
AllDigital

Define SPI_CS_REG = PORTA
Define SPI_CS_BIT = 5
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5

SPIPrepare

Symbol button = PORTD.0

Dim x As Byte

x = 1

CMCON = 0x07
ADCON1 = 0x0f

TRISA = 0xc0
TRISB = 0x00
TRISD = 0x01

TRISC = 0x10
TRISA.5 = 0
SPICSOff
SSPSTAT = 0x40
SSPCON1 = 0x22
IPR1.SSPIP = 0
PIE1.SSPIE = 0

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If button = 0 Then
            WaitMs 50
            While button = 0
            Wend
         
            TRISC = 0x10
            TRISA.5 = 0
            SPICSOff
            SSPSTAT = 0x40
            SSPCON1 = 0x22
            PIE1.SSPIE = 0
            SPICSOn
            SSPBUF = 0xff
            While SSPSTAT.BF = 0
            Wend
            SSPSTAT.BF = 0
            SPICSOff
            TRISC = 0x18
            TRISA.5 = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x24
            PIE1.SSPIE = 1
        Endif
    Wend
End                                             

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            LATB = SSPBUF
        Endif
    Endif

    SSPCON1.SSPOV = 0
Resume
SPI Slave - PIC18F4431 @ 8 MHz HS

Code:
AllDigital

Define SPI_CS_REG = PORTC
Define SPI_CS_BIT = 6
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 5
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 7

SPIPrepare

Dim data As Byte
Dim spidatareceived As Bit
Dim x As Byte

x = 1

ANSEL0 = 0x00
ANSEL1 = 0x00

TRISA = 0xc0
TRISB = 0x00

TRISC = 0x70
SSPSTAT = 0x40
SSPCON = 0x24
spidatareceived = 0
IPR1.SSPIP = 0
PIE1.SSPIE = 1

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If spidatareceived = 1 Then
            PIE1.SSPIE = 0
            TRISC = 0x10
            SPICSOff
            SSPSTAT = 0x40
            SSPCON = 0x22
            SPICSOn
            SSPBUF = PORTB
            While SSPSTAT.BF = 0
            Wend
            SSPSTAT.BF = 0
            SPICSOff
            TRISC = 0x70
            SSPSTAT = 0x40
            SSPCON = 0x24
            PIE1.SSPIE = 1
            spidatareceived = 0
        Endif
    Wend

End                                             

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
    Endif

    SSPCON.SSPOV = 0
Resume
 

Attachments

jayanthd

Joined Jul 4, 2015
945
Fixed the "Logic Contentions" errors.

OshonSoft's BitBanged SPI library was causing the problem when mixed with hardware SPI code.

I totaly removed all BitBanged SPI codes.

Here are the working codes. See attached Simulation video.

OshonSoft PIC18 Simulator IDE's Basic Compiler / OshonSoft PIC18 Basic Compiler codes follows.

SPI-Master - PIC18F4620 @ 8 MHz HS

Code:
AllDigital

Symbol spi_cs = PORTA.5
Symbol button = PORTD.0

Dim x As Byte

x = 1

CMCON = 0x07
ADCON1 = 0x0f

TRISA = 0xc0
TRISB = 0x00
TRISD = 0x01

TRISC = 0x10
TRISA.5 = 0
spi_cs = 1
SSPSTAT = 0x40
SSPCON1 = 0x22
IPR1.SSPIP = 0
PIE1.SSPIE = 0

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

    While x > 0
        If button = 0 Then
            WaitMs 50
            While button = 0
            Wend
            PIE1.SSPIE = 0
            TRISC = 0x10
            TRISA.5 = 0
            spi_cs = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x22
            spi_cs = 0
            SSPBUF = 0xff
            While SSPSTAT.BF = 0
            Wend
            SSPSTAT.BF = 0
            spi_cs = 1
            TRISC = 0x18
            TRISA.5 = 1
            SSPSTAT = 0x40
            SSPCON1 = 0x24
            PIE1.SSPIE = 1
        Endif
    Wend
End                                            

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            LATB = SSPBUF
        Endif
    Endif

    SSPCON1.SSPOV = 0
Resume
SPI-Slave - PIC18F4431 @ 8 MHz HS

Code:
AllDigital

Dim data As Byte
Dim spidatareceived As Bit
Dim x As Byte

Symbol spi_cs = LATC.6

x = 1

ANSEL0 = 0x00
ANSEL1 = 0x00

TRISA = 0xc0
TRISB = 0x00

SSPSTAT = 0x40
SSPCON = 0x24
TRISC = 0x70
IPR1.SSPIP = 0
PIE1.SSPIE = 1

INTCON.PEIE = 1
INTCON.GIEH = 1
INTCON.GIEL = 1
INTCON.GIE = 1

spidatareceived = 0

    While x > 0
        If spidatareceived = 1 Then
            PIE1.SSPIE = 0
            TRISC = 0x10
            spi_cs = 1
            SSPSTAT = 0x40
            SSPCON = 0x22
            spi_cs = 0
            SSPBUF = PORTB
            While SSPSTAT.BF = 0
            Wend
            SSPSTAT.BF = 0
            LATC.6 = 1
            spi_cs = 1
            TRISC = 0x70
            SSPSTAT = 0x40
            SSPCON = 0x24
            PIE1.SSPIE = 1
            spidatareceived = 0
        Endif
    Wend

End                                            

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
    Endif

    SSPCON.SSPOV = 0
Resume

SS1.png
 

Attachments

jjw

Joined Dec 24, 2013
823
Hi J,
In #75 you wrote this:
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 #65 I wrote this:
I am receiving SSPBUF and AZI from the HSEROUT on a computer terminal, so I think the DATA is in the SLAVE registers.
-------------------------------------------------------------------------------------------------
Does this prove that the SLAVE is putting SSPBUF and AZI into MEMORY?

Once we agree that SLAVE is working, we can concentrate on MASTER.
C.
I will never agree that the Slave is working by looking at code :)
Slave is not putting SSPBUF in memory.
SSPBUF is a register in the SPI hardware.
Your program can read its content to a variable or write to it from a variable ( or constant? )
That is not a problem.
I think you don't have to concentrate on master.
It has been tested many times in LCD, BMP280, Compass code.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Hi J2,
Just remembered!
The Oshonsoft Simulator doesn't work with QEI and PWM. Eric Gibbs wrote a section of CODE for the PWM, if you want me to dig it out.

It is possible that the problems in this thread maybe caused by Oshonsoft failures, this is why I would like verification that SSPBUF is filling with DATA.
C.
 

jayanthd

Joined Jul 4, 2015
945
@C,

Send your test codes and circuit. I will test it in Proteus. I can't test it in OshonSoft's PIC18 Simulator IDE as I don't know how to test hardware SPI in it.

Here is final code. Now tell me what exact Hardware SPI Master-Slave communication's OshonSoft PIC18 Basic Compiler codes you want and I will write it for you.

OshonSoft PIC18 Simulator IDE's Basic Compiler / OshonSoft PIC18 Basic Compiler codes follows.

SPI-Master - PIC18F4620 @ 8 MHz HS

Code:
AllDigital

Symbol spi_cs = PORTA.5
Symbol spi_cs_direction = TRISA.5

Symbol button = PORTD.0

Dim x As Byte

    x = 1

    initialize_ports
    initialize_spi_master
    enable_interrupts

    While x > 0
        If button = 0 Then
            WaitMs 50
            While button = 0
            Wend
          
            initialize_spi_master
            send_spi_data 0xff
            initialize_spi_slave
        Endif
    Wend
End                                              

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            LATB = SSPBUF
        Endif
    Endif

    SSPCON1.SSPOV = 0
Resume                                          

Proc initialize_ports()
    CMCON = 0x07
    ADCON1 = 0x0f

    TRISA = 0xc0
    TRISB = 0x00
    TRISD = 0x01
End Proc                                        

Proc initialize_spi_master()
    TRISC = 0x10
    spi_cs_direction = 0
    spi_cs = 1
    SSPSTAT = 0x40
    SSPCON1 = 0x22
    IPR1.SSPIP = 0
    PIE1.SSPIE = 0
End Proc                                        

Proc initialize_spi_slave()
    TRISC = 0x18
    spi_cs_direction = 1
    SSPSTAT = 0x40
    SSPCON1 = 0x24
    PIE1.SSPIE = 1
End Proc                                        

Proc enable_interrupts()
    INTCON.PEIE = 1
    INTCON.GIEH = 1
    INTCON.GIEL = 1
    INTCON.GIE = 1
End Proc                                        

Proc send_spi_data(data As Byte)
    spi_cs = 0
        SSPBUF = data
  
        While SSPSTAT.BF = 0
        Wend
  
        SSPSTAT.BF = 0
    spi_cs = 1
End Proc
SPI-Slave - PIC18F4431 @ 8 MHz HS

Code:
AllDigital

Dim data As Byte
Dim spidatareceived As Bit
Dim x As Byte

Symbol spi_cs = LATC.6
Symbol spi_cs_direction = TRISC.6


    initialize_ports
    initialize_variables
    initialize_spi_slave
    enable_interrupts

    While x > 0
        If spidatareceived = 1 Then
            initialize_spi_master
            send_spi_data PORTB
            initialize_spi_slave
            spidatareceived = 0
        Endif
    Wend

End                                              

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
    Endif

    SSPCON.SSPOV = 0
Resume                                          

Proc initialize_ports()
    ANSEL0 = 0x00
    ANSEL1 = 0x00

    TRISA = 0xc0
    TRISB = 0x00
End Proc                                        

Proc initialize_variables()
    x = 1
    spidatareceived = 0
End Proc                                        

Proc initialize_spi_master()
    PIE1.SSPIE = 0
    TRISC = 0x10
    spi_cs = 1
    SSPSTAT = 0x40
    SSPCON = 0x22
End Proc                                        

Proc initialize_spi_slave()
    SSPSTAT = 0x40
    SSPCON = 0x24
    TRISC = 0x70
    IPR1.SSPIP = 0
    PIE1.SSPIE = 1
End Proc                                        

Proc send_spi_data(data As Byte)
    spi_cs = 0
        SSPBUF = data
          
        While SSPSTAT.BF = 0
        Wend
          
        SSPSTAT.BF = 0
    spi_cs = 1
End Proc                                        

Proc enable_interrupts()
    INTCON.PEIE = 1
    INTCON.GIEH = 1
    INTCON.GIEL = 1
    INTCON.GIE = 1
End Proc
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
I will never agree that the Slave is working by looking at code :)
Slave is not putting SSPBUF in memory.
SSPBUF is a register in the SPI hardware.
.
Hi J,

Doesn't this prove it is putting into the SSPBUF memory:
SLAVE
Hserout "#SSPBUF = ", #SSPBUF, CrLf

I can see this on a computer terminal.
C.
 

jayanthd

Joined Jul 4, 2015
945
Hi C,

Can you test this code ?

Master requests continuous data from slave and receives it and displays it on its PORTB.

Continuous data reception from slave.

In my Slave's code if you change this line

Code:
send_spi_data PORTB
to this

Code:
send_spi_data CAP2BUFL
then you get slave's CAP2BUFL data on master's PORTB.

OshonSoft PIC18 Simulator IDE's Basic Compiler / OshonSoft PIC18 Basic Compiler codes follows.

SPI-Master - PIC18F4620 @ 8 MHz HS

Code:
AllDigital

Symbol spi_cs = PORTA.5
Symbol spi_cs_direction = TRISA.5

Symbol button = PORTD.0

Dim x As Byte

    x = 1

    initialize_ports
    initialize_spi_master
    enable_interrupts

    While x > 0
        initialize_spi_master
        send_spi_data 0xff
        initialize_spi_slave
        WaitMs 100
    Wend
End                                             

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            LATB = SSPBUF
        Endif
    Endif

    SSPCON1.SSPOV = 0
Resume                                         

Proc initialize_ports()
    CMCON = 0x07
    ADCON1 = 0x0f

    TRISA = 0xc0
    TRISB = 0x00
    TRISD = 0x01
End Proc                                       

Proc initialize_spi_master()
    TRISC = 0x10
    spi_cs_direction = 0
    spi_cs = 1
    SSPSTAT = 0x40
    SSPCON1 = 0x22
    IPR1.SSPIP = 0
    PIE1.SSPIE = 0
End Proc                                       

Proc initialize_spi_slave()
    TRISC = 0x18
    spi_cs_direction = 1
    SSPSTAT = 0x40
    SSPCON1 = 0x24
    PIE1.SSPIE = 1
End Proc                                       

Proc enable_interrupts()
    INTCON.PEIE = 1
    INTCON.GIEH = 1
    INTCON.GIEL = 1
    INTCON.GIE = 1
End Proc                                       

Proc send_spi_data(data As Byte)
    spi_cs = 0
        SSPBUF = data
 
        While SSPSTAT.BF = 0
        Wend
 
        SSPSTAT.BF = 0
    spi_cs = 1
End Proc
SPI-Slave - PIC18F4431 @ 8 MHz HS

Code:
AllDigital

Dim data As Byte
Dim spidatareceived As Bit
Dim x As Byte

Symbol spi_cs = LATC.6
Symbol spi_cs_direction = TRISC.6


    initialize_ports
    initialize_variables
    initialize_spi_slave
    enable_interrupts

    While x > 0
        If spidatareceived = 1 Then
            initialize_spi_master
            send_spi_data PORTB
            initialize_spi_slave
            spidatareceived = 0
        Endif
    Wend

End                                             

On Low Interrupt
    If PIR1.SSPIF Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
    Endif

    SSPCON.SSPOV = 0
Resume                                         

Proc initialize_ports()
    ANSEL0 = 0x00
    ANSEL1 = 0x00

    TRISA = 0xc0
    TRISB = 0x00
End Proc                                       

Proc initialize_variables()
    x = 1
    spidatareceived = 0
End Proc                                       

Proc initialize_spi_master()
    PIE1.SSPIE = 0
    TRISC = 0x10
    spi_cs = 1
    SSPSTAT = 0x40
    SSPCON = 0x22
End Proc                                       

Proc initialize_spi_slave()
    SSPSTAT = 0x40
    SSPCON = 0x24
    TRISC = 0x70
    IPR1.SSPIP = 0
    PIE1.SSPIE = 1
End Proc                                       

Proc send_spi_data(data As Byte)
    spi_cs = 0
        SSPBUF = data
         
        While SSPSTAT.BF = 0
        Wend
         
        SSPSTAT.BF = 0
    spi_cs = 1
End Proc                                       

Proc enable_interrupts()
    INTCON.PEIE = 1
    INTCON.GIEH = 1
    INTCON.GIEL = 1
    INTCON.GIE = 1
End Proc
 
Top