树莓派:tca9548a(多路I2C)简易驱动

一、电路连接

PCA9685和tca9548的连接

PCA9685 16路舵机驱动板
tca9548a I2C扩展板
SDA------------------------------> SD1
SCL------------------------------> SC1

树莓派和tca9548的连接

树莓派 tca9548a I2C扩展板
SDA1---------------------------> SDA
SCL1---------------------------> SCL

 

二、完整代码

from Adafruit_GPIO import I2C
import Adafruit_PCA9685
import time

# tca9548多路I2C驱动
# tca_select:选择一个通道
# tca_set(mask):选择多个通道

tca = I2C.get_i2c_device(address=0x70)

def tca_select(channel):
    """Select an individual channel."""
    if channel > 7:
        return
    tca.writeRaw8(1 << channel)

def tca_set(mask):
    """Select one or more channels.
           chan =   76543210
           mask = 0b00000000
    """
    if mask > 0xff:
        return
    tca.writeRaw8(mask)
########################################################
########################################################

# 选择通道1:我接上PCA9685 16路舵机驱动板
tca_select(1)

# 剩下的按正常使用就行了
pwm = Adafruit_PCA9685.PCA9685()

def set_servo_angle(channel, angle):              #输入角度转换成12^精度的数值
    date=4096*((angle*11)+500)/20000              #进行四舍五入运算 date=int(4096*((angle*11)+500)/(20000)+0.5)
    pwm.set_pwm(channel, 0,int(date))

pwm.set_pwm_freq(50)

print('Moving servo on channel x, press Ctrl-C to quit...')
while True:
    # Move servo on channel O between extremes.
    channel=int(input('pleas input channel:'))
    angle=int(input('pleas input angle:'))
    set_servo_angle(channel, angle)




 

三、现场图片

你可能感兴趣的:(树莓派)