This is a library for the PCA9685 ported from the Adafruit Ardiuno library.
PCA9685Lib.h@2:ec40a85eba51, 2015-02-14 (annotated)
- Committer:
- rvasquez6089
- Date:
- Sat Feb 14 19:35:08 2015 +0000
- Revision:
- 2:ec40a85eba51
- Parent:
- 1:f1b17f9d387e
Added Documentation and SetMode2 Register function
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rvasquez6089 | 2:ec40a85eba51 | 1 | |
rvasquez6089 | 2:ec40a85eba51 | 2 | |
rvasquez6089 | 2:ec40a85eba51 | 3 | |
rvasquez6089 | 2:ec40a85eba51 | 4 | |
rvasquez6089 | 2:ec40a85eba51 | 5 | #ifndef _ADAFRUIT_PWMServoDriver_H |
rvasquez6089 | 2:ec40a85eba51 | 6 | #define _ADAFRUIT_PWMServoDriver_H |
rvasquez6089 | 2:ec40a85eba51 | 7 | |
rvasquez6089 | 2:ec40a85eba51 | 8 | #include "mbed.h" |
rvasquez6089 | 2:ec40a85eba51 | 9 | #include <cmath> |
rvasquez6089 | 2:ec40a85eba51 | 10 | |
rvasquez6089 | 2:ec40a85eba51 | 11 | #define PCA9685_SUBADR1 0x2 |
rvasquez6089 | 2:ec40a85eba51 | 12 | #define PCA9685_SUBADR2 0x3 |
rvasquez6089 | 2:ec40a85eba51 | 13 | #define PCA9685_SUBADR3 0x4 |
rvasquez6089 | 2:ec40a85eba51 | 14 | |
rvasquez6089 | 2:ec40a85eba51 | 15 | #define PCA9685_MODE1 0x0 |
rvasquez6089 | 2:ec40a85eba51 | 16 | #define PCA9685_MODE2 0x1 |
rvasquez6089 | 2:ec40a85eba51 | 17 | #define PCA9685_PRESCALE 0xFE |
rvasquez6089 | 2:ec40a85eba51 | 18 | |
rvasquez6089 | 2:ec40a85eba51 | 19 | #define LED0_ON_L 0x6 |
rvasquez6089 | 2:ec40a85eba51 | 20 | #define LED0_ON_H 0x7 |
rvasquez6089 | 2:ec40a85eba51 | 21 | #define LED0_OFF_L 0x8 |
rvasquez6089 | 2:ec40a85eba51 | 22 | #define LED0_OFF_H 0x9 |
rvasquez6089 | 2:ec40a85eba51 | 23 | |
rvasquez6089 | 2:ec40a85eba51 | 24 | #define ALLLED_ON_L 0xFA |
rvasquez6089 | 2:ec40a85eba51 | 25 | #define ALLLED_ON_H 0xFB |
rvasquez6089 | 2:ec40a85eba51 | 26 | #define ALLLED_OFF_L 0xFC |
rvasquez6089 | 2:ec40a85eba51 | 27 | #define ALLLED_OFF_H 0xFD |
rvasquez6089 | 2:ec40a85eba51 | 28 | |
rvasquez6089 | 2:ec40a85eba51 | 29 | |
rvasquez6089 | 2:ec40a85eba51 | 30 | |
rvasquez6089 | 2:ec40a85eba51 | 31 | class PCA9685Lib; //Forward declaration |
rvasquez6089 | 2:ec40a85eba51 | 32 | |
rvasquez6089 | 2:ec40a85eba51 | 33 | /*! \struct LEDarr |
rvasquez6089 | 2:ec40a85eba51 | 34 | \brief LEDarr is a simple struct that stores the outpin it is, |
rvasquez6089 | 2:ec40a85eba51 | 35 | *a pointer to the class it is in, and an overloaded = operator |
rvasquez6089 | 2:ec40a85eba51 | 36 | * to make assigning duty cycle easier. |
rvasquez6089 | 2:ec40a85eba51 | 37 | A more detailed class description. |
rvasquez6089 | 2:ec40a85eba51 | 38 | */ |
rvasquez6089 | 2:ec40a85eba51 | 39 | struct LEDarr { |
rvasquez6089 | 2:ec40a85eba51 | 40 | |
rvasquez6089 | 2:ec40a85eba51 | 41 | uint8_t LEDnum; |
rvasquez6089 | 2:ec40a85eba51 | 42 | PCA9685Lib* PCALib; |
rvasquez6089 | 2:ec40a85eba51 | 43 | /** Overloaded assignement operator. |
rvasquez6089 | 2:ec40a85eba51 | 44 | * Allows the user to more simply assign duty cycle values to a pin. |
rvasquez6089 | 2:ec40a85eba51 | 45 | * @param duty unsigned 16 integer as the duty cycle |
rvasquez6089 | 2:ec40a85eba51 | 46 | */ |
rvasquez6089 | 2:ec40a85eba51 | 47 | void operator= (uint16_t duty); |
rvasquez6089 | 2:ec40a85eba51 | 48 | }; |
rvasquez6089 | 2:ec40a85eba51 | 49 | |
rvasquez6089 | 2:ec40a85eba51 | 50 | /** My PCA9685Lib class |
rvasquez6089 | 2:ec40a85eba51 | 51 | * used for controlling the pca9685 |
rvasquez6089 | 2:ec40a85eba51 | 52 | * |
rvasquez6089 | 2:ec40a85eba51 | 53 | * Example: |
rvasquez6089 | 2:ec40a85eba51 | 54 | * @code |
rvasquez6089 | 2:ec40a85eba51 | 55 | * // Change the duty cycle of an led |
rvasquez6089 | 2:ec40a85eba51 | 56 | * |
rvasquez6089 | 2:ec40a85eba51 | 57 | * #include "mbed.h" |
rvasquez6089 | 2:ec40a85eba51 | 58 | * #include "PCA9685Lib.h" |
rvasquez6089 | 2:ec40a85eba51 | 59 | * I2C i2c(p28, p27); |
rvasquez6089 | 2:ec40a85eba51 | 60 | * PCA9685Lib LedDriver(i2c); |
rvasquez6089 | 2:ec40a85eba51 | 61 | * LedDriver.begin(); |
rvasquez6089 | 2:ec40a85eba51 | 62 | * LedDriver.setPWMFreq(1600); |
rvasquez6089 | 2:ec40a85eba51 | 63 | * int main() |
rvasquez6089 | 2:ec40a85eba51 | 64 | * { |
rvasquez6089 | 2:ec40a85eba51 | 65 | * while(1) |
rvasquez6089 | 2:ec40a85eba51 | 66 | * { |
rvasquez6089 | 2:ec40a85eba51 | 67 | * |
rvasquez6089 | 2:ec40a85eba51 | 68 | * for(uint16_t i = 0; i < 16; i++) |
rvasquez6089 | 2:ec40a85eba51 | 69 | { |
rvasquez6089 | 2:ec40a85eba51 | 70 | for(uint16_t j = 0; j < 4096; j += 8) |
rvasquez6089 | 2:ec40a85eba51 | 71 | { |
rvasquez6089 | 2:ec40a85eba51 | 72 | LedDriver.LED[i] = j; |
rvasquez6089 | 2:ec40a85eba51 | 73 | } |
rvasquez6089 | 2:ec40a85eba51 | 74 | } |
rvasquez6089 | 2:ec40a85eba51 | 75 | for(uint16_t i = 0; i < 16; i++) |
rvasquez6089 | 2:ec40a85eba51 | 76 | { |
rvasquez6089 | 2:ec40a85eba51 | 77 | for(uint16_t j = 4095; j > 0; j -= 8) |
rvasquez6089 | 2:ec40a85eba51 | 78 | { |
rvasquez6089 | 2:ec40a85eba51 | 79 | LedDriver.LED[i] = j; |
rvasquez6089 | 2:ec40a85eba51 | 80 | } |
rvasquez6089 | 2:ec40a85eba51 | 81 | } |
rvasquez6089 | 2:ec40a85eba51 | 82 | * |
rvasquez6089 | 2:ec40a85eba51 | 83 | * } |
rvasquez6089 | 2:ec40a85eba51 | 84 | * } |
rvasquez6089 | 2:ec40a85eba51 | 85 | * @endcode |
rvasquez6089 | 2:ec40a85eba51 | 86 | */ |
rvasquez6089 | 2:ec40a85eba51 | 87 | |
rvasquez6089 | 2:ec40a85eba51 | 88 | |
rvasquez6089 | 2:ec40a85eba51 | 89 | class PCA9685Lib |
rvasquez6089 | 2:ec40a85eba51 | 90 | { |
rvasquez6089 | 2:ec40a85eba51 | 91 | public: |
rvasquez6089 | 2:ec40a85eba51 | 92 | /** Create an PCA9685Lib instance, pass in an I2C object and the PCA9685 device address |
rvasquez6089 | 2:ec40a85eba51 | 93 | * The default address is 0x80 |
rvasquez6089 | 2:ec40a85eba51 | 94 | * @param i2cobj Pass and instantiated I2C object to the class to communicate with |
rvasquez6089 | 2:ec40a85eba51 | 95 | * @param addr pass the device address the constructor (optional) |
rvasquez6089 | 2:ec40a85eba51 | 96 | * |
rvasquez6089 | 2:ec40a85eba51 | 97 | */ |
rvasquez6089 | 2:ec40a85eba51 | 98 | PCA9685Lib(I2C i2cobj, int addr = 0x80); //0b 1_000000_(R/W) <- default slave adress |
rvasquez6089 | 2:ec40a85eba51 | 99 | /** Seaches for I2C devices |
rvasquez6089 | 2:ec40a85eba51 | 100 | |
rvasquez6089 | 2:ec40a85eba51 | 101 | */ |
rvasquez6089 | 2:ec40a85eba51 | 102 | void i2c_probe(void); |
rvasquez6089 | 2:ec40a85eba51 | 103 | /** Resets the PCA9685 (clears device registers) |
rvasquez6089 | 2:ec40a85eba51 | 104 | */ |
rvasquez6089 | 2:ec40a85eba51 | 105 | void begin(void); |
rvasquez6089 | 2:ec40a85eba51 | 106 | /** Sets the I2C frequency. |
rvasquez6089 | 2:ec40a85eba51 | 107 | */ |
rvasquez6089 | 2:ec40a85eba51 | 108 | void setI2Cfreq(int freq); |
rvasquez6089 | 2:ec40a85eba51 | 109 | /** Resets the PCA9685 (clears device registers) |
rvasquez6089 | 2:ec40a85eba51 | 110 | */ |
rvasquez6089 | 2:ec40a85eba51 | 111 | void reset(void); |
rvasquez6089 | 2:ec40a85eba51 | 112 | /** sets the pwm frequency |
rvasquez6089 | 2:ec40a85eba51 | 113 | */ |
rvasquez6089 | 2:ec40a85eba51 | 114 | void setPWMFreq(float freq); |
rvasquez6089 | 2:ec40a85eba51 | 115 | /** Set the prescale registers on the pca9685 |
rvasquez6089 | 2:ec40a85eba51 | 116 | */ |
rvasquez6089 | 2:ec40a85eba51 | 117 | void setPrescale(uint8_t prescale); |
rvasquez6089 | 2:ec40a85eba51 | 118 | /** Sets the Mode2 to 0x15 (totem pole configuration) by default |
rvasquez6089 | 2:ec40a85eba51 | 119 | @param val Mode2 register value |
rvasquez6089 | 2:ec40a85eba51 | 120 | */ |
rvasquez6089 | 2:ec40a85eba51 | 121 | void setMode2(uint8_t val = 0x15); |
rvasquez6089 | 2:ec40a85eba51 | 122 | /** Sets the pwm on a specific pin |
rvasquez6089 | 2:ec40a85eba51 | 123 | * Each PWM clock cycle is divided in 4096 steps |
rvasquez6089 | 2:ec40a85eba51 | 124 | * See datasheet for more details |
rvasquez6089 | 2:ec40a85eba51 | 125 | * @param num Output pin number (0-15) |
rvasquez6089 | 2:ec40a85eba51 | 126 | * @param on This is speicify at what point in the cycle the output will turn on (0-4095 or 0x000-0xFFF) |
rvasquez6089 | 2:ec40a85eba51 | 127 | * @param off This is speicify at what point in the cycle the output will turn off (0-4095 or 0x000-0xFFF) |
rvasquez6089 | 2:ec40a85eba51 | 128 | */ |
rvasquez6089 | 2:ec40a85eba51 | 129 | void setPWM(uint8_t num, uint16_t on, uint16_t off); |
rvasquez6089 | 2:ec40a85eba51 | 130 | /** Sets the duty cycle on a specific pin |
rvasquez6089 | 2:ec40a85eba51 | 131 | * @param num Output pin number (0-15) |
rvasquez6089 | 2:ec40a85eba51 | 132 | * @param duty This is speicify at what point in the cycle the output will turn off (0-4095 or 0x000-0xFFF) |
rvasquez6089 | 2:ec40a85eba51 | 133 | */ |
rvasquez6089 | 2:ec40a85eba51 | 134 | void setDuty(uint8_t num, uint16_t duty); |
rvasquez6089 | 2:ec40a85eba51 | 135 | |
rvasquez6089 | 2:ec40a85eba51 | 136 | |
rvasquez6089 | 2:ec40a85eba51 | 137 | LEDarr LED[15]; |
rvasquez6089 | 2:ec40a85eba51 | 138 | private: |
rvasquez6089 | 2:ec40a85eba51 | 139 | int _i2caddr; |
rvasquez6089 | 2:ec40a85eba51 | 140 | I2C i2c; |
rvasquez6089 | 2:ec40a85eba51 | 141 | uint8_t read8(char addr); |
rvasquez6089 | 2:ec40a85eba51 | 142 | void write8(char addr, char d); |
rvasquez6089 | 2:ec40a85eba51 | 143 | }; |
rvasquez6089 | 2:ec40a85eba51 | 144 | |
rvasquez6089 | 2:ec40a85eba51 | 145 | |
rvasquez6089 | 2:ec40a85eba51 | 146 | #endif |
rvasquez6089 | 2:ec40a85eba51 | 147 | /* |
rvasquez6089 | 0:1ecf26e0cf3c | 148 | This is a library for our Adafruit 16-channel PWM & Servo driver |
rvasquez6089 | 0:1ecf26e0cf3c | 149 | |
rvasquez6089 | 0:1ecf26e0cf3c | 150 | Pick one up today in the adafruit shop! |
rvasquez6089 | 0:1ecf26e0cf3c | 151 | ------> http://www.adafruit.com/products/815 |
rvasquez6089 | 0:1ecf26e0cf3c | 152 | |
rvasquez6089 | 0:1ecf26e0cf3c | 153 | These displays use I2C to communicate, 2 pins are required to |
rvasquez6089 | 0:1ecf26e0cf3c | 154 | interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4 |
rvasquez6089 | 0:1ecf26e0cf3c | 155 | |
rvasquez6089 | 0:1ecf26e0cf3c | 156 | Adafruit invests time and resources providing this open source code, |
rvasquez6089 | 0:1ecf26e0cf3c | 157 | please support Adafruit and open-source hardware by purchasing |
rvasquez6089 | 0:1ecf26e0cf3c | 158 | products from Adafruit! |
rvasquez6089 | 0:1ecf26e0cf3c | 159 | |
rvasquez6089 | 0:1ecf26e0cf3c | 160 | Written by Limor Fried/Ladyada for Adafruit Industries. |
rvasquez6089 | 0:1ecf26e0cf3c | 161 | BSD license, all text above must be included in any redistribution |
rvasquez6089 | 0:1ecf26e0cf3c | 162 | ****************************************************/ |
rvasquez6089 | 0:1ecf26e0cf3c | 163 | |
rvasquez6089 | 0:1ecf26e0cf3c | 164 | /***************************** |
rvasquez6089 | 0:1ecf26e0cf3c | 165 | This program was ported from https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library. |
rvasquez6089 | 0:1ecf26e0cf3c | 166 | |
rvasquez6089 | 1:f1b17f9d387e | 167 | */ |