ccs spi code
You need to configure only a few thing in the SSPCON1 register. From this register, you can select operation as Master (lowest 4 bit) and corresponding clock (also with these bits). The fastest clock is FOSC/4. SO if your PIC is running at 40 MHz (internal PLL enabled), maximum clock speed for SPI is 10 MHz. Other speed includes FOSC/16 and FOSC/64. Timer 2 can also be used as the clock source. Other setting like clock polarity bit (CKP bit in SSPCON1) must also be configured. And don't forget to enabled the SSPEN bit in SSPCON1 to allow SPI mode instead of setting these pins as normal I/O. Also set SMP and CKE bit in SSPSTAT register accordingly. Set the TRIS accordingly for all the SPI pins as only SDI pin is configured automatically it's direction if SPI mode is enabled.
All the best.
Added after 17 minutes:
Hi Pepillo,
Here's are some sample code. Wrote it some time ago.
// Description:
// This routine initialises the Serial Peripheral Interface (SPI).
void SPI_Init(void)
{
// Transmit occurs on transition from active to idle clock state.
SSPSTAT = 0x40;
// Initialise SPI - synchronous serial port enabled, master clock = Fosc/4,
// and idle state is low level for clock
SSPCON1 = 0x20;
}
// Macro to send 1 byte of data on SPI bus.
#define SPI_PUT_DATA(data) {SSPBUF = data;}
// Macro to deselect the slave device.
#define SLAVE_DESELECT {output_high(P_CS);}
// Macro to select the slave device.
#define SLAVE_SELECT {output_low(P_CS);}
In order to receive, if software polling is used, poll the BF bit in SSPSTAT register. If the bit is set, the data is received and the data is located in SSPBUF. 2nd way is using the interrupt method.
All the best.