CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SPI not working with SS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
SoBot



Joined: 20 Apr 2010
Posts: 9

View user's profile Send private message

SPI not working with SS
PostPosted: Fri Sep 03, 2010 7:39 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 03, 2010 9:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Sep 04, 2010 4:04 am     Reply with quote

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: 19328

View user's profile Send private message

PostPosted: Sat Sep 04, 2010 4:25 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group