I have two MAX31855 thermocouple IC’s connected to SPI5 MISO and SCK with 2 GPIO for the chip select. I am using an SCM210260D module. The MAX13855 is read only, 32 bits.
The first time I run the code, it reads back the internal cold junction temp on only one of the devices and then second read gets back all zero’s from both devices. Then all reads are now zero. If I swap the reads around, and read the second device first, it works and then all zero’s for subsequent reads so it doesn’t appear to be a hardware issue. I’ll hook up the scope shortly to check it out. The following is the code I am using to setup and read the sensors.
init code
private static SpiController spiController = SpiController.FromName(SC20260.SpiBus.Spi5);
private static GpioPin csLower = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PA0);
private static GpioPin csUpper = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PC2);
private static SpiConnectionSettings lowerSpiSettings = new SpiConnectionSettings()
{
DataFrameFormat = SpiDataFrame.MsbFirst,
ChipSelectType = SpiChipSelectType.Gpio,
ChipSelectLine = csLower,
Mode = SpiMode.Mode0,
ClockFrequency = 4000000,
ChipSelectHoldTime = TimeSpan.FromMilliseconds(5),
};
private static SpiConnectionSettings upperSpiSettings = new SpiConnectionSettings()
{
DataFrameFormat = SpiDataFrame.MsbFirst,
ChipSelectType = SpiChipSelectType.Gpio,
ChipSelectLine = csUpper,
Mode = SpiMode.Mode0,
ClockFrequency = 4000000,
ChipSelectHoldTime = TimeSpan.FromMilliseconds(5),
};
static SpiDevice lowerSpiDevice = spiController.GetDevice(lowerSpiSettings);
static SpiDevice upperSpiDevice = spiController.GetDevice(upperSpiSettings);
Code to read the sensor. Adapted from Adafruit Arduino code. This is the first sensor. There is another function that reads the second sensor. No optimisation yet to reduce duplication. I need it working first.
private static float readLower()
{
byte[] data = new byte[4];
lowerSpiDevice.Read(data);
ulong value = (ulong)((data[0] << 24) + (data[1] << 16) + (data[2] << 7) + data[3]);
ulong valueInternal = value >> 4;
float temp = valueInternal & 0x7FF;
if((valueInternal & 0x800) == 0x800)
{
Int16 tmp = (short) (0xF800 | (valueInternal & 0x7FF));
temp = tmp;
}
internalTemp1 = temp * 0.0625F;
ulong valueExternal = value;
if((valueExternal & 0x80000000) == 0x80000000)
{
valueExternal = 0xFFFFC000 | ((valueExternal >> 18) & 0x00003FFF);
}
float temp1 = valueExternal * 0.25F;
bool error = (value & (ulong) 0x1000) == 0x1000;
bool open = (value & (ulong) 0x01) == 0x01;
return temp1;
}