# Using Logic Analyzer for SPI

I started my project of interfacing MicroSD Card with PIC32MX795F512L. To start with this first I made a program to test SPI protocol in which I did loopback test by shorting the SDO & SDI line. It was working fine. Then I moved ahead and write a good program to interface card but I even failed to initialize the card. So I though of purchasing some tools like saleae logic and MicroSD Sniffer. As this is my first time using the analyzer, what my understanding is to when we start our program, at the same time we have to start capturing the data in analyzer.

With this understanding, I tested a UART:

while(1)
{
putsUART1("Hello");
DelayMs(500);
}


I captured this in analyzer and everything was fine. Now I thought of capturing the SPI loopback test program but I am seeing some random data in analyzer. Following is the code for it:

void initSPI(void)
{
SPI1BRG = 71;
SPI1CONbits.MSTEN = 1; //MasterEnable
SPI1CONbits.CKE = 1;  //CKE on. CKP OFF
SPI1CONbits.ON = 1;  //SPI Module ON
}

unsigned char writeSPI(unsigned int b)
{

SPI1BUF=b; // write to buffer for TX
while( !SPI1STATbits.SPIRBF); // wait transfer complete
return(SPI1BUF);

}


and in main function, after calling initSPI, in while(1) I am sending data to SPI channel:

while(1)
{
x = writeSPI(0x12);
}


I am sending 0x12. So in the analyzer MOSI(SDO) should show this value but its showing some random values. I have connected the pins correctly and SPI settings in the analyzer is according to my settings in the code. I am capturing the data for 1sec and speed at 24MS/s. Also I want to know that how can I check at what frequency SPI Protocol is running.? Any help.!

Here is the image of analyzer result

and analyzer settings for SPI

• Well a little bit of hunting, I have found how to check frequency – user007 Feb 17 '16 at 7:09