2017年5月7日星期日

I2C connection debug for NodeMCU



---
c1
c2
c3
Device found at address 0x68,sda2,scl4
Device found at address 0x76,sda2,scl4
c4
c5
c6
c7


---
[http://nodemcu.a1w.ca/post/150677174324/use-an-i2c-scanner]
-- This code is public domain, attribution to gareth@l0l.org.uk appreciated.

local pins = {1, 2, 4, 5, 6, 7, 8}
local scl, sda = nil

print("Searching for I2C device")

for scl = 1, 7 do
  for sda = 1, 7 do
    tmr.wdclr()
    if sda ~= scl then
      i2c.setup(0, sda, scl, i2c.SLOW)
      for i = 0, 127 do
        i2c.start(0)
        if i2c.address(0, i ,i2c.TRANSMITTER) then
          print("Device found at address 0x" .. string.format("%02X,sda%d,scl%d", i, sda, scl))
          --print("Device is wired: SDA to GPIO" .. pins[sda] .. " - IO index " .. sda)
          --print("Device is wired: SCL to GPIO" .. pins[scl] .. " - IO index " .. scl)
        else
          --
        end
        i2c.stop(0)
        --print(string.format("%02X,a%d,c%d", i, sda, scl))
      end
    end
  end
  print(string.format("c%d",  scl))
end

---

Accelor Sensor:
The slave address of the MPU-9250 is b110100X which is 7 bits long. The LSB bit of the 7 bit address is determined by the logic level on pin AD0. This allows two MPU-9250s to be connected to the same I2C bus.  When used in this configuration, the address of the one of the devices should be b1101000 (pin AD0 is logic low) and the address of the other should be b1101001 (pin AD0 is logic high).

The reset value is 0x00 for all registers other than the registers below.
• Register 107 (0x01) Power Management 1
• Register 117 (0x71) WHO_AM_I

Magnet Sensor:
WIA reg =00H val =48H


BMP280 Sensor:
The 7-bit device address is 111011x. The 6 MSB bits are fixed. The last bit is changeable by SDO value and can be changed during operation. Connecting SDO to GND results in slave address 1110110 (0x76); connection it to VDDIO results in slave address 1110111 (0x77)



----

id  = 0
sda = 2
scl = 4

-- initialize i2c, set pin1 as sda, set pin2 as scl
i2c.setup(id, sda, scl, i2c.SLOW)

-- user defined function: read from reg_addr content of dev_addr
function read_reg(dev_addr, reg_addr)
    i2c.start(id)
    ack=i2c.address(id, dev_addr, i2c.TRANSMITTER)
    i2c.write(id, reg_addr)
    i2c.stop(id)
    i2c.start(id)
    i2c.address(id, dev_addr, i2c.RECEIVER)
    c = i2c.read(id, 1)
    i2c.stop(id)
    return c,ack
end

-- get content of register 0xAA of device 0x77
reg,ack = read_reg(0x68, 0x75)
print(string.format("%02x",string.byte(reg))) --return 0x73 or 0x71
print(ack)
reg,ack = read_reg(0x68, 0x6B)
print(string.format("%02x",string.byte(reg)))  --return 0x01
print(ack)
reg,ack = read_reg(0x76, 0xD0)
print(string.format("%02x",string.byte(reg))) --return 0x58
print(ack)

没有评论: