View previous topic :: View next topic |
Author |
Message |
neohimura
Joined: 08 Feb 2011 Posts: 14
|
SPI two way communication problem |
Posted: Fri Apr 22, 2011 11:51 am |
|
|
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
|
|
|
neohimura
Joined: 08 Feb 2011 Posts: 14
|
|
Posted: Fri Apr 22, 2011 9:53 pm |
|
|
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
|
|
Posted: Fri Apr 22, 2011 10:06 pm |
|
|
See this CCS example file:
Quote: |
c:\program files\picc\examples\ex_spi_slave.c |
|
|
|
neohimura
Joined: 08 Feb 2011 Posts: 14
|
|
Posted: Mon Apr 25, 2011 6:20 am |
|
|
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
|
|
Posted: Mon Apr 25, 2011 12:45 pm |
|
|
I don't have the PCD compiler, so there is no way I can test it. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Tue Apr 26, 2011 2:56 pm |
|
|
Have a look at this thread:
<http://www.ccsinfo.com/forum/viewtopic.php?t=41421&highlight=dspic+spi>
Best Wishes |
|
|
|