Pyboard I2C different answers

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
ulne
Posts: 5
Joined: Tue Jan 30, 2018 7:52 am
Location: Heidelberg

Pyboard I2C different answers

Post by ulne » Fri Jun 29, 2018 2:20 pm

Hallo
I use
pyboard V1.1
MicroPython v1.9.4-140-g8fb95d65 on 2018-06-12; PYBv1.1 with STM32F405RG
ADS1100 16 Bit ADC with Adress 73

>>> import pyb
>>> import machine
>>> from machine import I2C
>>> i2c = machine.I2C(-1, machine.Pin('X9'), machine.Pin('X10'))
...
>>> result = i2c.readfrom(73, 3)
>>> print(result)
b'?\xd6\x18'

>>> result = i2c.readfrom_mem(73,0, 3)
>>> print(result)
b'?\xd6\x80'
>>> result = i2c.readfrom(73, 3)
>>> print(result)
b'\x03\xfd\x80'

>>>
why are the answers different?

User avatar
Roberthh
Posts: 1009
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Pyboard I2C different answers

Post by Roberthh » Fri Jun 29, 2018 2:51 pm

See the datasheet on the content of the read:
You can read the output register and the contents of the
configuration register from the ADS1100. To do this, address
the ADS1100 for reading, and read three bytes from the
device. The first two bytes are the output register’s contents;
the third byte is the configuration register’s contents.
The first two bytes contain the data, the last byte is the config/status register, with bit 7 being the busy bit. At the first read the busy bit is 0, meaning that the data is valid, the next two reads have the busy bit = 1, meaning that the data is not valid.
In this example the device is set to single mode/16 bit. This means, that the next conversion has to be started manually.
The only thing that puzzles me is, that according to the data sheet the device is in continous mode by default, and your sample log does not show any writes to the device, setting it to single mode.

ulne
Posts: 5
Joined: Tue Jan 30, 2018 7:52 am
Location: Heidelberg

Re: Pyboard I2C different answers

Post by ulne » Wed Jul 04, 2018 2:31 pm

Thank you very mach for help

This works
import time
import array
import machine
from machine import I2C

# INIT I2C
i2c = machine.I2C(-1, machine.Pin('X9'), machine.Pin('X10'))

def adc_write(addr, buf):
i2c.writeto(addr, buf) # write: Address, Data
return

def adc_read(addr): # must be 72 to 79
result = i2c.readfrom(addr, 3) # read 3 Byte from Address, 2 Bytes Data
print(result)

while True:

# ADC's write
adc_write(73, b'\x8c')

# ADC's read
result = i2c.readfrom(73, 3) # read 3 Byte from Address, 2 Bytes Data + Reg
print(result)

# sleep
time.sleep(1)

I have taste with I2C Analyser

Thank you

Post Reply