Adafruit_PWMServoDriver.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*!
  2. * @file Adafruit_PWMServoDriver.h
  3. *
  4. * This is a library for our Adafruit 16-channel PWM & Servo driver.
  5. *
  6. * Designed specifically to work with the Adafruit 16-channel PWM & Servo
  7. * driver.
  8. *
  9. * Pick one up today in the adafruit shop!
  10. * ------> https://www.adafruit.com/product/815
  11. *
  12. * These driver use I2C to communicate, 2 pins are required to interface.
  13. * For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4.
  14. *
  15. * Adafruit invests time and resources providing this open source code,
  16. * please support Adafruit andopen-source hardware by purchasing products
  17. * from Adafruit!
  18. *
  19. * Limor Fried/Ladyada (Adafruit Industries).
  20. *
  21. * BSD license, all text above must be included in any redistribution
  22. */
  23. #ifndef _ADAFRUIT_PWMServoDriver_H
  24. #define _ADAFRUIT_PWMServoDriver_H
  25. #include <Arduino.h>
  26. #include <Wire.h>
  27. // REGISTER ADDRESSES
  28. #define PCA9685_MODE1 0x00 /**< Mode Register 1 */
  29. #define PCA9685_MODE2 0x01 /**< Mode Register 2 */
  30. #define PCA9685_SUBADR1 0x02 /**< I2C-bus subaddress 1 */
  31. #define PCA9685_SUBADR2 0x03 /**< I2C-bus subaddress 2 */
  32. #define PCA9685_SUBADR3 0x04 /**< I2C-bus subaddress 3 */
  33. #define PCA9685_ALLCALLADR 0x05 /**< LED All Call I2C-bus address */
  34. #define PCA9685_LED0_ON_L 0x06 /**< LED0 on tick, low byte*/
  35. #define PCA9685_LED0_ON_H 0x07 /**< LED0 on tick, high byte*/
  36. #define PCA9685_LED0_OFF_L 0x08 /**< LED0 off tick, low byte */
  37. #define PCA9685_LED0_OFF_H 0x09 /**< LED0 off tick, high byte */
  38. // etc all 16: LED15_OFF_H 0x45
  39. #define PCA9685_ALLLED_ON_L 0xFA /**< load all the LEDn_ON registers, low */
  40. #define PCA9685_ALLLED_ON_H 0xFB /**< load all the LEDn_ON registers, high */
  41. #define PCA9685_ALLLED_OFF_L 0xFC /**< load all the LEDn_OFF registers, low */
  42. #define PCA9685_ALLLED_OFF_H 0xFD /**< load all the LEDn_OFF registers,high */
  43. #define PCA9685_PRESCALE 0xFE /**< Prescaler for PWM output frequency */
  44. #define PCA9685_TESTMODE 0xFF /**< defines the test mode to be entered */
  45. // MODE1 bits
  46. #define MODE1_ALLCAL 0x01 /**< respond to LED All Call I2C-bus address */
  47. #define MODE1_SUB3 0x02 /**< respond to I2C-bus subaddress 3 */
  48. #define MODE1_SUB2 0x04 /**< respond to I2C-bus subaddress 2 */
  49. #define MODE1_SUB1 0x08 /**< respond to I2C-bus subaddress 1 */
  50. #define MODE1_SLEEP 0x10 /**< Low power mode. Oscillator off */
  51. #define MODE1_AI 0x20 /**< Auto-Increment enabled */
  52. #define MODE1_EXTCLK 0x40 /**< Use EXTCLK pin clock */
  53. #define MODE1_RESTART 0x80 /**< Restart enabled */
  54. // MODE2 bits
  55. #define MODE2_OUTNE_0 0x01 /**< Active LOW output enable input */
  56. #define MODE2_OUTNE_1 \
  57. 0x02 /**< Active LOW output enable input - high impedience */
  58. #define MODE2_OUTDRV 0x04 /**< totem pole structure vs open-drain */
  59. #define MODE2_OCH 0x08 /**< Outputs change on ACK vs STOP */
  60. #define MODE2_INVRT 0x10 /**< Output logic state inverted */
  61. #define PCA9685_I2C_ADDRESS 0x40 /**< Default PCA9685 I2C Slave Address */
  62. #define FREQUENCY_OSCILLATOR 25000000 /**< Int. osc. frequency in datasheet */
  63. #define PCA9685_PRESCALE_MIN 3 /**< minimum prescale value */
  64. #define PCA9685_PRESCALE_MAX 255 /**< maximum prescale value */
  65. /*!
  66. * @brief Class that stores state and functions for interacting with PCA9685
  67. * PWM chip
  68. */
  69. class Adafruit_PWMServoDriver {
  70. public:
  71. Adafruit_PWMServoDriver();
  72. Adafruit_PWMServoDriver(const uint8_t addr);
  73. Adafruit_PWMServoDriver(const uint8_t addr, TwoWire &i2c);
  74. void begin(uint8_t prescale = 0);
  75. void reset();
  76. void sleep();
  77. void wakeup();
  78. void setExtClk(uint8_t prescale);
  79. void setPWMFreq(float freq);
  80. void setOutputMode(bool totempole);
  81. uint8_t getPWM(uint8_t num);
  82. void setPWM(uint8_t num, uint16_t on, uint16_t off);
  83. void setPin(uint8_t num, uint16_t val, bool invert = false);
  84. uint8_t readPrescale(void);
  85. void writeMicroseconds(uint8_t num, uint16_t Microseconds);
  86. void setOscillatorFrequency(uint32_t freq);
  87. uint32_t getOscillatorFrequency(void);
  88. private:
  89. uint8_t _i2caddr;
  90. TwoWire *_i2c;
  91. uint32_t _oscillator_freq;
  92. uint8_t read8(uint8_t addr);
  93. void write8(uint8_t addr, uint8_t d);
  94. };
  95. #endif