
Hi J2,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,SPI should be connected as #122 and also QE. Master's UART to PC (Computer) using TTL to USB adpater.

For slave or master ?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.
Yes, there was a reason and you can see that in my Proteus Simulation screenshot that PORTC pins have the SPI pin names.Hi J2,
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.
View attachment 168844
Hi J2,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,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:
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.
.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.
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

Hi J2,.
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,Changing SS/CS pin in master is causing issues. I have not yet modified the slave code.
Hi J2,.
Use codes from #122. Let me see what happens.
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
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
Yes, it is correct. SDO = RC7.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.