The master has an LED to show me the code is running and the slave has an LCD to show me what is being received.
The problems I am having are:
that I need to re-initialise the SPI interface between every byte received and
that although the slave select pin works OK I can’t make it work with slave select disabled.
My code is shown below – both send and receive are here and the mode variable selects whether the send or receive code is executed.
I have used the LCD to check the SSPSTAT and SSPCON1 registers and these are not changed by the SPI1_Init_Advanced statement.
I expect there is something obvious that I have overlooked but I’ve spent ages looking at the datasheet and I’m struggling.
I’d be grateful for any advice.
Russell
Code: Select all
program MyProject
' Declarations section
' Lcd module connections
dim
LCD_RS as sbit at RB4_bit
LCD_EN as sbit at RB5_bit
LCD_D7 as sbit at RB3_bit
LCD_D6 as sbit at RB2_bit
LCD_D5 as sbit at RB1_bit
LCD_D4 as sbit at RB0_bit
CSN as sbit at RA5_bit
CE as sbit at RA4_bit
dim
LCD_RS_Direction as sbit at TRISB4_bit
LCD_EN_Direction as sbit at TRISB5_bit
LCD_D7_Direction as sbit at TRISB3_bit
LCD_D6_Direction as sbit at TRISB2_bit
LCD_D5_Direction as sbit at TRISB1_bit
LCD_D4_Direction as sbit at TRISB0_bit
' End Lcd module connections
dim
i as integer
n as integer
txt as string[3]
buffer as byte
take, dummy as byte
mode as byte
reg as byte
main:
mode = 1
'mode = 0 'remove the apostrophe to make the program receive
adcon1 =7
cmcon = 7
trisa.5 = 1
porta.5 = 0
SPI1_Init()
'*****************************************************************************************
Lcd_Init()
Lcd_Cmd(_LCD_CURSOR_OFF)
trisa.0 = 0 'sets a.0 as output
i = 1
n = 1
'*****************************************************************************************
'SEND ROUTINE
while mode = 1
SPI1_Write(buffer) 'Send a byte to the SPI
'*****************************************************************************************
porta.0 = i
delay_ms(200)
buffer = buffer + 1
if i = 1 then
i = 0
else
i = 1
end if
if buffer = 255 then
buffer = 0
end if
'*****************************************************************************************
WEND
' RECEIVE ROUTINE
'SPI1_Init_Advanced(_SPI_SLAVE_SS_DIS, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
'SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
WHILE mode = 0
SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH)
take = SPI1_Read(dummy) 'Does this pause until it gets data?
ByteToStr(take, txt)
'*****************************************************************************************
LCD_OUT(1,1,txt)
LCD_OUT(2,n,".")
'delay_ms(10)
if n = 16 then
LCD_OUT(2,1," ")
n=1
else
n= n+1
end if
'*****************************************************************************************
WEND
end.