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

Problem in SPI communication between Master and Slave

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



Joined: 15 Mar 2007
Posts: 1
Location: Pakistan

View user's profile Send private message Yahoo Messenger MSN Messenger

Problem in SPI communication between Master and Slave
PostPosted: Tue Mar 20, 2007 11:43 pm     Reply with quote

Hi all,

I am trying to communicate between 2 PIC18F452. one is Master and second is slave. Slave is connected to PC with serial port. The codes of master and slave controllers given blow.

Master Code

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

void main()
{
int rcv;
set_tris_b(0x00);
set_tris_c(0b00100000); //RC5 high for data out.
set_tris_a(0x00);
setup_spi(spi_master |spi_l_to_h | spi_clk_div_16 );
output_high(PIN_A0);

while(1)
{
output_low(PIN_A0);
rcv=spi_read('K');
output_high(PIN_A0);
delay_ms(50);
if (rcv==0x65)
{
output_low(PIN_A0);
[b]spi_write('R');

output_high(PIN_A0);[/b]
}
delay_ms(500);
}


}

Slave Code

#include <18F452.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void main()
{
int rcv;
set_tris_b(0x00);
set_tris_a(0b00100000);
set_tris_c(0b00101000); //RC5 high for data out. RC3 as clk.
setup_spi(SPI_SLAVE| SPI_L_TO_H | SPI_CLK_DIV_16);
printf("Slave waiting for the data from Master");

while(1)
{
while( !spi_data_is_in());
putc(spi_read(0x65));

}

}



from this code, i am getting the msg K (first data from Master) but not R(to indicate that master is getting the data from slave as well).

so the problem is,, i am getting the data from master but cannt send any data from slave to master. am i missing something in my code?
hope to see some help.

btw, my compiler version is 3.242.

regards
mskala



Joined: 06 Mar 2007
Posts: 100
Location: Massachusetts, USA

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 8:09 pm     Reply with quote

First thing to try is take out SPI_CLK_DIV_16 from the slave code. The slave doesn't generate the clock and config bits may not be set correctly by the compiler.

Also, even when you will get this to transmit data from slave to master, bytes will be out of sync.

In master you send 'K' and expect 0x65. But in slave you wait for the transaction to occur (SPI_DATA_IS_IN), then after it is completed you use SPI_READ(0x65), which will not be sent until the next time the master starts a transaction. This is because the master and slave send their 8 bits to each other at the same time.

To do this type of demo, you could have the slave code as:
Code:

while (1) {
    putc(spi_read(0x65));
}


because in slave mode the SPI_READ will wait for clock to come in.


Also, I assume you've tied pin A0 of master to pin A5 of the slave.
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