View previous topic :: View next topic |
Author |
Message |
SoBot
Joined: 20 Apr 2010 Posts: 9
|
SPI not working with SS |
Posted: Fri Sep 03, 2010 7:39 am |
|
|
I have my two PIC18F2341's connected over SPI. I send only one byte and test it on the other side. If my slave is in SPI_SS_Disabled mode, then everything works fine. If the SS is enabled then the slave doesn't get anything even though the master pulls down the SS line 1 second before he sends the byte and back up 1 second after, or even if I keep it low. Please tell me if I am missing something.
Slave code:
Code: |
#include <18f2431.h>
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOPROTECT
#use delay(clock=20M)
//#use delay(clock=40M, oscillator=10M)
#use fast_io(B)
#use fast_io(A)
#use fast_io(C)
int8 SPI_RXBuffer[SPI_RXBufferSize] ={0};
int8 SPI_RXBufferCounter = 0;
void main()
{
set_tris_A(0b11111001);
set_tris_B(0xC0); //11000000
set_tris_C(0b01111000); //SPI
// setup_spi(SPI_SLAVE | SPI_MODE_3 | SPI_SS_Disabled);
setup_spi(SPI_SLAVE | SPI_MODE_3 );
clear_interrupt(INT_SSP);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
While(1)
{
if (SPI_RXBufferCounter > 0)
{
if (SPI_RXBuffer[0] == 90)
{
output_high(LED);
delay_ms(100);
output_low(LED);
SPI_RXBufferCounter = 0;
}
}
}
}
#int_ssp
void ssp_isr(void)
{
if (SPI_RXBufferCounter < SPI_RXBufferSize)
{
SPI_RXBuffer[SPI_RXBufferCounter] = spi_read();
SPI_RXBufferCounter++;
}
} |
Master code:
Code: |
#include <18f2431.h>
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOPROTECT
#use delay(clock=20M)
//#use delay(clock=40M, oscillator=10M)// PLL
#use fast_io(B)
#use fast_io(A)
#use fast_io(C)
set_tris_A(0b11111001);
set_tris_B(0xC0); //11000000
set_tris_C(0b00010000); //01111000 //SPI master
setup_spi(spi_master | spi_l_to_h | spi_clk_div_64 );
//output_low(PIN_C6);
While(1)
{
delay_ms(1000);
output_low(PIN_C6);
delay_ms(1000);
spi_write(90);
delay_ms(1000);
output_high(PIN_C6);
} |
Last edited by SoBot on Sat Sep 04, 2010 5:07 am; edited 1 time in total |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Sep 03, 2010 9:23 am |
|
|
First thing to fix is ensuring that both the master and slave are using the same SPI mode. Your slave is setup for mode 3 (I guess, because the define is missing from your program) and the master is configured for mode 1.
I don't know why, but in the slave you are setting up with the easy to read define value SPI_MODE_3 and then in the master you are using the unreadable CCS define spi_l_to_h. Why mixing the two configuration methods? |
|
|
SoBot
Joined: 20 Apr 2010 Posts: 9
|
|
Posted: Sat Sep 04, 2010 4:04 am |
|
|
Thanks ckielstra. I changed it now to setup_spi(SPI_MASTER | SPI_MODE_3 | spi_clk_div_64 ) in the master and still the same results. Everything works well, but as soon as I turn on the SS in the slave no data is comming in. It doesn't even triger the interrupt. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Sat Sep 04, 2010 4:25 am |
|
|
Are you 100% confident that the slave line is actually changing state at the receive chip?. Something silly like a solder whisker, or dry-joint, could be an explanation....
Best Wishes |
|
|
|