Hello Karim,
I missed that you were monitoring using LEDs. As Rocco indicated, the use of LEDs is quite unsuitable for monitoring the SPI operation. You will need to use an oscilloscope, and to capture a single SPI transaction at a fast clock rate, it would need to be a storage type.
To monitor the correct operation of the SPI and shift register combination, you could connect LEDs at the outputs of the shift register. Bear in mind that your code will need to "strobe" the output latches of the shift register, after the SPI transaction is completed, to transfer the result of the SPI transaction.
The following code is presented as a general purpose function for a single master SPI transaction, for both sending a byte to the slave, and receiving the returned byte from the slave.
byte SPI_trans (byte sendval)
{
while (SPI0SR_SPTEF == 0);
SPI0DR = sendval;
while ((SPI0SR_SPIF == 0);
return SPI0DR
}
Note that the final read of SPI0DR is always necessary to clear the SPIF flag, and prevent an overrun error, whether or not you are using the returned data. Since the function waits for the SPIF flag to become set, the SPTEF flag would always be set whenever the function is re-entered, so the first line of the function is really a formality, and could be omitted in most instances.
Regards,
Mac