-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5-2-adc-sar.py
36 lines (29 loc) · 980 Bytes
/
5-2-adc-sar.py
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
import RPi.GPIO as GPIO
import time
DAC = [26, 19, 13, 6, 5, 11, 9, 10]
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
troyka = 17
comparator = 4
GPIO.setup(troyka, GPIO.OUT)
GPIO.setup(comparator, GPIO.IN)
GPIO.output(troyka, 1)
for pin in DAC: GPIO.setup(pin, GPIO.OUT, initial = GPIO.HIGH)
def decimal2binary(value):
return [int(element) for element in bin(value)[2:].zfill(8)]
try:
while True:
for i in range(1, 8 + 1):
voltage = 0
value_counter = 0
time.sleep(0.001)
value = 128 // i
comparator_value = GPIO.input(comparator)
if comparator_value == 1:
voltage += value / 256 * 3.3
value_counter += value
for i in range(len(DAC)):
GPIO.output(DAC[i], decimal2binary(value)[i])
print ('ADC value =', value, 'ADC voltage = ', voltage, ' ')
finally:
for pin in DAC: GPIO.output(pin, 0)