PIC 18F46J50 ReadSPI2 always returns zero

PIC 18F46J50 ReadSPI2 always returns zero

Postby johnf999 » Tue Jan 13, 2015 8:26 am

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.
johnf999
 
Posts: 6
Joined: Tue Jan 13, 2015 7:52 am

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby PhilB » Tue Jan 13, 2015 11:08 am

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
PhilB
 
Posts: 12
Joined: Fri Aug 29, 2014 9:06 am
PIC experience: Professional 2-5 years with MCHP products

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby ric » Tue Jan 13, 2015 2:57 pm

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.
My new leslie-special website (for fans of The Great Race) :)
User avatar
ric
Verified identity
 
Posts: 368
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby drh » Tue Jan 13, 2015 4:04 pm

Is the ReadSPI2() function set up to read 32 bits?
User avatar
drh
Verified identity
 
Posts: 53
Joined: Tue May 27, 2014 3:31 pm
Location: Laguna Niguel, Calif.
PIC experience: Professional 5+ years with MCHP products

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby johnf999 » Wed Jan 14, 2015 2:15 am

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?
johnf999
 
Posts: 6
Joined: Tue Jan 13, 2015 7:52 am

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby ric » Wed Jan 14, 2015 2:32 am

That depends upon if you are using C18 or XC8. Note you haven't mentioned which yet.
My new leslie-special website (for fans of The Great Race) :)
User avatar
ric
Verified identity
 
Posts: 368
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby johnf999 » Wed Jan 14, 2015 3:06 am

Sorry, I should have given the context. I'm using C18 - MPLAB 8.35
johnf999
 
Posts: 6
Joined: Tue Jan 13, 2015 7:52 am

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby ric » Wed Jan 14, 2015 4:20 am

On my PC
C:\MCC18\src\pmc_common\SPI\spi2read.c
My new leslie-special website (for fans of The Great Race) :)
User avatar
ric
Verified identity
 
Posts: 368
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby johnf999 » Wed Jan 14, 2015 4:57 am

Ahh! Right under my nose!

I'll see if that helps.
johnf999
 
Posts: 6
Joined: Tue Jan 13, 2015 7:52 am

Re: PIC 18F46J50 ReadSPI2 always returns zero

Postby johnf999 » Thu Jan 15, 2015 8:35 am

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.
johnf999
 
Posts: 6
Joined: Tue Jan 13, 2015 7:52 am

Next

Return to SSP (IIC, SPI)

Who is online

Users browsing this forum: No registered users and 1 guest

cron