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 support@ccsinfo.com

PIC to PIC spi interface problem...

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



Joined: 04 Aug 2005
Posts: 21

View user's profile Send private message

PIC to PIC spi interface problem...
PostPosted: Wed Mar 29, 2006 1:24 am     Reply with quote

Hi...

I'm trying to interface 2 pic16f877a using spi...
1 as master that will send data
1 as slave that will read the data and send it to rs232 port.

the thing is the data i'm sending is from a array buffer that consist of
9 int8...

here is snippet of my master code:

Code:
 

int8   buff8[8]={1,2,3,4,5,6,7,8};

setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_64 | SPI_XMIT_L_TO_H);

output_bit(pin_e0,1);   //SPIdata marker
for(i=0;i<=8;i++)
{   
   spi_write(buff8[i]);
   delay_ms(100);
}
output_bit(pin_e0,0);


and here is snippet of my slave code:

Code:
 setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_SS_DISABLED);

for ( ; !input(PIN_D3); ) //Detected a low to high transition PIN_D3
    ;

for (i=0;i<=8;i++)
{
   //if(spi_data_is_in())
   buff8[i]=spi_read();
}

for(i=0; i<=8; i++)
   printf("\r\n%x",buff8[i]);


what i get is some numbers that have no connection with the contents of buff8[i]... this is really puzzling me..
could someone help??

btw.. is it matters that i use 20M crystal for master and 4M at slave?

Thanks sooo much for ur help@suggestion!
Ttelmah
Guest







PostPosted: Wed Mar 29, 2006 2:43 am     Reply with quote

As a general 'comment', I'd say that a while loop is 'tidier' than a for, to wait for the input pin to change. However this is purely 'aesthetic'.
Speed is not your problem. The SPI bus is being clocked incredibly slowly (master clock/64, and then allowing 100mSec between bytes!). A few uSec betwen bytes to allow time for the array access in the slave is more than adequate.
The problem is your SPI setup. The 'SPI_L_TO_H' definition, tells the units which 'edge' to read the data on. The 'SPI_XMIT_L_TO_H' defintion, tells the master which edge to _change_ the data on. You are telling the slave, to read the data on the same clock edge that it _changes_...

Best Wsihes
Chicky



Joined: 04 Aug 2005
Posts: 21

View user's profile Send private message

PostPosted: Wed Mar 29, 2006 6:46 pm     Reply with quote

thanks Ttelmah...

i've removed the 'SPI_XMIT_L_TO_H' def, but not manage to get the desired result..

here is example of what i get

Quote:
05
08
01
05
03
05
06
01

02
06
03
05
06
01
02
07


i've also change the 'for' to 'while' for the input change...
any other ideas?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 29, 2006 7:02 pm     Reply with quote

Quote:

int8 buff8[8]={1,2,3,4,5,6,7,8};

for (i=0;i<=8;i++)
{
//if(spi_data_is_in())
buff8[i]=spi_read();
}

One problem with your code is that you're using "less than or equal to 8"
as the test in all your for(;;) loops. Those loops are going to process
9 elements, not 8. You are then writing past the end of the array and
you will corrupt some other variable. This can cause flaky operation
of the program or it could crash.

To process only elements 0 through 7, you need to do the loop like this,
with a "less than" test:
Code:

for(i=0; i<8; i++)
   {
    buff8[i]=spi_read();
   }
Guest








PostPosted: Thu Mar 30, 2006 1:48 am     Reply with quote

PCM.. i already tried that.. still got the same result...
hmmm.. this is quite frustrating Rolling Eyes
Chicky



Joined: 04 Aug 2005
Posts: 21

View user's profile Send private message

PostPosted: Thu Mar 30, 2006 2:17 am     Reply with quote

Yay~~ at last!!

u guys wanna know what i did?
i added this part:

Code:

for (i=0;i<8;i++)
{
   while (SSPBUF==0)       
   ;

   data=spi_read();
   printf("\r\n\ndata: %d",data);
   SSPBUF = 0;
}
printf("\r\n");


clearing SSPBUF really helped!!
now what i get are:

Quote:
data: 1
data: 2
data: 3
data: 4
data: 5
data: 6
data: 7
data: 8


thanks for ur help~~ Very Happy Very Happy
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