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

spi having corrupt data?

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



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

spi having corrupt data?
PostPosted: Tue Jan 02, 2007 7:31 am     Reply with quote

Hello

I am trying to connect two PICs together. The master PIC sends data to the slave PIC.

Something really odd happens - the data retrieved is sometimes wrong. For example, I have a loop where I send/receive a '1' from the master PIC to the slave PIC, and sometimes I get a '3'!

Would anyone have any idea why this could be the case? I tried slowing down the SPI (SPI_CLK_DIV_64) to no avail.

Thanks

Master (sender) code
Code:


#define SPI_MODE_0_0 0x4000
#define SPI_MODE_0_1 0x0000
#define SPI_MODE_1_0 0x0010
#define SPI_MODE_1_1 0x4010

void main(void)
{
   setup_spi(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_4);
   set_tris_c(0b00000011);

   while(1)
   {
      while(!input(SIG)); //wait for signal to send data (slave PIC starts spi_read())
      delay_us(10);
      spi_write(2);
      etc.....
      etc.....
      etc.....
      etc.....
   }
}


ps. also could someone tell me, which has less speed overhead in an 18F, UART or SPI?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jan 02, 2007 9:29 am     Reply with quote

Which PIC are you using?
What parameters do you have for the setup_spi() in your slave?
Ttelmah
Guest







PostPosted: Tue Jan 02, 2007 9:41 am     Reply with quote

There are a lot of 'bits' to this.
First, the key advantage of SPI, is that a byte is transferred _both_ ways at the same time. Also, since it is synchronised by the master clock, there are no timing problems with different crystals/stability of the oscillators on the two chips.
What do you mean by 'speed overhead'?. Both these transfers, assuming the hardware is used, use no overhead in the transmitting/sending devices, except to read/write the byte. The maximum transfer rate, is from SPI, which can clock right up to the processor clock/4.
Now on your 'code', you don't show enought to give any idea of what is wrong. You read a signal line, waiting for it to go high. Which pin is this? I is presumably being set high by the slave to say it is ready for a transmission?. If so, why?. The point is that with SPI, the master controls the transfer. You can send the byte at any time from the master, and _then_ retrieve it from the buffer in the slave, whenever you want. Because the transfer is buffered at both ends, there is no need for the slave to signal at all. This is wasting a line. If you want the slave to 'initiate' the transfer, then make _it_ the master. Have the sending device 'preload' the data to send, and load the next byte, once the slave has retrieved it.
The 'odds' are that a data problem, is a result of using the wrong edge fro transmission/sampling at one end.

Best Wishes
theteaman



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

PostPosted: Tue Jan 02, 2007 3:12 pm     Reply with quote

ckielstra wrote:
Which PIC are you using?
What parameters do you have for the setup_spi() in your slave?


I am using two 18F2525s. The parameter for the slave is:
Code:

setup_spi(SPI_SLAVE | SPI_MODE_0_0 | SPI_CLK_DIV_4);



Quote:

Which pin is this? I is presumably being set high by the slave to say it is ready for a transmission?. If so, why?. The point is that with SPI, the master controls the transfer. You can send the byte at any time from the master, and _then_ retrieve it from the buffer in the slave, whenever you want. Because the transfer is buffered at both ends, there is no need for the slave to signal at all. This is wasting a line.


I changed the master and slave around but the problem is still the same :/. But I figured out the solution, as you said - by changing to SPI_MODE_0_1, it now works... Can you explain what this actually means because I assumed both modes should work if master/slave are configured correctly??

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 02, 2007 3:42 pm     Reply with quote

There's an errata on the B4 silicon for the SPI slave mode.
(There are tons of other erratas in the earlier silicon revisions).
http://ww1.microchip.com/downloads/en/DeviceDoc/80282a.pdf
It says there must be a minimum of 3 instruction cycles between the
falling edge of Slave Select and the first SCK clock. Try adding
the line shown in bold below to your Master PIC's code.
Quote:

output_low(SS_PIN);
delay_cycles(5);
spi_write(0x55);
output_high(SS_PIN);


Also, post your crystal frequency and your compiler version.
theteaman



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

PostPosted: Wed Jan 03, 2007 1:36 am     Reply with quote

PCM programmer wrote:
There's an errata on the B4 silicon for the SPI slave mode.
(There are tons of other erratas in the earlier silicon revisions).
http://ww1.microchip.com/downloads/en/DeviceDoc/80282a.pdf
It says there must be a minimum of 3 instruction cycles between the
falling edge of Slave Select and the first SCK clock. Try adding
the line shown in bold below to your Master PIC's code.
Quote:

output_low(SS_PIN);
delay_cycles(5);
spi_write(0x55);
output_high(SS_PIN);


Also, post your crystal frequency and your compiler version.


Hi PCMProgrammer

Is slave select optional? Because I am using that pin.. I can change it though..
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 03, 2007 1:55 am     Reply with quote

It's enabled by default when you setup an SPI slave. You have to
explicitly disable it, if you don't want it. Do that by OR'ing in the
constant shown in bold below. This is for your Slave PIC only.
Quote:
setup_spi(SPI_SLAVE | SPI_MODE_0_0 | SPI_CLK_DIV_4 | SPI_SS_DISABLED);



Also, post your crystal frequency. Or, if you're using the PLL post
your clock frequency.
theteaman



Joined: 04 Aug 2006
Posts: 98

View user's profile Send private message

PostPosted: Wed Jan 03, 2007 2:14 am     Reply with quote

PCM programmer wrote:
It's enabled by default when you setup an SPI slave. You have to
explicitly disable it, if you don't want it. Do that by OR'ing in the
constant shown in bold below. This is for your Slave PIC only.
Quote:
setup_spi(SPI_SLAVE | SPI_MODE_0_0 | SPI_CLK_DIV_4 | SPI_SS_DISABLED);



Also, post your crystal frequency. Or, if you're using the PLL post
your clock frequency.


Thanks for that, I disabled /SS. My crystal is 20MHz.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 03, 2007 12:33 pm     Reply with quote

It may not work reliably with an SCLK frequency of 5 MHz.
The PIC's data sheet wants a setup and hold time of 100 ns (each)
for data to SCLK edge. That's for the Master or the Slave, when
receiving data.

I suggest setting the SCLK speed to the next lower setting:

SPI_CLK_DIV_16

Do that in both the Master and the Slave PICs.
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