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 CCS Technical Support

Sending Data Problem at SPI Slave mode

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



Joined: 03 Aug 2010
Posts: 26

View user's profile Send private message

Sending Data Problem at SPI Slave mode
PostPosted: Mon Feb 21, 2011 6:12 am     Reply with quote

I wrote these codes shown below but I couldn't send any data at SPI Slave mode. (My compiler version is 4.114) (Also I looked at this link: http://www.ccsinfo.com/forum/viewtopic.php?t=39145&highlight=spi+slave
Master Side
Code:

#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000) 

void main()
{
int a;
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);

while(1)
  {
  a=spi_read();
  if(a==58)
  {
  output_high(PIN_C3);
  delay_ms(1000);
  output_low(PIN_C3);
  delay_ms(1000);
  }
  }
}

Slave
Code:

#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
int a;

#int_ssp
void ssp_isr(void)
{
   a=58;
   spi_write(a);
   delay_us(100);
}

void main()
{
setup_spi(SPI_SLAVE | SPI_SS_DISABLED | SPI_L_TO_H);
clear_interrupt(INT_SSP);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(1)
  {
  }
}

Could you propose anything? (I tested these codes with real hardware.)
Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Mon Feb 21, 2011 6:29 am     Reply with quote

Two separate problems.
First, is that you are potentially not sending anything till _after_ the master has completed it's transaction.
With SPI, the master sends a byte to the slave, and gets back garbage (what is left in the slaves transmit buffer from last time). It then has to clock another byte to the slave, to get back what the slave loaded. INT_SSP, only occurs when the slave has received the first byte....
Then second, is that the master has to _clock_ each transaction. Currently you are not.

Code:

//Slave
#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
int a;

#int_ssp
void ssp_isr(void)
   {
   int dummy;
   dummy=spi_read(); //get the byte the master has sent - you may want it
   a=58;
   spi_write(a);
}

void main()
   {
   setup_spi(SPI_SLAVE | SPI_SS_DISABLED | SPI_L_TO_H);
   clear_interrupt(INT_SSP);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   while(1)
     {
   }
}

//Master
#include <18F14K50.H>
#include <PIC18F14K50_registers.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)

void main()
   {
   int a;
   setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);

   while(1)
      {
       spi_write(0); //send a byte to trigger the SSP interrupt.
       delay_us(40); //Wait for the slave to load the response
       a=spi_read(0); //read the reply _clocking it_. Hence the '0'
       if(a==58)
         {
         output_high(PIN_C3);
         delay_ms(1000);
         output_low(PIN_C3);
         delay_ms(1000);
      }
   }
}


Best Wishes
Elysion



Joined: 03 Aug 2010
Posts: 26

View user's profile Send private message

PostPosted: Tue Feb 22, 2011 8:55 am     Reply with quote

Ok Thanks for your consideration. It is worked. But when I added RS232 module to these codes, it didn't work.
My slave side is same but I added 2 lines to master side:
Code:

#include <18F14K50.H>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1,NOMCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B7,rcv=PIN_B5)

void main()
   {
   int a;
   setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);

   while(1)
      {
       spi_write(0); //send a byte to trigger the SSP interrupt.
       delay_us(40); //Wait for the slave to load the response
       a=spi_read(); //read the reply _clocking it_. Hence the '0'
         if(a==58)
         {
         printf("It is worked");
         output_high(PIN_C3);
         delay_ms(1000);
         output_low(PIN_C3);
         delay_ms(1000);
      }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Feb 22, 2011 1:00 pm     Reply with quote

Quote:
while(1)
{
spi_write(0); //send a byte to trigger the SSP interrupt.
delay_us(40); //Wait for the slave to load the response
a=spi_read(); //read the reply _clocking it_. Hence the '0'
if(a==58)

You took out the 0 parameter on the spi_read() function (but you left
in the comment). Why did you do that ? Now the Master won't make the
SCLK signal. Without the SCLK, it won't read the byte from the slave.
It won't work.
Elysion



Joined: 03 Aug 2010
Posts: 26

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 2:18 am     Reply with quote

Ttelmah codes didn't work and I changed this line "a=spi_read(0); " and It is worked. But I couldn't solve RS232 problem,yet
Ttelmah



Joined: 11 Mar 2010
Posts: 19545

View user's profile Send private message

PostPosted: Thu Feb 24, 2011 4:01 am     Reply with quote

Had you taken the time delay out of the slave code. It won't work if that is there.

What you are doing at present, is the read, without '0', returns the 'garbage' value that was clocked when you sent the command to the master. Because the code loops, you will on the second loop, get back the value from the first command. No good if the value is changing, but will 'appear' to work, with the code just returning one value.

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