Skip to content

Commit ab94c36

Browse files
committed
Initial implementation.
1 parent 7c1090a commit ab94c36

File tree

1 file changed

+133
-6
lines changed

1 file changed

+133
-6
lines changed

adafruit_mcp9600.py

Lines changed: 133 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@
4444
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
4545
"""
4646

47-
# imports
48-
49-
__version__ = "0.0.0-auto.0"
50-
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP9600.git"
51-
5247
from struct import unpack
5348
from micropython import const
5449
from adafruit_bus_device.i2c_device import I2CDevice
50+
from adafruit_register.i2c_struct import UnaryStruct, ROUnaryStruct
51+
from adafruit_register.i2c_bits import RWBits, ROBits
52+
from adafruit_register.i2c_bit import RWBit, ROBit
53+
54+
__version__ = "0.0.0-auto.0"
55+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP9600.git"
5556

5657
_DEFAULT_ADDRESS = const(0x67)
5758

@@ -61,9 +62,115 @@
6162
_REGISTER_THERM_CFG = const(0x05)
6263
_REGISTER_VERSION = const(0x20)
6364

64-
class MCP9600():
65+
66+
class BurstModeSamples:
67+
"""An enum-like class representing the options for number of burst mode samples."""
68+
BURST_SAMPLES_1 = 0b000
69+
BURST_SAMPLES_2 = 0b001
70+
BURST_SAMPLES_4 = 0b010
71+
BURST_SAMPLES_8 = 0b011
72+
BURST_SAMPLES_16 = 0b100
73+
BURST_SAMPLES_32 = 0b101
74+
BURST_SAMPLES_64 = 0b110
75+
BURST_SAMPLES_128 = 0b111
76+
77+
78+
class ShutdownModes:
79+
"""An enum-like class representing the options for shutdown modes"""
80+
NORMAL = 0b00
81+
SHUTDOWN = 0b01
82+
BURST = 0b10
83+
84+
85+
class MCP9600:
6586
"""Interface to the MCP9600 thermocouple amplifier breakout"""
6687

88+
# Alert temperature monitor options
89+
AMBIENT = 1
90+
THERMOCOUPLE = 0
91+
92+
# Temperature change type to trigger alert. Rising is heating up. Falling is cooling down.
93+
RISING = 1
94+
FALLING = 0
95+
96+
# Alert output options
97+
ACTIVE_HIGH = 1
98+
ACTIVE_LOW = 0
99+
100+
# Alert mode options
101+
INTERRUPT = 1 # Interrupt clear option must be set when using this mode!
102+
COMPARATOR = 0
103+
104+
# TODO: Get clarification on this and potentially update names
105+
# Interrupt clear options
106+
CLEAR_INTERRUPT_FLAG = 1
107+
NORMAL_STATE = 0
108+
109+
# Ambient (cold-junction) temperature sensor resolution options
110+
AMBIENT_RESOLUTION_0_0625 = 0 # 0.0625 degrees Celsius
111+
AMBIENT_RESOLUTION_0_25 = 1 # 0.25 degrees Celsius
112+
113+
# STATUS - 0x4
114+
burst_complete = RWBit(0x4, 7)
115+
temperature_update = RWBit(0x4, 6)
116+
input_range = ROBit(0x4, 4)
117+
alert_1 = ROBit(0x4, 0)
118+
alert_2 = ROBit(0x4, 1)
119+
alert_3 = ROBit(0x4, 2)
120+
alert_4 = ROBit(0x4, 3)
121+
# Device Configuration - 0x6
122+
ambient_resolution = RWBit(0x6, 7)
123+
burst_mode_samples = RWBits(3, 0x6, 2)
124+
shutdown_mode = RWBits(2, 0x6, 0)
125+
# Alert 1 Configuration - 0x8
126+
_alert_1_interrupt_clear = RWBit(0x8, 7)
127+
_alert_1_monitor = RWBit(0x8, 4)
128+
_alert_1_temp_direction = RWBit(0x8, 3)
129+
_alert_1_state = RWBit(0x8, 2)
130+
_alert_1_mode = RWBit(0x8, 1)
131+
_alert_1_enable = RWBit(0x8, 0)
132+
"""Set to ``True`` to enable alert output. Set to ``False`` to disable alert output."""
133+
# Alert 2 Configuration - 0x9
134+
_alert_2_interrupt_clear = RWBit(0x9, 7)
135+
_alert_2_monitor = RWBit(0x9, 4)
136+
_alert_2_temp_direction = RWBit(0x9, 3)
137+
_alert_2_state = RWBit(0x9, 2)
138+
_alert_2_mode = RWBit(0x9, 1)
139+
_alert_2_enable = RWBit(0x9, 0)
140+
# Alert 3 Configuration - 0xa
141+
_alert_3_interrupt_clear = RWBit(0xa, 7)
142+
_alert_3_monitor = RWBit(0xa, 4)
143+
_alert_3_temp_direction = RWBit(0xa, 3)
144+
_alert_3_state = RWBit(0xa, 2)
145+
_alert_3_mode = RWBit(0xa, 1)
146+
_alert_3_enable = RWBit(0xa, 0)
147+
# Alert 4 Configuration - 0xb
148+
_alert_4_interrupt_clear = RWBit(0xb, 7)
149+
_alert_4_monitor = RWBit(0xb, 4)
150+
_alert_4_temp_direction = RWBit(0xb, 3)
151+
_alert_4_state = RWBit(0xb, 2)
152+
_alert_4_mode = RWBit(0xb, 1)
153+
_alert_4_enable = RWBit(0xb, 0)
154+
# Alert 1 Hysteresis - 0xc
155+
_alert_1_hysteresis = UnaryStruct(0xc, ">H")
156+
# Alert 2 Hysteresis - 0xd
157+
_alert_2_hysteresis = UnaryStruct(0xd, ">H")
158+
# Alert 3 Hysteresis - 0xe
159+
_alert_3_hysteresis = UnaryStruct(0xe, ">H")
160+
# Alert 4 Hysteresis - 0xf
161+
_alert_4_hysteresis = UnaryStruct(0xf, ">H")
162+
# Alert 1 Limit - 0x10
163+
_alert_1_temperature_limit = UnaryStruct(0x10, ">H")
164+
# Alert 2 Limit - 0x11
165+
_alert_2_limit = UnaryStruct(0x11, ">H")
166+
# Alert 3 Limit - 0x12
167+
_alert_3_limit = UnaryStruct(0x12, ">H")
168+
# Alert 4 Limit - 0x13
169+
_alert_4_limit = UnaryStruct(0x13, ">H")
170+
# Device ID/Revision - 0x20
171+
_device_id = ROBits(8, 0x20, 8, register_width=2, lsb_first=False)
172+
_revision_id = ROBits(8, 0x20, 0, register_width=2)
173+
67174
types = ("K", "J", "T", "N", "S", "E", "B", "R")
68175

69176
def __init__(self, i2c, address=_DEFAULT_ADDRESS, tctype="K", tcfilter=0):
@@ -82,6 +189,26 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS, tctype="K", tcfilter=0):
82189
self.buf[1] = tcfilter | (ttype << 4)
83190
with self.i2c_device as tci2c:
84191
tci2c.write(self.buf, end=2)
192+
if self._device_id != 0x40:
193+
raise RuntimeError("Failed to find MCP9600 - check wiring!")
194+
195+
def alert_config(self, *, alert_number, alert_temp_source, alert_temp_limit, alert_hysteresis,
196+
alert_temp_direction, alert_mode, alert_state):
197+
""" For rising temps, hysteresis is below alert limit; falling temps, above alert limit
198+
Alert is enabled by default when set. To disable, use alert_disable method."""
199+
setattr(self, '_alert_%d_monitor' % alert_number, alert_temp_source)
200+
setattr(self, '_alert_%d_temperature_limit' % alert_number, int(alert_temp_limit / 0.0625))
201+
setattr(self, '_alert_%d_hysteresis' % alert_number, alert_hysteresis)
202+
setattr(self, '_alert_%d_temp_direction' % alert_number, alert_temp_direction)
203+
setattr(self, '_alert_%d_mode' % alert_number, alert_mode)
204+
setattr(self, '_alert_%d_state' % alert_number, alert_state)
205+
setattr(self, '_alert_%d_enable' % alert_number, True)
206+
207+
def alert_disable(self, alert_number):
208+
setattr(self, '_alert_%d_enable' % alert_number, False)
209+
210+
def alert_interrupt_clear(self, alert_number, interrupt_clear=True):
211+
setattr(self, '_alert_%d_interrupt_clear' % alert_number, interrupt_clear)
85212

86213
@property
87214
def version(self):

0 commit comments

Comments
 (0)