class MAX31856

MAX31856

Constants

CHIPS
CJ_RES

Cold-Junction Temperature Data Resolution

FAULTS
REG_1

Config Register 1


bit 7: Conversion Mode -> 1 (Normally Off Mode) bit 6: 1-shot -> 0 (off) bit 5: open-circuit fault detection -> 0 (off) bit 4: open-circuit fault detection type k -> 1 (on) bit 3: Cold-junction sensor disabled -> 0 (default) bit 2: Fault Mode -> 0 (default) bit 1: fault status clear -> 1 (clear any fault) bit 0: 50/60 Hz filter select -> 0 (60Hz)

REG_2

Config Register 2


bit 7: Reserved -> 0 bit 6: Averaging Mode 1 Sample -> 0 (default) bit 5: Averaging Mode 1 Sample -> 0 (default) bit 4: Averaging Mode 1 Sample -> 0 (default) bit 3: Thermocouple Type -> K Type (default) -> 0 (default) bit 2: Thermocouple Type -> K Type (default) -> 0 (default) bit 1: Thermocouple Type -> K Type (default) -> 1 (default) bit 0: Thermocouple Type -> K Type (default) -> 1 (default)

REG_3

Config Register 2


bit 7: Nil bit 6: Nil bit 5: Cold-Junction High Fault Threshold -> 0 (default) bit 4: Cold-Junction Low Fault Threshold -> 0 (default) bit 3: Thermocouple Temperature High Fault Threshold -> 0 (default) bit 2: Thermocouple Temperature Low Fault Threshold -> 0 (default) bit 1: Over-voltage or Undervoltage Input Fault -> 1 (default) bit 0: Thermocouple Open-Circuit Fault -> 1 (default)

REG_CJ

Read registers

REG_FAULT
REG_TC
TC_RES

Thermocouple Temperature Data Resolution

TYPES
VERSION

Attributes

chip[RW]
clock[RW]
type[RW]

Public Class Methods

new(type = :k, chip = 0, clock = 2_000_000) click to toggle source
# File lib/max31856.rb, line 91
def initialize(type = :k, chip = 0, clock = 2_000_000)
  @type = TYPES[type]
  @chip = CHIPS[chip]
  @clock = clock
end

Public Instance Methods

config(samples = 0x10) click to toggle source

Run once config

CR1_AVERAGE_1_SAMPLE 0x00 CR1_AVERAGE_2_SAMPLES 0x10 CR1_AVERAGE_4_SAMPLES 0x20 CR1_AVERAGE_8_SAMPLES 0x30 CR1_AVERAGE_16_SAMPLES 0x40

define CR1_VOLTAGE_MODE_GAIN_8 0x08 define CR1_VOLTAGE_MODE_GAIN_32 0x0C

Optionally set samples

# File lib/max31856.rb, line 130
def config(samples = 0x10)
  spi_work do |spi|
    spi.write(write_reg(REG_1))
    spi.write(write_reg([REG_2, (samples | type)]))
    spi.write(write_reg(REG_3))
  end
  sleep 0.2 # give it 200ms for conversion
end
read() click to toggle source

Read both

# File lib/max31856.rb, line 147
def read
  tc = cj = 0
  spi_work do |spi|
    cj = read_cj(spi.write(Array.new(4, 0xff).unshift(REG_CJ)))
    sleep 0.2
    tc = read_tc(spi.write(Array.new(4, 0xff).unshift(REG_TC)))
  end
  [tc, cj]
end
read_cj(raw) click to toggle source

Read cold-junction

# File lib/max31856.rb, line 160
def read_cj(raw)
  lb, mb, _offset = raw.reverse # Offset already on sum
  # MSB << 8 | LSB and remove last 2
  temp = ((mb << 8) | lb) >> 2

  # Handle negative
  temp -= 0x4000 unless (mb & 0x80).zero?

  # Convert to Celsius
  temp * CJ_RES
end
read_fault() click to toggle source

Read register faults

# File lib/max31856.rb, line 191
def read_fault
  spi_work do |spi|
    fault = spi.write(REG_FAULT, 0xff)
    p [fault, fault.last.to_s(2).rjust(8, '0')]
  end
end
read_tc(raw) click to toggle source

Read thermocouple

# File lib/max31856.rb, line 175
def read_tc(raw)
  fault, lb, mb, hb = raw.reverse
  FAULTS.each do |f, txt|
    raise txt if fault & f == 1
  end
  # MSB << 8 | LSB and remove last 5
  temp = ((hb << 16) | (mb << 8) | lb) >> 5

  # Handle negative
  temp -= 0x80000 unless (hb & 0x80).zero?

  # Convert to Celsius
  temp * TC_RES
end
spi_work() { |spi| ... } click to toggle source
# File lib/max31856.rb, line 97
def spi_work
  PiPiper::Spi.begin do |spi|
    # Set cpol, cpha
    PiPiper::Spi.set_mode(0, 1)

    # Setup the chip select behavior
    spi.chip_select_active_low(true)

    # Set the bit order to MSB
    spi.bit_order PiPiper::Spi::MSBFIRST

    # Set the clock divider to get a clock speed of 2MHz
    spi.clock clock

    spi.chip_select(chip) do
      yield spi
    end
  end
end
write_reg(ary) click to toggle source

Set 0x80 on first for writes

# File lib/max31856.rb, line 140
def write_reg(ary)
  ary[1..-1].unshift(ary[0] | 0x80)
end