Page 1 of 2

PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Tue Jan 13, 2015 8:26 am
by johnf999
I have a PIC 18F46J50 starter kit. I have a MAX31855 thermocouple interface connected with
RB1 as CS
RB2 as SCK2
RB2 as SDI2

The relevant code is:

Code: Select all
#include <pconfig.h>
#include <pps.h>
#include <spi.h>

   ANCON0 = 0xFF;                    // Default all pins to digital
   ANCON1 = 0xFF;

   PPSUnLock();
   PPSInput(PPS_SDI2, PPS_RP5);     // Remap SDI2 to RP5 (RB2)
   PPSOutput(PPS_RP13, PPS_SCK2);   // Remap SCK2 to RP13 (RC2)
   PPSLock();

   OpenSPI2(SPI_FOSC_64, MODE_01, SMPMID);


   LATBbits.LATB1 = 0;              // Chip Select (active low)
   unsigned char tmp = ReadSPI2();
   LATBbits.LATB1 = 1;


I'm using a working USB connection to display debugging info. The return value of ReadSPI2 is always zero, even when I disconnect RB2 from the MAX31855 and connect it to VDD, when PORTBbits.RB1 returns 1.

It seems to me that, even if I've cooked the MAX chip, I should still get 0xff from the read - unless:

a: the pin remapping is wrong, or
b: there is a bug in the SPI library, or
c: I've done something really stupid

Please vote for a, b, or c.

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Tue Jan 13, 2015 11:08 am
by PhilB
JohnF999,

Don't you have to write to the SPI before you can read a byte back? I don't know how ReadSPI works, but the routines that I use write a dummy byte to the SPI and get a byte back into the read register.

Regards,

Phil

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Tue Jan 13, 2015 2:57 pm
by ric
ReadSPI2() writes the dummy byte for you.
That's one of my dislikes of library functions. You usually have to find the source code for them to find out what they really do. Quicker just to write your own version.

Sorry, I don't know what your actual problem is, I haven't used this particular family.

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Tue Jan 13, 2015 4:04 pm
by drh
Is the ReadSPI2() function set up to read 32 bits?

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Wed Jan 14, 2015 2:15 am
by johnf999
Thanks to all for the responses.

I don't know whether you need to write a byte before reading, or whether ReadSPI2 does that for you, as ric said. So I added a write before read and it made no difference.

ReadSPI2() reads 8 bits - I call it 4 times to get the 32 bits from the MAX31855.

Where does one look for the library source code?

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Wed Jan 14, 2015 2:32 am
by ric
That depends upon if you are using C18 or XC8. Note you haven't mentioned which yet.

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Wed Jan 14, 2015 3:06 am
by johnf999
Sorry, I should have given the context. I'm using C18 - MPLAB 8.35

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Wed Jan 14, 2015 4:20 am
by ric
On my PC
C:\MCC18\src\pmc_common\SPI\spi2read.c

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Wed Jan 14, 2015 4:57 am
by johnf999
Ahh! Right under my nose!

I'll see if that helps.

Re: PIC 18F46J50 ReadSPI2 always returns zero

PostPosted: Thu Jan 15, 2015 8:35 am
by johnf999
It's now working. I'm not really sure why.

I remapped the unneeded SDI2INCK and SDO2, and also set the TRIS bits for them. I also changed the SPI mode from 01 to 00. None of which should have made a difference.

I've learned a lot about SPI along the way. Thanks for the help and support.