I2C SMBus Python code explain - Raspberry Pi Forums


hi,

can me explaining following code written hmc5883l

code: select all

#!/usr/bin/python import smbus import time import math  bus = smbus.smbus(0) address = 0x1e   def read_byte(adr):     return bus.read_byte_data(address, adr)  def read_word(adr):     high = bus.read_byte_data(address, adr)     low = bus.read_byte_data(address, adr+1)     val = (high << 8) + low     return val  def read_word_2c(adr):     val = read_word(adr)     if (val >= 0x8000):         return -((65535 - val) + 1)     else:         return val  def write_byte(adr, value):     bus.write_byte_data(address, adr, value)  write_byte(0, 0b01110000) # set 8 samples @ 15hz write_byte(1, 0b00100000) # 1.3 gain lsb / gauss 1090 (default) write_byte(2, 0b00000000) # continuous sampling  scale = 0.92  x_out = read_word_2c(3) * scale y_out = read_word_2c(7) * scale z_out = read_word_2c(5) * scale  bearing  = math.atan2(y_out, x_out)  if (bearing < 0):     bearing += 2 * math.pi  print "bearing: ", math.degrees(bearing)
can understand address i2c address of sensor

adr?

it's used inside function read_byte(adr): never been called .

adr register address
"read byte" reads single register (1 byte)
"read word" reads 2 registers (1 byte each) , joins result give word (2 bytes)
other read word manages 2 complement words, managing sign.
understand commands, please take sensor datasheet


raspberrypi



Comments