|
SPI, or Serial Peripheral Interface, is a synchronous serial communication protocol used to enable data exchange between a master device (like a microcontroller) and one or more peripheral (slave) devices. Microchip's PIC® microcontrollers often come with built-in SPI modules (sometimes labeled as MSSP - Master Synchronous Serial Port). These can be configured either as master or slave via software.
SPI operates using four main lines: SCK (Serial Clock), MOSI (Master Out Slave In), MISO (Master In Slave Out), and SS or CS (Slave Select or Chip Select). The master generates the clock signal on the SCK line and initiates communication by selecting a specific slave using the CS line. Data is then transmitted bit by bit, with the master sending data on the MOSI line and receiving data on the MISO line simultaneously, allowing for full-duplex communication. This simple and fast protocol is commonly used in embedded systems for connecting devices like sensors, memory chips, and displays.
CCS provides a support library for taking advantage of both hardware and software based SPI functionality. Hardware SPI uses the built-in SPI peripheral inside the microcontroller. Software SPI bit-bangs the I/O pins. The setup_spi() library is for hardware SPI, while the #use spi() library can be used for both hardware and software SPI. The following is an example showing how to use #use spi() for software SPI:
#use spi(MASTER, SPI_CLOCK=PIN_C3, SPI_DI=PIN_C4, SPI_DO=PIN_C5, MODE=0, BITS=8, STREAM=SPI_STREAM)
BYTE wData[128];
BYTE rData[128];
// Fill wData with the data to transfer
// Send 128 bytes and save the received data to rData
spi_transfer(SPI_STREAM, wData, rData, 128);
// Send 0xAA and receive a byte at the same time
int8 response = spi_xfer(SPI_STREAM, 0xAA, 8);
In this example, we are setting the master device. After setting all of the proper parameters in #use spi(), either the spi_transfer() function or the spi_xfer() function can be used to both transfer and receive data on the SPI bus. The spi_xfer() function lets you set how many bits of data will be transferred. The spi_transfer() function is designed for sending one or more bytes at a time. It is generally faster for larger data transfers, as it handles multiple bytes in a single call. The following is an example of how to set up #use spi() for hardware SPI:
#use spi(MASTER, SPI1, MODE=0, BITS=8, STREAM=SPI_STREAM, ENABLE=PIN_C0, DO=PIN_C5, DI=PIN_C4)
The setup_spi() library should be used when you want to configure hardware SPI manually and use register-level access. The compiler has functions to directly access the MSSP hardware SPI for users with special needs. The following is an example showing how to use setup_spi():
setup_spi(SPI_MASTER | SPI_CLK_DIV_16 | SPI_L_TO_H | SPI_XMIT_L_TO_H);
spi_write(0xAA);
int data = spi_read();
This example sets the master device and uses the spi_write() function to send the data and the spi_read() function to read the data.
Like us on Facebook. Follow us on X (Twitter).
About CCS:
CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit https://www.ccsinfo.com.
PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.
|