This is a library for the PCA9685 ported from the Adafruit Ardiuno library.
Diff: PCA9685Lib.h
- Revision:
- 2:ec40a85eba51
- Parent:
- 1:f1b17f9d387e
--- 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