Guest User

Untitled

a guest
Dec 27th, 2017
147
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import utime
  2. import machine # import Pin
  3. from machine import Pin
  4. import time
  5.  
  6. class HX711:
  7.  
  8. def __init__(self, pd_sck=4, dout=5, gain=128):
  9. self.gain = gain
  10. self.dataPin = Pin(dout, Pin.IN)
  11. self.pdsckPin = Pin(pd_sck, Pin.OUT, value=0)
  12. self.powerDown()
  13. self.powerUp()
  14. self.read()
  15.  
  16. def isready(self):
  17. time.sleep(.001)
  18. return self.dataPin.value() == 0
  19.  
  20. def read(self):
  21. self.powerUp()
  22. while not self.isready():
  23. pass
  24. print("<waiting finished> dataPin: {}, sckPin: {}".format(self.dataPin.value(), self.pdsckPin.value()))
  25. my = 0
  26. now = utime.ticks_us()
  27. for i in range(24):
  28. now = utime.ticks_us()
  29. self.pdsckPin.value(1)
  30. self.pdsckPin.value(0)
  31. data = self.dataPin.value()
  32. myus += ", " + str(utime.ticks_diff(utime.ticks_us(), now))
  33. mydata += str(data)
  34. my = ( my << 1) | data
  35. print("bitbanged: ", my)
  36. print("us: ", myus)
  37. print("data: ", mydata)
  38.  
  39. for i in range(3):
  40. self.pdsckPin.value(1)
  41. utime.sleep_us(2)
  42. self.pdsckPin.value(0)
  43. self.powerDown()
  44.  
  45. def powerDown(self):
  46. self.pdsckPin.value(0)
  47. self.pdsckPin.value(1)
  48. utime.sleep_us(80)
  49.  
  50. def powerUp(self):
  51. self.pdsckPin.value(0)
RAW Paste Data