I am testing SPI of my PIC24E512GU814 microcontroller. In order to test, I connected MOSI to MISO.
I wrote the following code for SPI initialization:
SPI3CON1 = 0;
SPI3CON1bits.CKE = 1;
SPI3CON1bits.SPRE = 7;
SPI3CON1bits.PPRE = 3;
SPI3CON1bits.MSTEN = 1;
SPI3STATbits.SPIEN = 1;
Then for transmitting/receiving the following code:
SPI3_ENABLE = 0; //chip select
delayUs(1);
while (SPI3STATbits.SPITBF);
SPI3BUF = test;
while (!SPI3STATbits.SPIRBF)
{
if (--timeout == 0)
{
debugOutput(0, "\r\nTimeout SPI 3\r\n");
}
}
temp = SPI3BUF;
debugOutput(0, "\r\nValue read on SPI 3 Port:%d\r\n", temp);
SPI3_ENABLE = 1;
I am always getting half the value transmitted. For example if I transmit 54, I am reading 27 in the receive buffer. What can be the issue?
Modified the code to the following didn't help
delayUs(1);
//writeSPI(eSpiPort3, &test, sizeof(byte));
debugOutput(0, "\r\nWriting on SPI 3 Port:%d\r\n", test);
SPI3_ENABLE = 0;
while (SPI3STATbits.SPITBF);
SPI3BUF = test;
while (!SPI3STATbits.SPIRBF)
{
if (--timeout == 0)
{
debugOutput(0, "\r\nTimeout SPI 3\r\n");
}
}
temp = SPI3BUF;
SPI3_ENABLE = 1;
delayUs(1);
debugOutput(0, "\r\nValue read on SPI 3 Port:%d\r\n", temp);