0
class LIS2DH_Class(object):
mySpi = 0
def __init__(self):
    self.spi = spidev.SpiDev()
    self.spi.open(0,0)
    self.spi.max_speed_hz = 10000000 #10 Mhz is max limit on LIS2DH SPI communication
def whoAmI(self):
    resp = self.spi.xfer2([0x8f,0x8f])
    return resp
print accel.whoAmI()

above is the code i wrote for an LIS2DH accelerometer. 0x0F is the whoami register. I'm bitwise OR with 0x80 to send a read command, followed by the register address. see protocol below;

this code above will return [255,51]

51 is the expected value, 255 is garbage. How can i reduce a read command to just sending 1 byte?

Spi read protocol

3
  • I do not understand the problem. You need to send two bytes. The first byte is the command, the second byte is irrelevant. Two bytes are returned. The first byte is irrelevant, the second byte is the read data. That is a typical SPI command/response.
    – joan
    May 4, 2016 at 21:19
  • @joan why do i need to send 2 bytes? the first byte is the command, and i should expect a response of 0x33 = 51. why is it returning 0xFF?
    – tritium_3
    May 4, 2016 at 21:24
  • @joan I understand now, i need to send 16 clock pulses, so in this case, should i send 0x8F00, to recieve my data back?
    – tritium_3
    May 4, 2016 at 21:26

1 Answer 1

2

Thank you @JOAN for clearing things up.

need to send 1st byte as command, and then shift out another 8 bits to get my return data. here are two functions that are working for me

def readRegister(self,regAddr):
    address = 0x80 | regAddr 
    resp = self.spi.xfer2([address,0x00])
    return resp[1]

def writeRegister(self,regAddr,value):
    self.spi.xfer2([regAddr,value])
1
  • After a day or two you can mark the answer correct.
    – joan
    May 4, 2016 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.