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

pic16f877a spi

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



Joined: 23 Jun 2011
Posts: 31
Location: UK

View user's profile Send private message

pic16f877a spi
PostPosted: Thu Oct 25, 2012 4:44 am     Reply with quote

Hi, just a quickie,

Am I correct in thinking that if two PICs are connected together via spi (one master, the other slave, using SS line), that both of the following examples would be compatible: -

Code:


setup_spi( SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16 | SPI_XMIT_L_TO_H );

setup_spi( SPI_SLAVE | SPI_L_TO_H | SPI_XMIT_L_TO_H );



and

Code:


setup_spi( SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16 | SPI_XMIT_H_TO_L );

setup_spi( SPI_SLAVE | SPI_L_TO_H | SPI_XMIT_H_TO_L );



the difference between the two is the SPI_XMIT_H_TO_L parts.

Cheers,

Tom
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 25, 2012 12:49 pm     Reply with quote

See ckielstra's table of SPI modes:
http://www.ccsinfo.com/forum/viewtopic.php?t=47444&start=3

Figure 2 shows the sampling edge and the idle state for SCLK in each mode:
http://www.totalphase.com/support/kb/10045/#modes

Finally, see this Microchip slide show tutorial on SPI:
http://ww1.microchip.com/downloads/en/devicedoc/spi.pdf
Tom Jetland



Joined: 23 Jun 2011
Posts: 31
Location: UK

View user's profile Send private message

PostPosted: Fri Oct 26, 2012 9:50 am     Reply with quote

Thanks PCM programmer. I'll have a look at this lot.

It's weird how I can only get the spi to work using the first example code block I specified, and not the second. My suspicion lies with the pic16f877A though, as microchip has a whole errata document on spi for this chip, clarifiying certain things, i.e.

http://ww1.microchip.com/downloads/en/DeviceDoc/80131e.pdf

If you can spare the time, please have a look at the 2. Module: MSSP (SPI Slave Mode) on page 2 and see if you can work out what they're talking about?

Thanks,

Tom
Ttelmah



Joined: 11 Mar 2010
Posts: 19328

View user's profile Send private message

PostPosted: Fri Oct 26, 2012 12:08 pm     Reply with quote

The only one that will cause you problems here, is the level. Basically in your slave code, you need to monitor the SCK pin (by using n input on the pin), and only enable the SPI _after_ the pin goes to the idle state. Obviously for a low idle state, there is a probability the line will be low when the chip wakes, but for the high idle state, you will need to wait, or the slave won't work.

Best Wishes
Tom Jetland



Joined: 23 Jun 2011
Posts: 31
Location: UK

View user's profile Send private message

PostPosted: Mon Oct 29, 2012 3:57 am     Reply with quote

Thanks guys,

I do sometimes struggle to understand some of the microchip datasheets, so it's great to get a second opinion.

:-)

Tom
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