Code micro-python "micro-bit" pour gérer le PCA9685 :
import microbit
PCA9685_ADDR=0x43
PCA9685_FREQ=60
def PCA9685_init():
microbit.i2c.init()
microbit.i2c.write(PCA9685_ADDR,bytes([0x00,0x10]))
prescale=int(25000000.0/4096.0/PCA9685_FREQ+0.5)
microbit.i2c.write(PCA9685_ADDR,bytes([0xfe,prescale]))
microbit.i2c.write(PCA9685_ADDR,bytes([0x00,0xa0]))
def PCA9685(canal,taux):
adresse=0x06+4*canal
on_l=0x00
on_h=0x00
off_l=0x00
off_h=0x00
if taux==0: off_h=0x10
if taux==100: on_h=0x10
if taux>0 and taux<100:
arret=taux*40
off_l=arret&0x00ff
off_h=arret>>8
microbit.i2c.write(PCA9685_ADDR,bytes([adresse,on_l,on_h,off_l,off_h]))
def PCA9685_LED(r,g,b):
PCA9685(4,100-b)
PCA9685(5,100-g)
PCA9685(6,100-r)
def PCA9685_moteurs(sensA,puissA,sensB,puissB):
PCA9685(0,0) if sensA==0 else PCA9685(0,100)
PCA9685(2,0) if sensA==0 else PCA9685(2,100)
PCA9685(1,puissA)
PCA9685(3,puissB)
PCA9685_init()
PCA9685_LED(0,0,100)
PCA9685_moteurs(0,50,0,50)