Adafruit PCA9685 PWM Library
Adafruit_PWMServoDriver.h
Go to the documentation of this file.
1 
23 #ifndef _ADAFRUIT_PWMServoDriver_H
24 #define _ADAFRUIT_PWMServoDriver_H
25 
26 #include <Adafruit_I2CDevice.h>
27 #include <Arduino.h>
28 
29 // REGISTER ADDRESSES
30 #define PCA9685_MODE1 0x00
31 #define PCA9685_MODE2 0x01
32 #define PCA9685_SUBADR1 0x02
33 #define PCA9685_SUBADR2 0x03
34 #define PCA9685_SUBADR3 0x04
35 #define PCA9685_ALLCALLADR 0x05
36 #define PCA9685_LED0_ON_L 0x06
37 #define PCA9685_LED0_ON_H 0x07
38 #define PCA9685_LED0_OFF_L 0x08
39 #define PCA9685_LED0_OFF_H 0x09
40 // etc all 16: LED15_OFF_H 0x45
41 #define PCA9685_ALLLED_ON_L 0xFA
42 #define PCA9685_ALLLED_ON_H 0xFB
43 #define PCA9685_ALLLED_OFF_L 0xFC
44 #define PCA9685_ALLLED_OFF_H 0xFD
45 #define PCA9685_PRESCALE 0xFE
46 #define PCA9685_TESTMODE 0xFF
48 // MODE1 bits
49 #define MODE1_ALLCAL 0x01
50 #define MODE1_SUB3 0x02
51 #define MODE1_SUB2 0x04
52 #define MODE1_SUB1 0x08
53 #define MODE1_SLEEP 0x10
54 #define MODE1_AI 0x20
55 #define MODE1_EXTCLK 0x40
56 #define MODE1_RESTART 0x80
57 // MODE2 bits
58 #define MODE2_OUTNE_0 0x01
59 #define MODE2_OUTNE_1 \
60  0x02
61 #define MODE2_OUTDRV 0x04
62 #define MODE2_OCH 0x08
63 #define MODE2_INVRT 0x10
65 #define PCA9685_I2C_ADDRESS 0x40
66 #define FREQUENCY_OSCILLATOR 25000000
68 #define PCA9685_PRESCALE_MIN 3
69 #define PCA9685_PRESCALE_MAX 255
75 class Adafruit_PWMServoDriver {
76 public:
78  Adafruit_PWMServoDriver(const uint8_t addr);
79  Adafruit_PWMServoDriver(const uint8_t addr, TwoWire &i2c);
80  bool begin(uint8_t prescale = 0);
81  void reset();
82  void sleep();
83  void wakeup();
84  void setExtClk(uint8_t prescale);
85  void setPWMFreq(float freq);
86  void setOutputMode(bool totempole);
87  uint16_t getPWM(uint8_t num, bool off = false);
88  uint8_t setPWM(uint8_t num, uint16_t on, uint16_t off);
89  void setPin(uint8_t num, uint16_t val, bool invert = false);
90  uint8_t readPrescale(void);
91  void writeMicroseconds(uint8_t num, uint16_t Microseconds);
92 
93  void setOscillatorFrequency(uint32_t freq);
94  uint32_t getOscillatorFrequency(void);
95 
96 private:
97  uint8_t _i2caddr;
98  TwoWire *_i2c;
99  Adafruit_I2CDevice *i2c_dev = NULL;
100 
101  uint32_t _oscillator_freq;
102  uint8_t read8(uint8_t addr);
103  void write8(uint8_t addr, uint8_t d);
104 };
105 
106 #endif
Class that stores state and functions for interacting with PCA9685 PWM chip.
Definition: Adafruit_PWMServoDriver.h:75