I'm trying to read the data from a sensor where I need to send it a 16-bit register addres
This is the diagram from the datasheet
When reading: Set the LSB of slave address to 1 so that is ABh (1010_1011b).
How should I do this in uPython?
Read data from 16-bit register addres (I2C)
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Read data from 16-bit register addres (I2C)
This seems to be an ad hoc protocol and I think you have no choice but to "bit bang" it - i.e. control the two I/O pins one bit at a time. The data line is presumably a bidirectional open-drain interface with a pullup resistor, and the SACK is a zero returned from the device. A bit banged solution could be concocted so long as there aren't any severe timing constraints.
If you're unfamiliar with the techniques your best bet would be to find and study existing examples.
If you're unfamiliar with the techniques your best bet would be to find and study existing examples.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Read data from 16-bit register addres (I2C)
This is a standard I2C protocol, and you can do it using http://docs.micropython.org/en/latest/p ... adfrom_mem with the addrsize set to "16", for example:
If that doesn't work (addrsize is not implemented in all ports), you can take a look at my driver for the vl6180 sensor: https://bitbucket.org/thesheep/micropyt ... 0.py-26:30
Code: Select all
from machine import I2C, Pin
i2c =machine.I2C(sda=Pin(4), scl=Pin(5))
value = i2c.readfrom_mem(0x55, 0x2233, 1, addrsize=16)
Re: Read data from 16-bit register addres (I2C)
Thank you both for your answer.
I'm not familiar with the ustruct module, so I'm reading your _get_reg16 method but I still don't get how to implement it, my confusion is because I was doing it a little bit different in c++:
can you help me a little bit with this?
I'm looking examples of the struct module to understand what I'm doing
addrsize is not implemented yet in the esp32 port, so I'm trying with your second suggestion.deshipu wrote: ↑Sat Oct 28, 2017 7:57 amIf that doesn't work (addrsize is not implemented in all ports), you can take a look at my driver for the vl6180 sensor: https://bitbucket.org/thesheep/micropyt ... 0.py-26:30
I'm not familiar with the ustruct module, so I'm reading your _get_reg16 method but I still don't get how to implement it, my confusion is because I was doing it a little bit different in c++:
Code: Select all
uint8_t SSS::read(uint16_t addr) {
uint8_t out;
uint8_t input[ 2 ];
input[ 0 ] = (addr & 0xFF00) >> 8;
input[ 1 ] = addr & 0x00FF;
Wire.beginTransmission(i2c_address); // start transmission
Wire.write(input, 2); // sends register
Wire.endTransmission(); // end transmission
delay(35);
Wire.requestFrom(i2c_address, 1);// send data n-bytes read
while(Wire.available() == 0);
out = Wire.read();
Wire.endTransmission(); // end transmission
return out;
}
I'm looking examples of the struct module to understand what I'm doing
Last edited by gepd on Sat Oct 28, 2017 9:31 pm, edited 1 time in total.
Re: Read data from 16-bit register addres (I2C)
You can pretty much copy the _get_reg16 or _get_reg8 methods (depending on whether the value you are reading is 16 or 8 bit). There is no need to modify them. I don't understand what you are trying to do with your modifications. Just specify the address as a 16-bit number, and it should work.
Re: Read data from 16-bit register addres (I2C)
You're right. I did that the first time but for some reason it didn't work so I thought I had to change something, now seems like I'm getting the right data, I'll keep testing it.
Thanks!
Thanks!
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Read data from 16-bit register addres (I2C)
Apologies - brain fade on my part. It is indeed standard I2C.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Read data from 16-bit register addres (I2C)
Happens to me all the time.