diff options
| author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-10-28 21:04:03 +0300 |
|---|---|---|
| committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2015-10-28 21:04:03 +0300 |
| commit | a2e0d92eeb7e07cbad06368ccce22cc5d360ae55 (patch) | |
| tree | 0ee507d01912e99382b4cdd55dc2f7721d17a732 /examples | |
| parent | f3b1a933fc148454e00188b46292f85d5fadafd5 (diff) | |
examples: Add example of I2C usage, taking PyBoard accelerometer as subject.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/accel_i2c.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/examples/accel_i2c.py b/examples/accel_i2c.py new file mode 100644 index 000000000..2c5f4a077 --- /dev/null +++ b/examples/accel_i2c.py @@ -0,0 +1,33 @@ +# This is an example on how to access accelerometer on +# PyBoard directly using I2C bus. As such, it's more +# intended to be an I2C example, rather than accelerometer +# example. For the latter, using pyb.Accel class is +# much easier. + +import pyb +import time + +# Accelerometer needs to be powered on first. Even +# though signal is called "AVDD", and there's separate +# "DVDD", without AVDD, it won't event talk on I2C bus. +accel_pwr = pyb.Pin("MMA_AVDD") +accel_pwr.value(1) + +i2c = pyb.I2C(1) +addrs = i2c.scan() +print("Scanning devices:", [hex(x) for x in addrs]) +if 0x4c not in addrs: + print("Accelerometer is not detected") + +ACCEL_ADDR = 0x4c +ACCEL_AXIS_X_REG = 0 +ACCEL_MODE_REG = 7 + +# Now activate measurements +i2c.mem_write(b"\x01", ACCEL_ADDR, ACCEL_MODE_REG) + +print("Try to move accelerometer and watch the values") +while True: + val = i2c.mem_read(1, ACCEL_ADDR, ACCEL_AXIS_X_REG) + print(val[0]) + time.sleep(1) |
