3

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);
Kevin Reid
  • 7,444
  • 1
  • 25
  • 44
md.jamal
  • 163
  • 9
  • 2
    54 in binary is 110110,27 is 11011, the last bit seems to get lost have you seen the waveforms of MOSI and MISO on an Oscilloscope or Logicanalyzer? Try enabling the CS before the debugOutput call, and try disabling the delayUs in the beginning and observe the data waveforms. – Abel Tom Mar 12 '18 at 09:41
  • I have updated the code after making modifications. Still no help. The value received is half. I have to try Logic Analyzer. I have buspirate. But it will not work as it doesn't support slave mode – md.jamal Mar 12 '18 at 09:49
  • 2
    This sort of problem usually happens because the mode or capture edge is incorrect; what is your port setup? – Peter Smith Mar 12 '18 at 10:04
  • Pin Configuration is correct. SDO3 is output, SDI3 is input , SCK3 is output and SS3 is output – md.jamal Mar 12 '18 at 10:07
  • 1
    @md.jamal You have set PPRE and SPRE to 1:1, This is from the reference manual : Do not set the primary and secondary prescalers to the value of 1:1 at the same time. Page 9 http://ww1.microchip.com/downloads/en/DeviceDoc/70005185a.pdf – Abel Tom Mar 12 '18 at 10:22
  • Commenting the line which set SPRE did the job. Now I am getting the same value which I am transmitting in loopback.. Thanks. I can accept it as answer if you post. – md.jamal Mar 12 '18 at 10:52

1 Answers1

4

You have set PPRE bits and SPRE bits in SSPI3CON1 register to 1:1, This is from the Page 9 of the reference manual

Do not set the primary and secondary prescalers to the value of 1:1 at the same time.

Abel Tom
  • 435
  • 5
  • 15