This is a library for the PCA9685 ported from the Adafruit Ardiuno library.
Revision 2:ec40a85eba51, committed 2015-02-14
- Comitter:
- rvasquez6089
- Date:
- Sat Feb 14 19:35:08 2015 +0000
- Parent:
- 1:f1b17f9d387e
- Commit message:
- Added Documentation and SetMode2 Register function
Changed in this revision
PCA9685Lib.cpp | Show annotated file Show diff for this revision Revisions of this file |
PCA9685Lib.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/PCA9685Lib.cpp Fri Feb 13 23:26:00 2015 +0000 +++ b/PCA9685Lib.cpp Sat Feb 14 19:35:08 2015 +0000 @@ -1,25 +1,4 @@ -/* - This is a library for our Adafruit 16-channel PWM & Servo driver - Pick one up today in the adafruit shop! - ------> http://www.adafruit.com/products/815 - - These displays use I2C to communicate, 2 pins are required to - interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4 - - Adafruit invests time and resources providing this open source code, - please support Adafruit and open-source hardware by purchasing - products from Adafruit! - - Written by Limor Fried/Ladyada for Adafruit Industries. - BSD license, all text above must be included in any redistribution - ****************************************************/ - - /***************************** - This program was ported from https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library. - I also added some functions. - Shundo Kishi - */ #include "PCA9685Lib.h" @@ -87,6 +66,11 @@ setPrescale(prescale); } +void PCA9685Lib::setMode2(uint8_t val) +{ + write8(PCA9685_MODE2, val); +} + void PCA9685Lib::setPWM(uint8_t num, uint16_t on, uint16_t off) { // hmm doesnt work, whyso? ( Not in AI mode. See line 54 above. ( Works now!! :D ) @@ -126,3 +110,24 @@ } +/* + This is a library for our Adafruit 16-channel PWM & Servo driver + + Pick one up today in the adafruit shop! + ------> http://www.adafruit.com/products/815 + + These displays use I2C to communicate, 2 pins are required to + interface. For Arduino UNOs, thats SCL -> Analog 5, SDA -> Analog 4 + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Limor Fried/Ladyada for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ****************************************************/ + + /***************************** + This program was ported from https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library. + + */ \ No newline at end of file
--- a/PCA9685Lib.h Fri Feb 13 23:26:00 2015 +0000 +++ b/PCA9685Lib.h Sat Feb 14 19:35:08 2015 +0000 @@ -1,4 +1,150 @@ -/*************************************************** + + + + +#ifndef _ADAFRUIT_PWMServoDriver_H +#define _ADAFRUIT_PWMServoDriver_H + +#include "mbed.h" +#include <cmath> + +#define PCA9685_SUBADR1 0x2 +#define PCA9685_SUBADR2 0x3 +#define PCA9685_SUBADR3 0x4 + +#define PCA9685_MODE1 0x0 +#define PCA9685_MODE2 0x1 +#define PCA9685_PRESCALE 0xFE + +#define LED0_ON_L 0x6 +#define LED0_ON_H 0x7 +#define LED0_OFF_L 0x8 +#define LED0_OFF_H 0x9 + +#define ALLLED_ON_L 0xFA +#define ALLLED_ON_H 0xFB +#define ALLLED_OFF_L 0xFC +#define ALLLED_OFF_H 0xFD + + + +class PCA9685Lib; //Forward declaration + +/*! \struct LEDarr + \brief LEDarr is a simple struct that stores the outpin it is, + *a pointer to the class it is in, and an overloaded = operator + * to make assigning duty cycle easier. + A more detailed class description. +*/ +struct LEDarr { + + uint8_t LEDnum; + PCA9685Lib* PCALib; + /** Overloaded assignement operator. + * Allows the user to more simply assign duty cycle values to a pin. + * @param duty unsigned 16 integer as the duty cycle + */ + void operator= (uint16_t duty); +}; + +/** My PCA9685Lib class +* used for controlling the pca9685 +* +* Example: +* @code +* // Change the duty cycle of an led +* +* #include "mbed.h" +* #include "PCA9685Lib.h" +* I2C i2c(p28, p27); +* PCA9685Lib LedDriver(i2c); +* LedDriver.begin(); +* LedDriver.setPWMFreq(1600); +* int main() +* { +* while(1) +* { +* +* for(uint16_t i = 0; i < 16; i++) + { + for(uint16_t j = 0; j < 4096; j += 8) + { + LedDriver.LED[i] = j; + } + } + for(uint16_t i = 0; i < 16; i++) + { + for(uint16_t j = 4095; j > 0; j -= 8) + { + LedDriver.LED[i] = j; + } + } +* +* } +* } +* @endcode +*/ + + +class PCA9685Lib +{ +public: + /** Create an PCA9685Lib instance, pass in an I2C object and the PCA9685 device address + * The default address is 0x80 + * @param i2cobj Pass and instantiated I2C object to the class to communicate with + * @param addr pass the device address the constructor (optional) + * + */ + PCA9685Lib(I2C i2cobj, int addr = 0x80); //0b 1_000000_(R/W) <- default slave adress + /** Seaches for I2C devices + + */ + void i2c_probe(void); + /** Resets the PCA9685 (clears device registers) + */ + void begin(void); + /** Sets the I2C frequency. + */ + void setI2Cfreq(int freq); + /** Resets the PCA9685 (clears device registers) + */ + void reset(void); + /** sets the pwm frequency + */ + void setPWMFreq(float freq); + /** Set the prescale registers on the pca9685 + */ + void setPrescale(uint8_t prescale); + /** Sets the Mode2 to 0x15 (totem pole configuration) by default + @param val Mode2 register value + */ + void setMode2(uint8_t val = 0x15); + /** Sets the pwm on a specific pin + * Each PWM clock cycle is divided in 4096 steps + * See datasheet for more details + * @param num Output pin number (0-15) + * @param on This is speicify at what point in the cycle the output will turn on (0-4095 or 0x000-0xFFF) + * @param off This is speicify at what point in the cycle the output will turn off (0-4095 or 0x000-0xFFF) + */ + void setPWM(uint8_t num, uint16_t on, uint16_t off); + /** Sets the duty cycle on a specific pin + * @param num Output pin number (0-15) + * @param duty This is speicify at what point in the cycle the output will turn off (0-4095 or 0x000-0xFFF) + */ + void setDuty(uint8_t num, uint16_t duty); + + + LEDarr LED[15]; +private: + int _i2caddr; + I2C i2c; + uint8_t read8(char addr); + void write8(char addr, char d); +}; + + +#endif +/* This is a library for our Adafruit 16-channel PWM & Servo driver Pick one up today in the adafruit shop! @@ -17,92 +163,5 @@ /***************************** This program was ported from https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library. - I also added some functions. - Shundo Kishi - *****************************/ -#ifndef _ADAFRUIT_PWMServoDriver_H -#define _ADAFRUIT_PWMServoDriver_H - -#include "mbed.h" -#include <cmath> - -#define PCA9685_SUBADR1 0x2 -#define PCA9685_SUBADR2 0x3 -#define PCA9685_SUBADR3 0x4 - -#define PCA9685_MODE1 0x0 -#define PCA9685_PRESCALE 0xFE - -#define LED0_ON_L 0x6 -#define LED0_ON_H 0x7 -#define LED0_OFF_L 0x8 -#define LED0_OFF_H 0x9 - -#define ALLLED_ON_L 0xFA -#define ALLLED_ON_H 0xFB -#define ALLLED_OFF_L 0xFC -#define ALLLED_OFF_H 0xFD - - - - /** An analog input, used for reading the voltage on a pin - * - * Example: - * @code - * // Print messages when the AnalogIn is greater than 50% - * - * #include "mbed.h" - * - * AnalogIn temperature(p20); - * - * int main() { - * while(1) { - * if(temperature > 0.5) { - * printf("Too hot! (%f)", temperature.read()); - * } - * } - * } - * @endcode */ -class PCA9685Lib; //Forward declaration -struct LEDarr -{ - /** Create an PCA9685 object, pass in an I2C object and the PCA9685 device address - * The default address is 0x80 - * @param i2cobj PCA9685Lib pin to connect to - * @param name (optional) A string to identify the object - */ - uint8_t LEDnum; - PCA9685Lib* PCALib; - void operator= (uint16_t duty); -}; - -class PCA9685Lib { - public: - /** Create an PCA9685 object, pass in an I2C object and the PCA9685 device address - * The default address is 0x80 - * @param i2cobj PCA9685Lib pin to connect to - * @param name (optional) A string to identify the object - */ - PCA9685Lib(I2C i2cobj, int addr = 0x80); //0b 1_000000_(R/W) <- default slave adress - void i2c_probe(void); - void begin(void); - void setI2Cfreq(int freq); - void reset(void); - void setPWMFreq(float freq); - void setPrescale(uint8_t prescale); - void setPWM(uint8_t num, uint16_t on, uint16_t off); - void setDuty(uint8_t num, uint16_t duty); - void operator[](int i); - LEDarr LED[15]; - private: - int _i2caddr; - I2C i2c; - - uint8_t read8(char addr); - void write8(char addr, char d); -}; - - -#endif