40pin IO开发
实验06——IIC 实验
软件运行:(RDK X5 在 40PIN 上默认使能 I2C5(物理管脚号 3 和 5)和 I2C0(物理管脚号 27 和 28),IO电压3.3V。)
进入用户主目录,运行
cd usersudo python3 ./test_i2c.py
终端打印信息如下:

#!/usr/bin/env python3
import sys
import signal
import os
import time
# 导入i2cdev
from i2cdev import I2C
def signal_handler(signal, frame):
sys.exit(0)
def i2cdevTest():
# device, bus = 0x51, 0
bus = input("Please input I2C BUS num:")
os.system('i2cdetect -y -r ' + bus)
device = input("Please input I2C device num(Hex):")
print("Read data from device %s on I2C bus %s" % (device, bus))
i2c = I2C(eval("0x" + device), int(bus))
value = i2c.read(1)
i2c.write(value)
print("read value=", value)
i2c.close()
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal_handler)
print("Starting demo now! Press CTRL+C to exit")
print("List of enabled I2C controllers:")
os.system('ls /dev/i2c*')
while True:
i2cdevTest()