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

SPI two way communication problem

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



Joined: 08 Feb 2011
Posts: 14

View user's profile Send private message

SPI two way communication problem
PostPosted: Fri Apr 22, 2011 11:51 am     Reply with quote

Hi,
I'm trying to do two way communication between a master and a slave using SPI. I succeeded in doing one way communication for example sending data from master to slave or sending data from slave to master separately. When I try to send and receive data from one slave I'm getting the wrong data. Below is one of the way I tried to do this. I also tried putting spi_write and spi_read in sequence but it didn't work. Can anyone please help me. Thank you.

MASTER
Code:
void main()
{
setup_spi(spi_master|spi_l_to_h|SPI_CLK_DIV_16);

   while(1)
   {
      output_low(slave3_select);                         
      delay_us(100);                                     
         
         spi_write(joystick_data[0]);
         spi_write(joystick_data[1]);
         spi_write(joystick_data[2]);
         spi_write(joystick_data[3]);
         motor1_speed=spi_read(joystick_data[4]);
         motor2_speed=spi_read(joystick_data[5]);
         motor3_speed=spi_read(joystick_data[6]);
         motor4_speed=spi_read(joystick_data[7]);     
   
      output_high(slave3_select);                         
      delay_us(100); 
   }
}




SLAVE
Code:
void main()

setup_spi(spi_slave|spi_l_to_h);

   while(1)
   {     
       if(input(slave3_select) == 0)
      {     
         spiheader=spi_read();


            spidata[0]=spi_read();
            spidata[1]=spi_read();
            spidata[2]=spi_read();
            spidata[3]=spi_read(current_ccp_delta1);
            spidata[4]=spi_read(current_ccp_delta2);
            spidata[5]=spi_read(current_ccp_delta3);
            spidata[6]=spi_read(current_ccp_delta4);
      }

      else
      {
         delay_cycles(1);
      }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 22, 2011 12:57 pm     Reply with quote

Look at these SPI slave examples and try to do it in a similar way:
http://www.ccsinfo.com/forum/viewtopic.php?t=39145
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
neohimura



Joined: 08 Feb 2011
Posts: 14

View user's profile Send private message

PostPosted: Fri Apr 22, 2011 9:53 pm     Reply with quote

Examples you gave used the SPI interrupt. Is it possible to do it without using the interrupt?
Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Apr 22, 2011 10:06 pm     Reply with quote

See this CCS example file:
Quote:

c:\program files\picc\examples\ex_spi_slave.c
neohimura



Joined: 08 Feb 2011
Posts: 14

View user's profile Send private message

PostPosted: Mon Apr 25, 2011 6:20 am     Reply with quote

When I tried the above code with two pic18f4550, it works.. but when I change the slave pic into dspic30f4011, the data becomes corrupted. I can only receive data from the master. When I try to send data to master, it doesn't work.. Is there any setting i need to change in dspic30?
Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 25, 2011 12:45 pm     Reply with quote

I don't have the PCD compiler, so there is no way I can test it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19480

View user's profile Send private message

PostPosted: Tue Apr 26, 2011 2:56 pm     Reply with quote

Have a look at this thread:
<http://www.ccsinfo.com/forum/viewtopic.php?t=41421&highlight=dspic+spi>

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