1

#用树莓派读霍尔磁性传感器的寄存器值,程序是在网上找大佬的移植过来的,但是跑起来...

 3 years ago
source link: https://www.oschina.net/question/5367559_2323778
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

#用树莓派读霍尔磁性传感器的寄存器值,程序是在网上找大佬的移植过来的,但是跑起来老是会报错,请大佬帮我看下我究竟是哪里有问题,谢谢。 #GPIO设定的pin为 SDA=GPIO3,SCL=GPIO5

Delusion乄 发布于 今天 10:44

from smbus2 import SMBus import time import RPi.GPIO as GPIO PUD_DICT = { 2:GPIO.PUD_OFF, 0:GPIO.PUD_UP, 1:GPIO.PUD_DOWN } class Rpi: def _init_(self,*args,**kwargs): GPIO.setmode(GPIO.BOARD) def config_pin(self,*args,**kwargs): pin = kwargs["pin"] level = kwargs["level"] direct = kwargs["direct"] pull_up_down = kwargs.get("pud",None) if direct == 0: GPIO.setup(pin,GPIO.IN) elif direct == 1: GPIO.setup(pin,GPIO.OUT) GPIO.OUT(pin,level) if not pull_up_down: GPIO.setup(pin,GPIO.IN,pull_up_down=PUD_DICT[pull_up_down]) def get_pin_status(self,pin): return {"level":GPIO.input(pin)} class SoftwareI2C: def _init_(self,scl_pin,sda_pin): self._board = Rpi() self.scl_pin = scl_pin self.sda_pin = sda_pin self.i2c_delay = 100 self._board_config_pin(pin=self.scl_pin,direct=0,pud=0) self._board_config_pin(pin=self.scl_pin,direct=0,pud=0) def wait_microseconds(self,duration): begin_time = datetime.datetime.now() while True: end_time = datetime.datetime.now() d_time = end_time.microsecond - begin_time.microsecond if d_time > duration: break def pull(self,pin): """Drives the line and returns line status""" self._board.config_pin(pin,direct=1,level=0) self.wait_microseconds(self.i2c_delay) def release(self,pin): """Releases the line and returns ling status""" self._board.config_pin(pin,direct=1,level=1) self.wait_microseconds(self.i2c_delay) def release_wait(self,pin): """ direct=1,level=1)""" while not self.get_pin_status(pin)["level"]: self.wait_microseconds(self.i2c_delay) self.wait_microseconds(self.i2c_delay) def start(self): self.release(self.sda_pin) self.release_wait(self.scl_pin) self.pull(self.sda_pin) self.pull(self.scl_pin) def stop(self): self.pull(self.sda_pin) self.pull(self.scl_pin) self.wait_microseconds(self.i2c_delay) self.release(self.scl_pin) self.wait_microseconds(self.i2c_delay) self.release(self.sda_pin) def send_bit(self,bit): if bit: self.release(self.sda_pin) else: self.pull(self.sda_pin) self.release_wait(self.scl_pin) self.pull(self.scl_pin) self.pull(self.sda_pin) def read_bit(self): self.release(self.sda_pin) self._board.config_pin(self.sda_pin,direct=0,level=1) self.release_wait(self.scl_pin) bit = self._board.get_pin_status(self.sda_pin)["level"] self.pull(self.scl_pin) self.pull(self.sda_pin) return bit def send_byte(self,byte): for i in range(8): self.send_bit(byte & 0x80) byte = byte << 1 self.read_bit() def read_byte(self): byte = 0x00 for i in range(8): byte = (byte<<1) | self.read_bit() self.read_bit() return byte #3D Hall sensor(TLI493DA2B6HTSA1) write and read sw_i2c = SoftwareI2C() #Write 0x88 0x19 to reg0x10 sw_i2c.start() sw_i2c.send_byte(0x35<<1|0x00) sw_i2c.send_byte(0x10) sw_i2c.send_byte(0x88) sw_i2c.send_byte(0x19) sw_i2c.stop() time.sleep() #Read data from 0x00-0x06 sw_i2c.start() sw_i2c.send_byte(0x35<<1|0x00) sw_i2c.send_byte(0x20) sw_i2c.start() sw_i2c.send_byte(0x35<<1|0x01) print(sw_i2c.read_byte()) print(sw_i2c.read_byte()) print(sw_i2c.read_byte()) sw_i2c.stop()


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK