找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7889|回复: 1
收起左侧

PCA9685 PWM舵机驱动板16路模块 IIC接口含源码与资料

[复制链接]
ID:292497 发表于 2018-3-16 09:52 | 显示全部楼层 |阅读模式
16路模块 PWM舵机驱动板 控制器 机器人 IIC接口 使用说明书
0.jpg

AdafruitPWMServoDriverLibrary单片机源程序如下:
  1. /***************************************************
  2.   This is a library for our Adafruit 16-channel PWM & Servo driver

  3.   Pick one up today in the adafruit shop!

  4.   These displays use I2C to communicate, 2 pins are required to  
  5.   interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4

  6.   Adafruit invests time and resources providing this open source code,
  7.   please support Adafruit and open-source hardware by purchasing
  8.   products from Adafruit!

  9.   Written by Limor Fried/Ladyada for Adafruit Industries.  
  10.   BSD license, all text above must be included in any redistribution
  11. ****************************************************/

  12. #include <Adafruit_PWMServoDriver.h>
  13. #include <Wire.h>
  14. #if defined(__AVR__)
  15. #define WIRE Wire
  16. #elif defined(CORE_TEENSY) // Teensy boards
  17. #define WIRE Wire
  18. #else // Arduino Due
  19. #define WIRE Wire1
  20. #endif

  21. // Set to true to print some debug messages, or false to disable them.
  22. #define ENABLE_DEBUG_OUTPUT true

  23. Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr) {
  24.   _i2caddr = addr;
  25. }

  26. void Adafruit_PWMServoDriver::begin(void) {
  27. WIRE.begin();
  28. reset();
  29. }


  30. void Adafruit_PWMServoDriver::reset(void) {
  31. write8(PCA9685_MODE1, 0x0);
  32. }

  33. void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
  34.   //Serial.print("Attempting to set freq ");
  35.   //Serial.println(freq);
  36.   freq *= 0.9;  // Correct for overshoot in the frequency setting (see issue #11).
  37.   float prescaleval = 25000000;
  38.   prescaleval /= 4096;
  39.   prescaleval /= freq;
  40.   prescaleval -= 1;
  41.   if (ENABLE_DEBUG_OUTPUT) {
  42.     Serial.print("Estimated pre-scale: "); Serial.println(prescaleval);
  43.   }
  44.   uint8_t prescale = floor(prescaleval + 0.5);
  45.   if (ENABLE_DEBUG_OUTPUT) {
  46.     Serial.print("Final pre-scale: "); Serial.println(prescale);
  47.   }
  48.   
  49.   uint8_t oldmode = read8(PCA9685_MODE1);
  50.   uint8_t newmode = (oldmode&0x7F) | 0x10; // sleep
  51.   write8(PCA9685_MODE1, newmode); // go to sleep
  52.   write8(PCA9685_PRESCALE, prescale); // set the prescaler
  53.   write8(PCA9685_MODE1, oldmode);
  54.   delay(5);
  55.   write8(PCA9685_MODE1, oldmode | 0xa1);  //  This sets the MODE1 register to turn on auto increment.
  56.                                           // This is why the beginTransmission below was not working.
  57.   //  Serial.print("Mode now 0x"); Serial.println(read8(PCA9685_MODE1), HEX);
  58. }

  59. void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
  60.   //Serial.print("Setting PWM "); Serial.print(num); Serial.print(": "); Serial.print(on); Serial.print("->"); Serial.println(off);

  61.   WIRE.beginTransmission(_i2caddr);
  62.   WIRE.write(LED0_ON_L+4*num);
  63.   WIRE.write(on);
  64.   WIRE.write(on>>8);
  65.   WIRE.write(off);
  66.   WIRE.write(off>>8);
  67.   WIRE.endTransmission();
  68. }

  69. // Sets pin without having to deal with on/off tick placement and properly handles
  70. // a zero value as completely off.  Optional invert parameter supports inverting
  71. // the pulse for sinking to ground.  Val should be a value from 0 to 4095 inclusive.
  72. void Adafruit_PWMServoDriver::setPin(uint8_t num, uint16_t val, bool invert)
  73. {
  74.   // Clamp value between 0 and 4095 inclusive.
  75.   val = min(val, 4095);
  76.   if (invert) {
  77.     if (val == 0) {
  78.       // Special value for signal fully on.
  79.       setPWM(num, 4096, 0);
  80.     }
  81.     else if (val == 4095) {
  82.       // Special value for signal fully off.
  83.       setPWM(num, 0, 4096);
  84.     }
  85.     else {
  86.       setPWM(num, 0, 4095-val);
  87.     }
  88.   }
  89.   else {
  90.     if (val == 4095) {
  91.       // Special value for signal fully on.
  92.       setPWM(num, 4096, 0);
  93.     }
  94.     else if (val == 0) {
  95.       // Special value for signal fully off.
  96.       setPWM(num, 0, 4096);
  97.     }
  98.     else {
  99.       setPWM(num, 0, val);
  100.     }
  101.   }
  102. }

  103. uint8_t Adafruit_PWMServoDriver::read8(uint8_t addr) {
  104.   WIRE.beginTransmission(_i2caddr);
  105.   WIRE.write(addr);
  106.   WIRE.endTransmission();

  107.   WIRE.requestFrom((uint8_t)_i2caddr, (uint8_t)1);
  108.   return WIRE.read();
  109. }

  110. void Adafruit_PWMServoDriver::write8(uint8_t addr, uint8_t d) {
  111.   WIRE.beginTransmission(_i2caddr);
  112.   WIRE.write(addr);
  113.   WIRE.write(d);
  114.   WIRE.endTransmission();
  115. }
复制代码

所有资料51hei提供下载:
16路模块 PWM舵机驱动板 控制器 机器人 IIC接口 .zip (1.82 MB, 下载次数: 73)
回复

使用道具 举报

ID:516946 发表于 2020-9-2 10:03 | 显示全部楼层
你好我想问下你这个程序实际上可以使用吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表