#include void initspi(void) { char junk; SPI2CONbits.ON = 0; // disable SPI to reset any previous state junk = SPI2BUF; // read SPI buffer to clear the receive buffer SPI2BRG = 7; //set BAUD rate to 1.25MHz, with Pclk at 20MHz SPI2CONbits.MSTEN = 1; // enable master mode SPI2CONbits.CKE = 1; // set clock-to-data timing (data centered on rising SCK edge) SPI2CONbits.ON = 1; // turn SPI on } char spi_send_receive(char send) { SPI2BUF = send; // send data to slave while (!SPI2STATbits.SPIBUSY); // wait until received buffer fills, indicating data received return SPI2BUF; // return received data and clear the read buffer full } void main(void) { char received; initspi(); // initialize the SPI port received = spi_send_receive(‘A’); // send letter A and receive byte back from slave }