Class: MAX31856
- Inherits:
-
Object
- Object
- MAX31856
- Defined in:
- lib/max31856.rb,
lib/max31856/version.rb
Overview
MAX31856
Constant Summary collapse
- TC_RES =
Thermocouple Temperature Data Resolution
0.0078125
- CJ_RES =
Cold-Junction Temperature Data Resolution
0.015625
- REG_CJ =
Fault status register
0x08
- REG_TC =
Fault status register
0x0c
- REG_FAULT =
Fault status register
0x0F
- REG_1 =
Config Register 1
bit 7: Conversion Mode -> 0 (Normally Off Mode) bit 6: 1-shot -> 1 (ON) bit 5: open-circuit fault detection -> 0 (off) bit 4: open-circuit fault detection -> 0 (off) bit 3: Cold-junction temerature sensor enabled -> 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)
[0x00, 0b01000010].freeze
- 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)
0x01
- TYPES =
{ b: 0x00, e: 0x01, j: 0x02, k: 0x03, n: 0x04, r: 0x05, s: 0x06, t: 0x07 }.freeze
- CHIPS =
{ 0 => PiPiper::Spi::CHIP_SELECT_0, 1 => PiPiper::Spi::CHIP_SELECT_1, 2 => PiPiper::Spi::CHIP_SELECT_BOTH, 3 => PiPiper::Spi::CHIP_SELECT_NONE }.freeze
- FAULTS =
{ 0x80 => 'Cold Junction Out-of-Range', 0x40 => 'Thermocouple Out-of-Range', 0x20 => 'Cold-Junction High Fault', 0x10 => 'Cold-Junction Low Fault', 0x08 => 'Thermocouple Temperature High Fault', 0x04 => 'Thermocouple Temperature Low Fault', 0x02 => 'Overvoltage or Undervoltage Input Fault', 0x01 => 'Thermocouple Open-Circuit Fault' }.freeze
- VERSION =
'0.1.0'.freeze
Instance Attribute Summary collapse
-
#chip ⇒ Object
Returns the value of attribute chip.
-
#clock ⇒ Object
Returns the value of attribute clock.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #config ⇒ Object
-
#initialize(type = :k, chip = 0, clock = 2_000_000) ⇒ MAX31856
constructor
A new instance of MAX31856.
- #read ⇒ Object
- #read_cj(raw) ⇒ Object
-
#read_fault ⇒ Object
Read register faults.
- #read_tc(raw) ⇒ Object
- #spi_work ⇒ Object
Constructor Details
Instance Attribute Details
#chip ⇒ Object
Returns the value of attribute chip.
8 9 10 |
# File 'lib/max31856.rb', line 8 def chip @chip end |
#clock ⇒ Object
Returns the value of attribute clock.
8 9 10 |
# File 'lib/max31856.rb', line 8 def clock @clock end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/max31856.rb', line 8 def type @type end |
Instance Method Details
#config ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/max31856.rb', line 102 def config spi_work do |spi| spi.write(REG_2, type) spi.write(REG_1) end sleep(0.2) # give it 200ms for conversion end |
#read ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/max31856.rb', line 110 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) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/max31856.rb', line 120 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 ⇒ Object
Read register faults
148 149 150 151 152 153 |
# File 'lib/max31856.rb', line 148 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) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/max31856.rb', line 132 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 ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/max31856.rb', line 82 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 |