-
Notifications
You must be signed in to change notification settings - Fork 0
/
hx711_2.py
executable file
·109 lines (88 loc) · 2.03 KB
/
hx711_2.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import mraa
class Hx711:
def __init__(self,dout,pd_sck, gain = 128):
# self.PD_SCK = pd_sck
# self.DOUT = dout
self.PD_SCK = mraa.Gpio(pd_sck)
self.DOUT = mraa.Gpio(dout)
self.PD_SCK.dir(mraa.DIR_OUT)
self.DOUT.dir(mraa.DIR_IN)
self.GAIN = 1
self.OFFSET = 0
self.SCALE = 1
self.set_gain(gain)
def set_gain(self,gain):
if gain == 128:
self.GAIN = 1
elif gain == 64:
self.GAIN = 3
elif gain == 32:
self.GAIN = 2
self.PD_SCK.write(0) # is for low signal
self.read()
def read(self):
while self.DOUT.read() == 1:
continue
value = 0
data = 0
filler = 0x00
for i in range(24):
self.PD_SCK.write(1)
value = value<<1
self.PD_SCK.write(0)
if(self.DOUT.read()):
value = value + 1
self.PD_SCK.write(1)
value = value ^ 0x800000
self.PD_SCK.write(0)
return value
def read_average(self,times):
abuf = []
for i in range(10):
abuf.append(self.read())
abuf.sort()
aval = (abuf[4] + abuf[3] + abuf[5] + abuf[6])/4
return aval
# sum0 = 0
# for i in range(times):
# sum0 += self.read()
# return sum0/times;
def get_value(self,times):
return self.read_average(times) - self.OFFSET
def get_units(self,times):
return self.get_value(times) /self.SCALE
def tare(self, times):
abuf = []
for i in range(10):
abuf.append(self.read())
abuf.sort()
aval = (abuf[4] + abuf[3] + abuf[5] + abuf[6])/4
#sum = self.read_average(times)
self.set_offset(aval)
def set_scale(self,scale):
self.SCALE = scale
def get_scale(self):
return self.SCALE
def get_offset(self):
return self.OFFSET
def set_offset(self,offset):
self.OFFSET = offset;
def power_down(self):
self.PD_SCK.write(0)
self.PD_SCK.write(1)
def power_up(self):
self.PD_SCK.write(0)
# def shiftIn(self):
# value =0
# for i in range(24):
# self.PD_SCK.write(1)
# value = value<<1
# self.PD_SCK.write(0)
# if(self.DOUT.read()):
# value = value + 1
# return value
# def is_ready(self):
# if self.DOUT.read() == 0:
# return True
# else:
# return False