Tuesday, March 10, 2009

SPI Modes in CCS C

When interfacing SPI devices, the appropriate SPI mode should be used. The SPI modes are described here in detail: http://en.wikipedia.org/wiki/Serial_peripheral_interface

Here are some easy to use defines for use with the CCS C compiler in the setup_spi() function:

#define SPI_MODE_0_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)    //SPI Mode 0
#define SPI_MODE_0_1  (SPI_L_TO_H)                      //SPI Mode 1
#define SPI_MODE_1_0  (SPI_H_TO_L)                      //SPI Mode 2
#define SPI_MODE_1_1  (SPI_H_TO_L | SPI_XMIT_L_TO_H)    //SPI Mode 3

Just put them in a header or source file somewhere and call setup_spi() like this:

setup_spi(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_4);

Note that SPI Mode 0 is a typical SPI mode. The AS1107 uses it, for example. I think it’s the most common SPI mode.

1 comment:

  1. Thank very much. This snippet solved my problem with the MCP3901.
    G.E.

    ReplyDelete