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

problem with the setup_spi() function

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



Joined: 24 Apr 2011
Posts: 14

View user's profile Send private message

problem with the setup_spi() function
PostPosted: Sun Apr 24, 2011 11:12 am     Reply with quote

I understand that if you want to use spi in ccs, you have to first setup the spi by this line of code : setup_spi( SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16), now I understand the SPI_MASTER parameter, what I do not understand is the SPI_H_TO_L parameter, oh and also for the third parameter if you want to use the full oscillator frequency what should you write? Sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 24, 2011 11:48 am     Reply with quote

It's much easier if you use SPI modes instead of CCS's constants.
The following define statements allow you to select any SPI mode.
Code:

// SPI mode definitions for 16F and 18F PICs.
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

This post has an example of how to do it:
http://www.ccsinfo.com/forum/viewtopic.php?t=45258&start=3

This page has a good diagram of the 4 modes:
http://elm-chan.org/docs/spi_e.html

You need to look at the timing diagram for your SPI slave device and
decide what mode it uses. The two things to look for are:
1. The idle state of SCLK (high or low). Look at the sides of the timing
diagram to see the idle state, before and after the SPI transaction.
2. The clock edge of SCLK (rising or falling) that is used to sample the
SPI data. Look at the edge of SCLK that occurs in the middle of a data
bit. Notice if it's rising or falling. Here is another quick reference to
help you decide what mode the slave is using:
http://www.totalphase.com/support/kb/10045/#modes

You also need to select an i/o pin on your SPI Master PIC for the
Chip Select signal. Example:
Code:

#define SPI_CS  PIN_C2 



There is no option to use the direct PIC oscillator frequency for SCLK.
The smallest divisor is 4. So if you have a 40 MHz oscillator, you can
get a 10 MHz SCLK.
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