ad_convter.py
1.7 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
73
'''
@ author: jie
@ tools: pycharm
@ content: AD转换实现类
@ date: 2021.6.18
'''
import smbus
import time
import logging
# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)
address = 0x48
#check your PCF8591 address by type in 'sudo i2cdetect -y -1' in terminal.
# def setup(Addr):
# global address
# address = Addr
def read(chn): #channel
if chn == 0:
bus.write_byte(address,0x40)
# control = 0x40
# write = bus.write_byte_data(address, control, 0)
if chn == 1:
bus.write_byte(address,0x41)
if chn == 2:
bus.write_byte(address,0x42)
if chn == 3:
bus.write_byte(address,0x43)
bus.read_byte(address) # dummy read to start conversion
return bus.read_byte(address)
def write(val):
temp = val # move string value to temp
temp = int(temp) # change string to integer
# print temp to see on terminal else comment out
bus.write_byte_data(address, 0x40, temp)
# if __name__ == '__main__':
# # setup(0x48)
# while True:
# print ("0x40",read(0))
# print ("0x41",read(1))
# print ("0x42",read(2))
# print ("0x43",read(3))
# time.sleep(1)
# bus = smbus.SMBus(1)
# address = 0x48
# A0 = 0x40
# class AdConvter():
# def __init__(self):
# self.A0 = 0x40
# self.A1 = 0x41
# self.A2 = 0x42
# self.A3 = 0x43
# # 读取
# def read(self):
# # bus = self.bus
# write = bus.write_byte_data(address, A0, 0)
# data = bus.read_byte(address)
# return data
# if __name__ == '__main__':
# ad = AdConvter()
# while True:
# value = ad.read()
# print (value)
# time.sleep(1)