在研究使用HX711AD模块的电子秤的时候,没有发现适用于micropython的代码,所以本人照着c语言的版本自行写了一个,代码如下.
但是下列代码有些许问题,
1.在等待DT返回高电平的时候,偶尔会直接跳过等待阶段,直接执行24个电平检测信号,导致数据错误.
2.因为读取DT是靠高低电平,来写二进制的,数据精度难以保证.
from machine import Pin,Timer,PWM,ADC
import utime
import ustruct
import select
from micropython import const
HX711_SCK = Pin(4,Pin.OUT)
HX711_DT = Pin(13,Pin.IN)
GapValue=const(450)
select.poll()
def HX711_Read():
utime.sleep_us(1)
HX711_SCK.value(0)
count='0b'
utime.sleep_us(1)
print(HX711_DT.value())
if HX711_DT.value()==1:
for i in range(24):
HX711_SCK.value(1)
utime.sleep_us(1)
HX711_SCK.value(0)
utime.sleep_us(1)
print(HX711_DT.value())
if HX711_DT.value()==1:
count=count+'1'
else:
count=count+'0'
count = count.replace(count[2],'0')
print(count)
HX711_SCK.value(1)
utime.sleep_us(1)
count=int(count)/100
print(count)
HX711_SCK.value(0)
utime.sleep_us(1)
return count
def Get_Weight():
HX711_Buffer = HX711_Read()
Weight_Shiwu = HX711_Buffer
print(Weight_Shiwu)
Weight_Shiwu = Weight_Shiwu
Weight_Shiwu =float(Weight_Shiwu/GapValue)
return Weight_Shiwu
Weight = Get_Weight();
print(str(Weight)+'g')
|