![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
amkt20
Joined: 15 Mar 2007 Posts: 1 Location: Pakistan
|
Problem in SPI communication between Master and Slave |
Posted: Tue Mar 20, 2007 11:43 pm |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Wed Mar 21, 2007 8:09 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|