-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test_dac.py
More file actions
executable file
·73 lines (59 loc) · 1.75 KB
/
example_test_dac.py
File metadata and controls
executable file
·73 lines (59 loc) · 1.75 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
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
#
# FILE: example_test_dac.py
# AUTHOR: Converted from Arduino example
# PURPOSE: Test DAC mode
# URL: https://github.com/RobTillaart/AD5593R
#
# Outputs should have different output voltage
# from zero to full scale in 8 steps.
#
import time
from AD5593R import AD5593R, AD5593R_LDAC_DIRECT, AD5593R_LIB_VERSION
# Create AD5593R instance with I2C address 0x10
# Default bus number is 1 (for Raspberry Pi)
ad = AD5593R(0x10, bus_number=1)
print("AD5593R Test DAC")
print(f"AD5593R_LIB_VERSION: {AD5593R_LIB_VERSION}")
print()
# Initialize device
print(f"Connect: {ad.is_connected()}")
print(f"Address: 0x{ad.get_address():02X}")
print()
# Set all eight pins to DAC mode
ad.set_dac_mode(0xFF)
# Use internal Vref 2.5V
ad.set_external_reference(False, 5.0)
# Do not double the output
ad.set_dac_range_2x(False)
# COPY input register direct to DAC
# Must be set after set_external_reference()
ad.set_ldac_mode(AD5593R_LDAC_DIRECT)
# Read back default values
print("Read back default values (expect all zeros):")
for pin in range(8):
value = ad.read_dac(pin)
print(f"0x{value:03X}", end="\t")
print()
print()
try:
while True:
# Write 8 different values
print("Writing DAC values:")
for pin in range(8):
# Expect increasing values: 0, 585, 1170, 1755, 2340, 2925, 3510, 4095
value = (pin * 4095) // 7
result = ad.write_dac(pin, value)
print(f"{result}", end="\t")
print()
# Read back
print("Read back values:")
for pin in range(8):
value = ad.read_dac(pin)
print(f"0x{value:03X}", end="\t")
print()
print()
time.sleep(1)
except KeyboardInterrupt:
print("\nExiting...")
ad.close()