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 CCS Technical Support

SPI HtoL or LtoH

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



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

SPI HtoL or LtoH
PostPosted: Fri Sep 19, 2008 11:41 am     Reply with quote

I don't see a good explanation of what exactly SPI_H_TO_L means.
I want to convert the example below to CCS C.
The pin goes low, then data is written, then it goes high and data is read.
so should the setup be SPI_L_TO_H?




uns8 spi_comm(uns8 outgoing_byte)
{
uns8 incoming_byte, x;

for(x = 0 ; x < 8 ; x++)
{
SCL = 0; //Toggle the SPI clock

SDI = outgoing_byte.7; //Put bit on SPI data bus
outgoing_byte <<= 1; //Rotate byte 1 to the left

SCL = 1;

incoming_byte <<= 1; //Rotate byte 1 to the left
incoming_byte.0 = SDO; //Read bit on SPI data bus
}

return(incoming_byte);
}

Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 19, 2008 11:55 am     Reply with quote

You don't really need to know what those constants mean.
To setup the SPI with the setup_spi() function, you need to:

1. Look at the data sheet for your SPI slave device and figure out the
required SPI mode for your Slave device with the help of these two
websites:
http://www.totalphase.com/support/kb/10045/#modes
http://elm-chan.org/docs/spi_e.html
To select the mode you have to answer these two questions:
A. What edge of SCLK is used to sample the incoming data in the Slave ?
B. What is the idle state of the SCLK ?
Once you know that, just look at the timing diagrams shown in either
of those two links and you can find the SPI mode.

2. Add the following #define statements to your program.
Then use the appropriate mode constant (not the individual "L to H" setting) in your setup_spi() statement.
Code:

#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)


3. You also need to choose the correct clock divisor for the setup_spi()
statement, so you don't exceed the maximum specified SCLK frequency
for your SPI slave device.

Example of final result:
Code:
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);

This just an example. You'll have to figure out the correct settings for
your device.


If you still want an explanation of "L to H", etc., ckielstra has a table here:
http://www.ccsinfo.com/forum/viewtopic.php?t=32040&start=5
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