-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAD7416ARZ.py
More file actions
executable file
·61 lines (49 loc) · 2.11 KB
/
Copy pathAD7416ARZ.py
File metadata and controls
executable file
·61 lines (49 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# AD7416ARZ
# This code is designed to work with the AD7416ARZ_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/content/Analog-Digital-Converters?sku=AD7416ARZ_I2CS#tabs-0-product_tabset-2
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# I2C Address of the device
AD7416ARZ_DEFAULT_ADDRESS = 0x48
# AD7416ARZ Register Map
AD7416ARZ_REG_TEMP = 0x00 # Temperature Value
AD7416ARZ_REG_CONFIG = 0x01 # Configuration Register
AD7416ARZ_REG_THYST = 0x02 # THYST setpoint
AD7416ARZ_REG_TOTI = 0x03 # TOTI setpoint
# AD7416ARZ Configuration Register
AD7416ARZ_FAULTQUEUE_1 = 0x00 # Fault Queue = 1
AD7416ARZ_FAULTQUEUE_2 = 0x08 # Fault Queue = 2
AD7416ARZ_FAULTQUEUE_4 = 0x10 # Fault Queue = 4
AD7416ARZ_FAULTQUEUE_6 = 0x18 # Fault Queue = 6
AD7416ARZ_MODE_CMP = 0x00 # Comparater Mode
AD7416ARZ_MODE_INT = 0x02 # Interrupt Mode
AD7416ARZ_MODE_SHUTDOWN = 0x01 # Shutdown Mode
class AD7416ARZ():
def __init__(self):
self.temp_configuration()
def temp_configuration(self):
"""Select the temperature configuration from the given provided values"""
TEMP_CONFIG = (AD7416ARZ_FAULTQUEUE_1 | AD7416ARZ_MODE_CMP)
bus.write_byte_data(AD7416ARZ_DEFAULT_ADDRESS, AD7416ARZ_REG_CONFIG, TEMP_CONFIG)
def read_temp(self):
"""Read data back from AD7416ARZ_REG_TEMP(0x00), 2 bytes, temp MSB, temp LSB"""
data = bus.read_i2c_block_data(AD7416ARZ_DEFAULT_ADDRESS, AD7416ARZ_REG_TEMP, 2)
# Convert the data to 10-bits
temp = ((data[0] * 256) + (data[1] & 0xC0)) / 64
if temp > 511 :
temp -= 1024
cTemp = temp * 0.25
fTemp = cTemp * 1.8 + 32
return {'c' : cTemp, 'f' : fTemp}
from AD7416ARZ import AD7416ARZ
ad7416arz = AD7416ARZ()
while True:
temp = ad7416arz.read_temp()
print "Temperature in Celsius : %.2f C"%(temp['c'])
print "Temperature in Fahrenheit : %.2f F"%(temp['f'])
print " ***************************************** "
time.sleep(1)