Hi J,In d/s DS39616D SSP is in pages 205-216
Thanks, I've been reading this as I go along, but I think this is an Oshonsoft programming question.
C.
Hi J,In d/s DS39616D SSP is in pages 205-216
Hi J2,I had written this mikroC PRO PIC code sometime ago. If you understand c then you can refer it else I will port my code as an example to OshonSoft PIC18 Basic and post it here.
Actually my code is multi-master but if you take it one way then it is master-slave.
View attachment 168501
I send PIC16F Proteus Simulation. .PNG file is image file and opens in mspaint.Hi J2,
I don't understand Arduino, and this is what I got when I tried to open the RAR.
My set up is wired as yours.
Thanks, C.
Yes it is, but it is needed, if you want to setup the PIC as slave.Hi J,
Thanks, I've been reading this as I go along, but I think this is an Oshonsoft programming question.
C.
It is only SPI master (bitbanged in sw) , HW SPI is not used.How do you configure SPI as master or slave in PIC18 Simulator IDE's Basic Compiler ?
Hi E,hi jjw,
There is a way that can force the hardware SPI in Oshonsoft.
These a clips are clips from a program I wrote for the TS ie@ C
I have it running at 20MHz with SPI and a TFT
E
Call spi_init() 'hw spi
WaitMs 100
Call lcd_reset() ''lcd_rst before LCD Init.
WaitMs 50
Call tft_init() 'initialise lcd
WaitMs 50
Proc:' example
ss = 0
spi8 = SSPBUF
SSPBUF = color.HB
While Not SSPSTAT.BF
Wend
spi8 = SSPBUF
SSPBUF = color.LB
While Not SSPSTAT.BF
Wend
ss = 1
endproc
Proc spi_init()
TRISC.5 = 0 'sdo to 9225
TRISC.2 = 0 'TRISC.2 = 0 'ss
TRISC.4 = 1 'sdi not used on TFT
TRISC.3 = 0 'clk
ss = 1
SSPSTAT = 0
'SSPSTAT.CKE = 1 'clock edge un remark for trailing edge
'SSPCON1 = 0x10 'full whack... at 8mhz 2mhz operation no SS control
SSPCON1 = %00100000
'00 = fosc/4.. 01 = fosc /16
'10 = fosc/64.. 11 contolled by timer2.
SSPCON1.CKP = 1 'clock polarity unremark for high polarity
End Proc
Hi J2,Master can send a known byte like 0xAF to reuest data from slave and slave after receiving 0xAF should send the required data to master and master should read it and display it.
Hi J,Yes it is, but it is needed, if you want to setup the PIC as slave.
Oshonsoft supports only master.
You don't need to know the address of the SSPBUF.Hi J,
I realise the D/S is needed and I have set up the SLAVE as best as I can, but perhaps not perfectly.
At the moment I'm pretty sure that the SLAVE puts SSPBUF into 0xfc9, as the D/S.
I have added (Just for testing) an HSEROUT of SSPBUF, which I can see on a terminal, and I can change the numbers using the two test buttons of QEI.ress
Now I need the MASTER to READ the SSPBUF from 0xfc9 of the SLAVE.
C.
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
else
LATB = data_
spiDataReceived = 0
end if
Delay_ms(1000)
wend
end.
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
Delay_ms(100)
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.

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


Hi J2,Here is new project [attached]. It uses PIC18F4620 (Master) and PIC18F4431 (Slave) both @ XT 4 MHz.
Simulation video included in the attachment.
Tested and fully working in Proteus.
I have added a button to master for requesting data from slave. When button is pressed and released the master sends 0xFF to slave requesting data and slave upon receiving 0xFF responds by sending its PORTB data to master and master displays it on its PORTB.
If you want some other trigger for requesting data from slave then just change the button press and release detect code to some other triggering flag.
View attachment 168559
Hi J,You don't need to know the address of the SSPBUF.
SSPBUF is a register like PORTA, TRISC, LATA etc.
In the slave you can write to it.
for example:
Dim somedata as Byte
SSPBUF = somedata
You can read it in the master:
Dim data as Byte
SPIreceive data
Look Erics code to setup HW SPI.
It sets the master.
You need to setup the HW slave in PIC184431:
SSPCON =??
SSPSTAT = ??
TRISC = ??
The values are in the d/s and in the link msg#1
It is wrong.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
SSPCON = AZIL
----------------MASTER------------------------------
Dim AZIL as Byte
SPIreceive AZIL
C.
SSPCON = AZIL
SSPBUF = AZIL