spi_data_is_in2( ) Syntax: result = spi_data_is_in() result = spi_data_is_in2() Parameters: None Returns: 0 (FALSE) or 1 (TRUE) Function: Returns TRUE if data has been received over the SPI. Availability: This function is only available on devices with SPI hardware. Requires: Nothing Examples: ( !spi_data_is_in() && input(PIN_B2) ); if( spi_data_is_in() ) data = spi_read(); Example Files: None Also See: spi_read(), spi_write(), SPI overview spi_read( ) spi_read2( ) Syntax: value = spi_read (data) value = spi_read2 (data) Parameters: data is optional and if included is an 8 bit int. Returns: An 8 bit int Function: Return a value read by the SPI. If a value is passed to SPI_READ the data will be clocked out and the data received will be returned. If no data is ready, SPI_READ will wait for the data. If this device is the master then either do a SPI_WRITE(data) followed by a SPI_READ() or do a SPI_READ(data). These both do the same thing and will generate a clock. If there is no data to send just do a SPI_READ(0) to get the clock. If this device is a slave then either call SPI_READ() to wait for the clock and data or use SPI_DATA_IS_IN() to determine if data is ready.
Availability: This function is only available on devices with SPI hardware. Requires: Nothing Examples: in_data = spi_read(out_data); Example Files: ex_spi.c Also See: spi_data_is_in(), spi_write(), SPI overview spi_write( ) spi_write2( ) Syntax: SPI_WRITE (value) SPI_WRITE2 (value) Parameters: value is an 8 bit int Returns: Nothing Function: Sends a byte out the SPI interface. This will cause 8 clocks to be generated. This function will write the value out to the SPI. At the same time data is clocked out data is clocked in and stored in a receive buffer. SPI_READ may be used to read the buffer. Availability: This function is only available on devices with SPI hardware. Requires: Nothing Examples: spi_write( data_out ); data_in = spi_read(); Example Files: ex_spi.c Also See: spi_read(), spi_data_is_in(), SPI overview
spi_xfer( ) Syntax: spi_xfer(data) spi_xfer(stream, data) spi_xfer(stream, data, bits) result = spi_xfer(data) result = spi_xfer(stream, data) result = spi_xfer(stream, data, bits) Parameters: data is the variable or constant to transfer via SPI. The pin used to transfer data is defined in the DO=pin option in #use spi. stream is the SPI stream to use as defined in the STREAM=name option in #use spi. bits is how many bits of data will be transferred. Returns: The data read in from the SPI. The pin used to transfer result is defined in the DI=pin option in #use spi. Function: Transfers data to and reads data from an SPI device. Availability: All devices with SPI support. Requires: #use spi Examples: int i = 34; spi_xfer(i); // transfers the number 34 via SPI int trans = 34, res; res = spi_xfer(trans); // transfers the number 34 via SPI // also reads the number coming in from SPI Example Files: None Also See: #USE SPI
SPI™ is a fluid standard for 3 or 4 wire, full duplex communications named by Motorola. Most PIC devices support most common SPI™ modes. CCS provides a support library for taking advantage of both hardware and software based SPI™ functionality. For software support, see #use spi. Relevant Functions: setup_spi(mode) setup_spi2 Configure the hardware SPI to the specified mode. The mode configures setup_spi2(mode) thing such as master or slave mode, clock speed and clock/data trigger configuration. Note: for devices with dual SPI interfaces a second function, setup_spi2(), is provided to configure the second interface. spi_data_is_in() Returns TRUE if the SPI receive buffer has a byte of data. spi_data_is_in2() spi_write(value) spi_write2(value) Transmits the value over the SPI interface. This will cause the data to be clocked out on the SDO pin. spi_read(value) spi_read2(value) Performs an SPI transaction, where the value is clocked out on the SDO pin and data clocked in on the SDI pin is returned. If you just want to clock in data then you can use spi_read() without a parameter. Relevant Preprocessor: None Relevant Interrupts: #int_sspA #int_ssp2 Transaction (read or write) has completed on the indicated peripheral. Relevant getenv() Parameters: SPI Returns TRUE if the device has an SPI peripheral Example Code: //configure the device to be a master, data transmitted on H-to-L clock transition setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16); spi_write(0x80); //write 0x80 to SPI device value=spi_read(); //read a value from the SPI device value=spi_read(0x80); //write 0x80 to SPI device the same time you are reading a value.