use of basic I2C communication #9418
-
Can anyone help me with using basic I2C functions from machine library? I'm trying to write a class for VEL6075 chip (https://datasheetspdf.com/pdf-file/1103874/Vishay/VEML6075/1). It has 16 bit registers where typical size register in many applications is 8bit. I'm limited to machine library. I came with this code import machine
import time
SCL_PIN=machine.Pin(1)
SDA_PIN=machine.Pin(0)
i2c=machine.I2C(0,scl=SCL_PIN, sda=SDA_PIN,freq=400000)
buf=bytearray(2)
i2c.readfrom_mem_into(0x10, 0x0c, buf, addrsize=16)
print(buf[0], buf[1]) but it do not return 0x26 chip ID, but value 0x08. If i try to read 0x07 or 0x09 then i get 0x00. its not working. Ok, then i've tried: VEL=machine.SoftI2C(scl=SCL_PIN, sda=SDA_PIN,freq=400000)
VEL.start()
x1=bytearray(1)
x1[0]=0x10 << 1 | 1
VEL.write(x1)
time.sleep(0.03)
VEL.write(bytes(0x0c))
time.sleep(0.03)
VEL.start()
VEL.write(bytes(0x10 << 1))
time.sleep(0.03)
b1=bytearray(1)
b2=bytearray(2)
VEL.readinto(b1)
VEL.readinto(b2)
VEL.stop() But with this its even worse as b1 and b2 are 0xff. How to properly read 16bit registers? Or how to use properly "primitive" functions to get communication to I2C. (Edit by @jimmo to add code formatting) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Please see the welcome post for tips about code formatting. Have you seen this driver? https://github.com/neliogodoi/MicroPython-VEML6075/blob/master/veml6075.py |
Beta Was this translation helpful? Give feedback.
-
ok, true... cables are not highest quality and had to check cables. It seems to work now. Thanks! Now it works as a charm. I still have issues to understand that code. Can you explain how he reads 16 bit registers here? |
Beta Was this translation helpful? Give feedback.
ok, true... cables are not highest quality and had to check cables. It seems to work now. Thanks! Now it works as a charm.
I still have issues to understand that code. Can you explain how he reads 16 bit registers here?
self._i2c.readfrom_mem(self._addr, register, 2)
will read 2x8bit from 2 registers to my understanding. So if i will read 0x10 it will read 0x10 & 0x11. It will not grab 0x1000 register as a whole, unless i misunderstand the concept, meaning reading 2 bytes will continue reading from the same register as its simply 2 bytes long. My imagination tells me that it will read 2x8bit from 2 registers not from 1.