MASTER and SLAVE PICs using SPI in Oshonsoft

jayanthd

Joined Jul 4, 2015
945
I added UART code to master as slave's UART pins are shared with SPI pins.

Master's UART prints the SSPBUF (LATB in my code) value. See attached video.

No need fro Slave's PORTD 8x Leds or Master's PORTB 8x Leds. Use Master's UART to print the received data.

Latest codes attached. Only changes to master's code (UART code added).

SS7.png
 

Attachments

Thread Starter

camerart

Joined Feb 25, 2013
3,842
I added UART code to master as slave's UART pins are shared with SPI pins.

Master's UART prints the SSPBUF (LATB in my code) value. See attached video.

No need fro Slave's PORTD 8x Leds or Master's PORTB 8x Leds. Use Master's UART to print the received data.

Latest codes attached. Only changes to master's code (UART code added).

View attachment 168840
Hi J2,
The reason SPI was chosen was as an alternative to using the UART, as it is already used to the full.
Thanks for the idea.
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
SPI should be connected as #122 and also QE. Master's UART to PC (Computer) using TTL to USB adpater.
Hi J2,
UART ok, I follow.

Is there a reason that you chose the alternative PINs for SPI? All of my test programs up to now, and the wiring is using the normal SPI PINs. I can change them if necessary.
C.

Alternative.jpg
 
Last edited by a moderator:

jayanthd

Joined Jul 4, 2015
945
Hi J2,
All of my test programs up to now, and the wiring is using the normal SPI PINs. I can change them if necessary.
C.
For slave or master ?

Which pins are you using for Master's and Slave's SPIs ? Mention PORT bit names.

I am just using the actual hardware SPI pins.

Edit:

Okay, I see. There are alternate SPI pins. To you the default pins we have to configure CONFIG3L register and I have not done that. If needed I will make the changes.
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,842
For slave or master ?

Which pins are you using for Master's and Slave's SPIs ? Mention PORT bit names.

I am just using the actual hardware SPI pins.

Edit:

Okay, I see. There are alternate SPI pins. To you the default pins we have to configure CONFIG3L register and I have not done that. If needed I will make the changes.
Hi J2,

Here are the SPI connections:

To change CONFIG: Open Oshonsoft PIC simulator IDE>>>Click OPTIONS>>>CONFIG BITS>>>[set BITs]>>GENERATE BASIC CODE.

Here's what I've been using
-----------------------------------------------------------------------------------------------
Define CONFIG1L = 0x00
Define CONFIG1H = 0x08 'INT OSC
Define CONFIG2L = 0x0c
Define CONFIG2H = 0x20
Define CONFIG3L = 0x04
Define CONFIG3H = 0x80
Define CONFIG4L = 0x80 'Set for HVP
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40
--------------------------------------------------------------------------------------------------------
C.
 

Attachments

Last edited:

jayanthd

Joined Jul 4, 2015
945
Okay, I will make changes to code and send it to you for testing.

I was making a code to print SSPBUF value in master as binary string. Here it is.

SS8.png
 

jayanthd

Joined Jul 4, 2015
945
Changing the SPI pins will take some time. I changed only Masters CS pin to PORTD.2 and simulation started to behave erratically. SSPBUF of master was getting wrong data and I don't know what is causing the issue. I tried with and without CONFIG register codes. With hardware SPI SS/CS pin the code with/without CONFIG registers code works fine. I am trying to fix it.

Why do you want to change the default SS/CS pin of PIC when hardware SPI is used ? Changing SS/CS pin in master is causing issues. I have not yet modified the slave code.
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Changing the SPI pins will take some time. I changed only Masters CS pin to PORTD.2 and simulation started to behave erratically. SSPBUF of master was getting wrong data and I don't know what is causing the issue. I tried with and without CONFIG register codes. With hardware SPI SS/CS pin the code with/without CONFIG registers code works fine. I am trying to fix it.

Why do you want to change the default SS/CS pin of PIC when hardware SPI is used ? Changing SS/CS pin in master is causing issues. I have not yet modified the slave code.
Hi J2,
TRANSMITTER:

SLAVE:
I chose C/S on 18F4431 PIN 44 because of this:

MASTER:
I chose 18F4620 PIN 40 as C/S because I was running out of spare PINs.
C.
 

Attachments

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Hi J2,
TRANSMITTER:

I would change all of the connections, I have lots of test programs, and PCB boards using the same settings.

Note Both TRANSMITTER and RECEIVER each with 18F4620 and 18F4431 PICs on board, with most connections the same.

SLAVE:
I chose C/S on 18F4431 PIN 44 because of this:

MASTER:
I chose 18F4620 PIN 40 as C/S because I was running out of spare PINs.
C.
 

jayanthd

Joined Jul 4, 2015
945
Hi J2,
TRANSMITTER:

SLAVE:
I chose C/S on 18F4431 PIN 44 because of this:

MASTER:
I chose 18F4620 PIN 40 as C/S because I was running out of spare PINs.
C.
.
Just use simple test hardware with connections as shown in #130. I just want to confirm that code works on hardware. Do not add any other code to my code. Use codes from #122. Let me see what happens.

Master's RD2 pin causes issues atleast in Proteus. I also tested RD0 pin for SS/CS but same issue that simulation behaves erratically.

Edit:

Use this master code.

Code:
Define CONFIG1L = 0x00
Define CONFIG1H = 0x42
Define CONFIG2L = 0x16
Define CONFIG2H = 0x0e
Define CONFIG3L = 0x00
Define CONFIG3H = 0x05
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

AllDigital

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

Dim x As Byte
Dim data As Word
Dim msg As String
 
    initialize_variables
    initialize_ports
    initialize_spi_master
    Hseropen 9600
    enable_interrupts

    While x > 0
        initialize_spi_master
        send_spi_data 0xff
        initialize_spi_slave
        WaitMs 80
     
        If data <> LATB Then
            Hserout "SSPBUF(DEC): ", #LATB, CrLf
            data = LATB
            msg = "0x"
            msg = msg + HexStr(data)
            Hserout "SSPBUF(HEX): ", msg, CrLf
            msg = "%"
                                 
            If data.7 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
         
            If data.6 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif

            If data.5 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
         
            If data.4 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
         
            If data.3 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
         
            If data.2 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif

            If data.1 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
         
            If data.0 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
         
            Hserout "SSPBUF(BIN): ", msg, CrLf
        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_variables()
    x = 1
    data = 0
End Proc                                       

Proc initialize_ports()
    CMCON = %00000111
    ADCON1 = %00001111

    TRISA = %11000000
    TRISB = 0
    TRISD = 0
End Proc                                       

Proc initialize_spi_master()
    TRISC = %00010000
    spi_cs_direction = 0
    spi_cs = 1
    SSPSTAT = %01000000
    SSPCON1 = %00100010
    IPR1.SSPIP = 0
    PIE1.SSPIE = 0
End Proc                                       

Proc initialize_spi_slave()
    TRISC = %00011000
    spi_cs_direction = 1
    SSPSTAT = %01000000
    SSPCON1 = %00100100
    PIE1.SSPIE = 1
End Proc                                       

Proc enable_interrupts()
    Enable High
    Enable Low
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

The above master code gives this result on UART.

SS9.png
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,842
.
Just use simple test hardware with connections as shown in #130. I just want to confirm that code works on hardware. Do not add any other code to my code. Use codes from #122. Let me see what happens.

Master's RD2 pin causes issues atleast in Proteus. I also tested RD0 pin for SS/CS but same issue that simulation behaves erratically.

Edit:

Use this master code.

Code:
Define CONFIG1L = 0x00
Define CONFIG1H = 0x42
Define CONFIG2L = 0x16
Define CONFIG2H = 0x0e
Define CONFIG3L = 0x00
Define CONFIG3H = 0x05
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

AllDigital

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

Dim x As Byte
Dim data As Word
Dim msg As String

    initialize_variables
    initialize_ports
    initialize_spi_master
    Hseropen 9600
    enable_interrupts

    While x > 0
        initialize_spi_master
        send_spi_data 0xff
        initialize_spi_slave
        WaitMs 80
   
        If data <> LATB Then
            Hserout "SSPBUF(DEC): ", #LATB, CrLf
            data = LATB
            msg = "0x"
            msg = msg + HexStr(data)
            Hserout "SSPBUF(HEX): ", msg, CrLf
            msg = "%"
                               
            If data.7 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
       
            If data.6 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif

            If data.5 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
       
            If data.4 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
       
            If data.3 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
       
            If data.2 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif

            If data.1 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
       
            If data.0 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
       
            Hserout "SSPBUF(BIN): ", msg, CrLf
        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_variables()
    x = 1
    data = 0
End Proc                                     

Proc initialize_ports()
    CMCON = %00000111
    ADCON1 = %00001111

    TRISA = %11000000
    TRISB = 0
    TRISD = 0
End Proc                                     

Proc initialize_spi_master()
    TRISC = %00010000
    spi_cs_direction = 0
    spi_cs = 1
    SSPSTAT = %01000000
    SSPCON1 = %00100010
    IPR1.SSPIP = 0
    PIE1.SSPIE = 0
End Proc                                     

Proc initialize_spi_slave()
    TRISC = %00011000
    spi_cs_direction = 1
    SSPSTAT = %01000000
    SSPCON1 = %00100100
    PIE1.SSPIE = 1
End Proc                                     

Proc enable_interrupts()
    Enable High
    Enable Low
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

The above master code gives this result on UART.

View attachment 168871
Hi J2,
A bit frustrating for you!
It is difficult for me to 'simply wire' the PICs, as they are surface mount and need to be on PCBs. Here's the now outdated PCB, I have modified, by changing the SLAVE PIC:

EDIT: I'll try to change the program connections this end, and see if I can test your CODE.

Thanks,
C
 

Attachments

Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Changing SS/CS pin in master is causing issues. I have not yet modified the slave code.
Hi J2,
To clarify.
MASTER:
The 18F4620 C/S PIN, could be any PIN, set to OUT, which is simply a switch PIN, for the SLAVE C/S which is specific and necessary.
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
.
Use codes from #122. Let me see what happens.
Hi J2,
I have been unable to open the SLAVE CODE at #122, I get an error.
Will you edit it into #134, please then both will be in the same place.

I've been diverted a little, by a radio kit that arrived on Saturday, just finished building it (I daren't power it up yet:) )

SLAVE.
EDIT:
Using my program and Oshonsoft SPI settings, I just tried your idea of

PORTB = SSPBUF
Hserout "PORTB ", #PORTB, CrLf

and indeed the PORTB is switching.

C.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Here are the codes.

Master code

Code:
Define CONFIG1L = 0x00
Define CONFIG1H = 0x42
Define CONFIG2L = 0x16
Define CONFIG2H = 0x0e
Define CONFIG3L = 0x00
Define CONFIG3H = 0x05
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

AllDigital

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

Dim x As Byte
Dim data As Word
Dim msg As String
   
    initialize_variables
    initialize_ports
    initialize_spi_master
    Hseropen 9600
    enable_interrupts

    While x > 0
        initialize_spi_master
        send_spi_data 0xff
        initialize_spi_slave
        WaitMs 80
       
        If data <> LATB Then
            Hserout "SSPBUF(DEC): ", #LATB, CrLf
            data = LATB
            msg = "0x"
            msg = msg + HexStr(data)
            Hserout "SSPBUF(HEX): ", msg, CrLf
            msg = "%"
                                   
            If data.7 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
           
            If data.6 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif

            If data.5 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
           
            If data.4 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
           
            If data.3 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
           
            If data.2 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif

            If data.1 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
           
            If data.0 = 1 Then
                msg = msg + "1"
            Else
                msg = msg + "0"
            Endif
           
            Hserout "SSPBUF(BIN): ", msg, CrLf
        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_variables()
    x = 1
    data = 0
End Proc                                         

Proc initialize_ports()
    CMCON = %00000111
    ADCON1 = %00001111

    TRISA = %11000000
    TRISB = 0
    TRISD = 0
End Proc                                         

Proc initialize_spi_master()
    TRISC = %00010000
    spi_cs_direction = 0
    spi_cs = 1
    SSPSTAT = %01000000
    SSPCON1 = %00100010
    IPR1.SSPIP = 0
    PIE1.SSPIE = 0
End Proc                                         

Proc initialize_spi_slave()
    TRISC = %00011000
    spi_cs_direction = 1
    SSPSTAT = %01000000
    SSPCON1 = %00100100
    PIE1.SSPIE = 1
End Proc                                         

Proc enable_interrupts()
    Enable High
    Enable Low
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
Slave code

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

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

End                                               

On Low Interrupt
    If PIE1.SSPIE = 1 And PIR1.SSPIF = 1 Then
        PIR1.SSPIF = 0

        If SSPSTAT.BF Then
            data = SSPBUF

            If data = 0xff Then
                spidatareceived = 1
            Endif

            data = 0
        Endif
       
        SSPCON.SSPOV = 0
    Endif
Resume                                           

Proc initialize_ports()
    ANSEL0 = 0
    ANSEL1 = 0

    TRISA = %11011100
    TRISB = 0
    TRISD = 0
End Proc                                         

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

Proc initialize_spi_master()
    PIE1.SSPIE = 0
    TRISC = %00010000
    spi_cs = 1
    SSPSTAT = %01000000
    SSPCON = %00100010
End Proc                                         

Proc initialize_spi_slave()
    SSPSTAT = %01000000
    SSPCON = %00100100
    TRISC = %01110000
    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                                         

Proc initialize_qei()
    POSCNTH = 0
    POSCNTL = 0
    QEICON = %10000000
    QEICON = %10010100
    DFLTCON = 0
    MAXCNTH = %00000100
    MAXCNTL = 0
End Proc
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Hi J2,
Thanks for both CODES.

I've just found some test boards, that I can use for testing both of your codes, as you have written them :)

I have to add some components, to get them to work first,then wire them.

Am I correct that the SPI connections are:
SLAVE:
RC6 = C/S
RC5 = SCK
RC4 = SDI
Which PIN for SDO?
C.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Hi J2,
Thanks for both CODES.

I've just found some test boards, that I can use for testing both of your codes, as you have written them :)

I have to add some components, to get them to work first,then wire them.

Am I correct that the SPI connections are:
SLAVE:
RC6 = C/S
RC5 = SCK
RC4 = SDI
Which PIN for SDO?
C.
Yes, it is correct. SDO = RC7.


In the Proteus circuit image you can read the pin function names on the PIC model.
 
Last edited:
Top