View previous topic :: View next topic |
Author |
Message |
Ringo42
Joined: 07 May 2004 Posts: 263
|
SPI HtoL or LtoH |
Posted: Fri Sep 19, 2008 11:41 am |
|
|
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
|
|
Posted: Fri Sep 19, 2008 11:55 am |
|
|
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 |
|
|
|