-
Notifications
You must be signed in to change notification settings - Fork 0
/
hx711.py
executable file
·98 lines (77 loc) · 1.75 KB
/
hx711.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
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):
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):
sum = self.read_average(times)
self.set_offset(sum)
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