JAM2014
Joined: 24 Apr 2014 Posts: 144
|
| SPI trouble with MAX31856 |
Posted: Thu Mar 19, 2026 4:06 pm |
|
|
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.
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 |
|