Sleek / Mbed 2 deprecated PCA9685PWM

Dependencies:   mbed

Fork of PCA9685PWM by Paolo Sanna

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PCA9685.h Source File

PCA9685.h

00001 /**Library for PCA9685 16-channel, 12-bit PWM Fm+ I²C-bus LED controller
00002 * Example code
00003 * @code
00004 *
00005 *#include"PCA9685.h"
00006 *#include"mbed.h"
00007 *
00008 *
00009 *
00010 *PCA9685 pwm(D14,D15);
00011 * 
00012 *void setServoPulse(uint8_t n, float pulse) {
00013 *    float pulselength = 10000;   // 10,000 units per seconds
00014 *    pulse = 4094 * pulse / pulselength;
00015 *    pwm.setPWM(n, 0, pulse);
00016 *}
00017 * 
00018 *void initServoDriver() {
00019 *    pwm.begin();
00020 *    pwm.setPrescale(64);    //This value is decided for 10ms interval.
00021 *    pwm.frequencyI2C(400000); //400kHz
00022 *}
00023 * 
00024 * int main() {
00025 *
00026 *    while(1){
00027 *    initServoDriver();
00028 *    wait(0.2);
00029 *    setServoPulse(0, 2300);
00030 *    setServoPulse(1, 500);    
00031 *    wait(0.5);//delay necessary to perform the action
00032 *    setServoPulse(0, 1350);
00033 *    setServoPulse(1, 1350);
00034 *    wait(0.5);
00035 *    setServoPulse(0,550);
00036 *    setServoPulse(1, 2250);
00037 *    wait(0.5);
00038 *    setServoPulse(0, 2300);
00039 *    wait(2);
00040 *    for (int mov = 550; mov < 2300; mov++){
00041 *    setServoPulse(0, mov);
00042 *    wait(0.001); 
00043 *    }  
00044 *    for (int mov = 500; mov < 2200; mov++){
00045 *    setServoPulse(1, mov);
00046 *    wait(0.001); 
00047 *    }     
00048 *   }
00049 *}
00050 *@endcode
00051 *
00052 */
00053 #ifndef PCA9685_H
00054 #define PCA9685_H
00055 
00056 #include "mbed.h"
00057 #include <cmath>
00058 //register definitions
00059 #define PCA9685_SUBADR1 0x2
00060 #define PCA9685_SUBADR2 0x3
00061 #define PCA9685_SUBADR3 0x4
00062 
00063 #define PCA9685_MODE1 0x0
00064 #define PCA9685_PRESCALE 0xFE
00065 
00066 #define LED0_ON_L 0x6
00067 #define LED0_ON_H 0x7
00068 #define LED0_OFF_L 0x8
00069 #define LED0_OFF_H 0x9
00070 
00071 #define ALLLED_ON_L 0xFA
00072 #define ALLLED_ON_H 0xFB
00073 #define ALLLED_OFF_L 0xFC
00074 #define ALLLED_OFF_H 0xFD
00075 
00076 
00077 class PCA9685
00078 {
00079 public:
00080     PCA9685(PinName sda, PinName scl, int addr = 0xC0);
00081     void frequencyI2C(int freq);
00082     void begin(void); //Initialize the controller
00083     void reset(void); //Reset the controller
00084     void setPrescale(uint8_t prescale);//setPrescale(prescale)
00085     /** Set prescale
00086      *
00087      *  @param prescale: set scale for the PWM frequency
00088      *
00089      */
00090     void setPWMFreq(float freq);//Set the pwm frequency
00091     /** Set frequency
00092       *
00093       * @param frequency in Hz
00094       *
00095       */
00096     void setPWM(uint8_t num, uint16_t on, uint16_t off);//SetPWM(channel, on, off)
00097     /** Set the start (on) and the end (off) of the part of the PWM pulse of the channel
00098      *  @param channel : from 0 to 15 the channel the should be update
00099      *  @param  on: from 0 to 4095 the tick when the signal should pass from low to high
00100      *  @param off: from 0 to 4095 the tick when the signal should pass from high to low
00101      */
00102 private:
00103     void write8(uint8_t address, uint8_t data);
00104     char read8(char address);
00105     int _i2caddr;
00106     I2C i2c;
00107 };
00108 
00109 #endif