CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SPI trouble with MAX31856

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JAM2014



Joined: 24 Apr 2014
Posts: 144

View user's profile Send private message

SPI trouble with MAX31856
PostPosted: Thu Mar 19, 2026 4:06 pm     Reply with quote

Hi All,

Compiler Version: PCH v5.120
PIC: 18LF25K50, running at 3.3V
SPI Peripheral: MAX31856 (thermocouple to digital converter), also 3.3V

MAX31856 datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max31856.pdf

I'm working with SPI for the first time, and not getting the expected results.

Here are the relevant pin connections:

PIC SDO (pin 24) ---> MAX SDI (pin 12)
PIC SDI (pin 21) <--- MAX SDO (pin 11)
PIC SCK (pin 22) ---> MAX SCK (pin 10)
PIC CS (pin 23) ---> MAX CS (pin 9)

Relevant code:

Code:

// Max31856 Register Addresses
#define REG_CR0 0x00   // Control Reg. 1
#define REG_CR1 0x01   // Control Reg. 2
#define REG_LTCBH 0x0C    // Linearized TC Temperature, Byte 2
#define REG_LTCBM 0x0D  // Linearized TC Temperature, Byte 1
#define REG_LTCBL 0x0E  // Linearized TC Temperature, Byte 0

//Here we setup SPI1
#use spi(MASTER, SPI1, MODE=3, BITS=8, STREAM=MAX31856)


Code:

void max31856_write(unsigned int8 reg, unsigned int8 value) {
    output_low(TC_CS);
   delay_us(2);
    spi_xfer(MAX31856, reg | 0x80); // Set write bit
    spi_xfer(MAX31856, value);
   delay_us(2);
    output_high(TC_CS);
}

unsigned int8 max31856_read(unsigned int8 reg) {
    unsigned int8 value;
    output_low(TC_CS);
   delay_us(2);
    spi_xfer(MAX31856, reg & 0x7F); // Clear write bit
    value = spi_xfer(MAX31856, 0);
   delay_us(2);
    output_high(TC_CS);
    return value;
}

float GetTCData(){

//This routine uses the native CCS SPI routines to read the three temperature registers on the MAX31856 device...
    float TCtemp;
    unsigned int8 HighVal, MidVal, LowVal;
   signed int32 RawData;

   // Configure: CR0 = 0x00 (No fault auto-detect), CR1 = 0x07 (T-type)
    max31856_write(REG_CR0, 0x00);
    max31856_write(REG_CR1, 0x07);

    // Read 3-byte temperature data (linearized)
    output_low(TC_CS);
   delay_us(2);
    spi_xfer(MAX31856, REG_LTCBH & 0x7F);
    HighVal = spi_xfer(MAX31856, 0);
    MidVal = spi_xfer(MAX31856, 0);
    LowVal = spi_xfer(MAX31856, 0);
   delay_us(2);
    output_high(TC_CS);

   fprintf(Console, "Hi:%u Mid:%u Low:%u\n\r", HighVal, MidVal, LowVal);

   // Convert to Celsius: Sign-extend and divide by 128
    RawData = ((int32)HighVal << 16) | ((int32)MidVal << 8) | LowVal;
    if(RawData & 0x800000) RawData |= 0xFF000000; // Handle negative
    TCtemp = (float)RawData / 128.0;

   return TCtemp;
}


When I call GetTCData(), it returns the three data bytes, High, Mid and Low, as all 0's. I put a scope on the CS connection, and I am seeing the CS line being asserted. I'm also seeing clock activity on the SCK line.

Code:

Hi:0 Mid:0 Low:0


Based on the MAX31856 data sheet, I believe that SPI mode 3 is correct, but I'm not 100% sure of that. Other modes do not seem to work!

I'm using the SOIC version of the 18LF25K50, and I believe I've got the correct pins for hardware SPI on this device.

Any thoughts on what I might be doing wrong?

Thanks!

Jack
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group