Package: haas-python-pca9685
HaaS Python介绍HaaS Python是阿里云IoT团队最新研发的一套低代码编程框架，兼容MicroPython编程规范，依托HaaS平台软硬件积木提供AI、支付、蓝牙配网、云连接、UI等物联网场景常用的能力，从而解决了物联网应用开发难的问题。有了Python轻应用框架，物联网编程不再局限于专业软件开发人员，一般的技术员也可以快速实现复杂的物联网需求。
更多HaaS Python介绍和开发资料见HaaS Python官网,创意案例,硬件积木HaaS Python is a set of low-code programming frameworks newly developed by Alibaba Cloud IoT team. It is compatible with MicroPython programming specifications. It relies on the hardware and software building blocks of the HaaS platform to provide capabilities commonly used in IoT scenarios such as AI, payment, Bluetooth network configuration, cloud connection, and UI. Solve the difficult problem of IoT application development. With the Python light application framework, IoT programming is no longer limited to professional software developers, and general technicians can quickly implement complex IoT requirements.
For more HaaS Python introduction and development materials, seeHaaS Python official website,Creative Case,Hardware Building Blocks安装方法在HaaS Python官网下载开发板对应的固件，并完成固件烧录Download the firmware corresponding to the development board atHaaS Python websiteand complete the firmware burning在开发板上运行以下命令完成安装和使用,如果您在使用过程中遇到问题，欢迎在github上向我们提交issue，我们的工程师会及时解答Run the following commands on the development board to complete the installation and use. If you encounter problems during use, please submit an issue to us ongithub, Our engineers will answer in timeimport upip
upip.install("haas-python-pca9685")
import haas-python-pca968516路PWM模块 - PCA9685一、产品简介PCA9685芯片，是16通道12bit PWM舵机驱动，主控板通过I2C控制芯片，进而可以驱动16个舵机，这样可以解决需要很多电机控制的项目，会大量占用主控板的引脚，也会影响主控板的处理能力。引脚定义GND：接地OE：GPIO（低电平有效，板载已经将OE拉低，使用时可以不接）SCL：I2C时钟SDA：I2C数字VCC：5VV+：NC二、技术参数供电电压：5V接口：I2C接口，最大支持1MHz速率地址：6地址位I2C总线上可挂载62个设备PWM输出支持可调频，1.6KHz分辨率：12位IC待机电流：20mA模块尺寸：25mm * 61mm * 10mm三、软件接口PC9685 PWM舵机控制器HaaS Python驱动：下载地址PCA9685(i2cObj) - 创建PCA9685驱动对象函数原型：pca9685Obj = PCA9685(i2cObj)参数说明：参数类型必选参数？说明i2cObjI2C是调用此函数前需确保i2cObj对象已经处于open状态返回值：
PCA9685对象创建成功，返回PCA9685对象；PCA9685对象创建失败，抛出Exception示例代码：fromdriverimportI2Cimportpca9685i2cObj=I2C()i2cObj.open("pca9685")robot=pca9685.PCA9685(i2cObj)print('pca5860 inited')输出：pca5860 inited!setServo(index,pos) - 设置各路Servo电机的位置函数功能：
设置各路Servo电机的位置函数原型：PCA9685.setServo(index,pos)参数说明：返回值类型说明index整型index：0-15，分别代表第index+1路PWMpos浮点-pi/2~pi/2单位：度pi约等于3.141返回值：
无示例：frommathimportpifromdriverimportI2Cimportpca9685importutimei2cObj=I2C()i2cObj.open("pca9685")robot=pca9685.PCA9685(i2cObj)print('pca5860 inited')robot.setServo(0,-pi/4)utime.sleep(1)robot.setServo(0,pi/4)utime.sleep(1)robot.setServo(1,-pi/4)utime.sleep(1)robot.setServo(1,pi/4)utime.sleep(1)robot.setServo(2,-pi/4)utime.sleep(1)robot.setServo(2,pi/4)utime.sleep(1)robot.setServo(3,-pi/4)utime.sleep(1)robot.setServo(3,pi/4)utime.sleep(1)输出：pca5860 initedreset(index) - 复位各路PWM函数功能：
复位各路PWM函数原型：PCA9685.reset(index)参数说明：返回值类型必选参数？说明index整型是index：0-15，分别代表第index+1路PWM返回值：
无示例：frommathimportpifromdriverimportI2Cimportpca9685importutimei2cObj=I2C()i2cObj.open("pca9685")robot=pca9685.PCA9685(i2cObj)print('pca5860 inited')robot.setServo(0,-pi/4)utime.sleep(1)robot.setServo(0,pi/4)utime.sleep(1)robot.reset(0)输出：pca5860 inited四、接口案例此使用实例在board.json中定义了名为pca9685的I2C类型的对象。在Python脚本中周期性的设置第1路的舵机从0度到负90度，然后从负90度回到0度位置并打印在日志中。代码：board.json配置{"name":"board-name","version":"1.0.0","io":{"pca9685":{"type":"I2C","port":0,"addrWidth":7,"freq":1000000,"mode":"master","devAddr":64}},"debugLevel":"ERROR","repl":"disable"}示例：frommathimportpifromdriverimportI2Cimportpca9685importutimei2cObj=I2C()i2cObj.open("pca9685")robot=pca9685.PCA9685(i2cObj)print('pca5860 inited')whileTrue:robot.setServo(0,0)print(0)utime.sleep(1)robot.setServo(0,-pi/2)print(-pi/2)utime.sleep(1)输出：pca5860 inited
0
-pi/2
0
-pi/2
0
-pi/2
0
-pi/2五、工作原理PCA9685传感器寄存器描述如下表所示：序号寄存器地址功能说明10x00MODE120x01MODE230x02SUBADR140x03SUBADR250x04SUBADR360x05ALLCALLADR70x06LED0_ON_L80x07LED0_ON_H90x08LED0_OFF_L100x09LED0_OFF_H11......120x06+4*XLEDX_ON_L130x06+4*X+1LED0_ON_H140x06+4*X+2LED0_OFF_L150x06+4*X+3LED0_OFF_H160xFAALL_LED_ON_L170xFBALL_LED_ON_H180xFCALL_LED_OFF_L190xFDALL_LED_OFF_H190xFEPRE_SCALE190xFFTestMode通道的四个寄存器在上述的寄存器总览表中可看出：16个通道中，每个都有LEDX_ON_L、LEDX_ON_H、LEDX_OFF_L、LEDX_OFF_H 四个寄存器，芯片中12位的计数器CNT，会根据PRE_SCALE设置的值进行计数。当LEDX_ON_H[3:0]:LEDX_ON_L < CNT < LEDX_OFF_H[3:0]:LEDX_OFF_L时，输出高电平；当LEDX_OFF_H[3:0]:LEDX_OFF_L < CNT < 4096时，输出低电平。如下图所示，假设LEDX_ON=410,LEDX_OFF=1300,那么在计数器从0计数到409，开始输出高电平，高电平持续LEDX_OFF-LEDX_ON，然后开始输出低电平，到一个PWM周期结束，后面周而复始循环动作。PRE_SCALE寄存器prescale = round(osc_clock/4096∗update_rate) - 1osc_clock为选用的时钟频率，如使用内部25MHz时钟，即为25000000Hz。update_rate为PWM的频率，比如舵机PWM周期为20ms，则update_rate=50Hz。4096是因为计数器是12位。prescale为计数器加一所需要的时钟数舵机控制舵机的PWM信号控制周期为20ms（50Hz的频率），想要控制角度只需控制一个周期中高电平持续的时间。以180°舵机为例，对应关系如下：一个周期中高电平持续时间舵机保持的角度0.5ms0度1ms45度1.5ms90度2ms135度2.5ms180度参考文献及购买链接[1]PCA9685模块使用说明[2]PCA9685模块购买