![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
basic pca9685
Fork of ic2_test_bus by
PCA9685.h@1:dbe78e80d722, 2018-10-29 (annotated)
- Committer:
- richnash
- Date:
- Mon Oct 29 14:56:07 2018 +0000
- Revision:
- 1:dbe78e80d722
basic pca9685
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
richnash | 1:dbe78e80d722 | 1 | /**Library for PCA9685 16-channel, 12-bit PWM Fm+ I²C-bus LED controller |
richnash | 1:dbe78e80d722 | 2 | * Example code |
richnash | 1:dbe78e80d722 | 3 | * @code |
richnash | 1:dbe78e80d722 | 4 | * |
richnash | 1:dbe78e80d722 | 5 | *#include"PCA9685.h" |
richnash | 1:dbe78e80d722 | 6 | *#include"mbed.h" |
richnash | 1:dbe78e80d722 | 7 | * |
richnash | 1:dbe78e80d722 | 8 | * |
richnash | 1:dbe78e80d722 | 9 | * |
richnash | 1:dbe78e80d722 | 10 | *PCA9685 pwm(D14,D15); |
richnash | 1:dbe78e80d722 | 11 | * |
richnash | 1:dbe78e80d722 | 12 | *void setServoPulse(uint8_t n, float pulse) { |
richnash | 1:dbe78e80d722 | 13 | * float pulselength = 10000; // 10,000 units per seconds |
richnash | 1:dbe78e80d722 | 14 | * pulse = 4094 * pulse / pulselength; |
richnash | 1:dbe78e80d722 | 15 | * pwm.setPWM(n, 0, pulse); |
richnash | 1:dbe78e80d722 | 16 | *} |
richnash | 1:dbe78e80d722 | 17 | * |
richnash | 1:dbe78e80d722 | 18 | *void initServoDriver() { |
richnash | 1:dbe78e80d722 | 19 | * pwm.begin(); |
richnash | 1:dbe78e80d722 | 20 | * pwm.setPrescale(64); //This value is decided for 10ms interval. |
richnash | 1:dbe78e80d722 | 21 | * pwm.frequencyI2C(400000); //400kHz |
richnash | 1:dbe78e80d722 | 22 | *} |
richnash | 1:dbe78e80d722 | 23 | * |
richnash | 1:dbe78e80d722 | 24 | * int main() { |
richnash | 1:dbe78e80d722 | 25 | * |
richnash | 1:dbe78e80d722 | 26 | * while(1){ |
richnash | 1:dbe78e80d722 | 27 | * initServoDriver(); |
richnash | 1:dbe78e80d722 | 28 | * wait(0.2); |
richnash | 1:dbe78e80d722 | 29 | * setServoPulse(0, 2300); |
richnash | 1:dbe78e80d722 | 30 | * setServoPulse(1, 500); |
richnash | 1:dbe78e80d722 | 31 | * wait(0.5);//delay necessary to perform the action |
richnash | 1:dbe78e80d722 | 32 | * setServoPulse(0, 1350); |
richnash | 1:dbe78e80d722 | 33 | * setServoPulse(1, 1350); |
richnash | 1:dbe78e80d722 | 34 | * wait(0.5); |
richnash | 1:dbe78e80d722 | 35 | * setServoPulse(0,550); |
richnash | 1:dbe78e80d722 | 36 | * setServoPulse(1, 2250); |
richnash | 1:dbe78e80d722 | 37 | * wait(0.5); |
richnash | 1:dbe78e80d722 | 38 | * setServoPulse(0, 2300); |
richnash | 1:dbe78e80d722 | 39 | * wait(2); |
richnash | 1:dbe78e80d722 | 40 | * for (int mov = 550; mov < 2300; mov++){ |
richnash | 1:dbe78e80d722 | 41 | * setServoPulse(0, mov); |
richnash | 1:dbe78e80d722 | 42 | * wait(0.001); |
richnash | 1:dbe78e80d722 | 43 | * } |
richnash | 1:dbe78e80d722 | 44 | * for (int mov = 500; mov < 2200; mov++){ |
richnash | 1:dbe78e80d722 | 45 | * setServoPulse(1, mov); |
richnash | 1:dbe78e80d722 | 46 | * wait(0.001); |
richnash | 1:dbe78e80d722 | 47 | * } |
richnash | 1:dbe78e80d722 | 48 | * } |
richnash | 1:dbe78e80d722 | 49 | *} |
richnash | 1:dbe78e80d722 | 50 | *@endcode |
richnash | 1:dbe78e80d722 | 51 | * |
richnash | 1:dbe78e80d722 | 52 | */ |
richnash | 1:dbe78e80d722 | 53 | #ifndef PCA9685_H |
richnash | 1:dbe78e80d722 | 54 | #define PCA9685_H |
richnash | 1:dbe78e80d722 | 55 | |
richnash | 1:dbe78e80d722 | 56 | #include "mbed.h" |
richnash | 1:dbe78e80d722 | 57 | #include <cmath> |
richnash | 1:dbe78e80d722 | 58 | //register definitions |
richnash | 1:dbe78e80d722 | 59 | #define PCA9685_SUBADR1 0x2 |
richnash | 1:dbe78e80d722 | 60 | #define PCA9685_SUBADR2 0x3 |
richnash | 1:dbe78e80d722 | 61 | #define PCA9685_SUBADR3 0x4 |
richnash | 1:dbe78e80d722 | 62 | |
richnash | 1:dbe78e80d722 | 63 | #define PCA9685_MODE1 0x0 |
richnash | 1:dbe78e80d722 | 64 | #define PCA9685_PRESCALE 0xFE |
richnash | 1:dbe78e80d722 | 65 | |
richnash | 1:dbe78e80d722 | 66 | #define LED0_ON_L 0x6 |
richnash | 1:dbe78e80d722 | 67 | #define LED0_ON_H 0x7 |
richnash | 1:dbe78e80d722 | 68 | #define LED0_OFF_L 0x8 |
richnash | 1:dbe78e80d722 | 69 | #define LED0_OFF_H 0x9 |
richnash | 1:dbe78e80d722 | 70 | |
richnash | 1:dbe78e80d722 | 71 | #define ALLLED_ON_L 0xFA |
richnash | 1:dbe78e80d722 | 72 | #define ALLLED_ON_H 0xFB |
richnash | 1:dbe78e80d722 | 73 | #define ALLLED_OFF_L 0xFC |
richnash | 1:dbe78e80d722 | 74 | #define ALLLED_OFF_H 0xFD |
richnash | 1:dbe78e80d722 | 75 | |
richnash | 1:dbe78e80d722 | 76 | |
richnash | 1:dbe78e80d722 | 77 | class PCA9685 |
richnash | 1:dbe78e80d722 | 78 | { |
richnash | 1:dbe78e80d722 | 79 | public: |
richnash | 1:dbe78e80d722 | 80 | PCA9685(PinName sda, PinName scl, int addr = 0x80); |
richnash | 1:dbe78e80d722 | 81 | |
richnash | 1:dbe78e80d722 | 82 | void frequencyI2C(int freq); |
richnash | 1:dbe78e80d722 | 83 | |
richnash | 1:dbe78e80d722 | 84 | void begin(void); //Initialize the controller |
richnash | 1:dbe78e80d722 | 85 | void reset(void); //Reset the controller |
richnash | 1:dbe78e80d722 | 86 | void setPrescale(uint8_t prescale);//setPrescale(prescale) |
richnash | 1:dbe78e80d722 | 87 | /** Set prescale |
richnash | 1:dbe78e80d722 | 88 | * |
richnash | 1:dbe78e80d722 | 89 | * @param prescale: set scale for the PWM frequency |
richnash | 1:dbe78e80d722 | 90 | * |
richnash | 1:dbe78e80d722 | 91 | */ |
richnash | 1:dbe78e80d722 | 92 | void setPWMFreq(float freq);//Set the pwm frequency |
richnash | 1:dbe78e80d722 | 93 | /** Set frequency |
richnash | 1:dbe78e80d722 | 94 | * |
richnash | 1:dbe78e80d722 | 95 | * @param frequency in Hz |
richnash | 1:dbe78e80d722 | 96 | * |
richnash | 1:dbe78e80d722 | 97 | */ |
richnash | 1:dbe78e80d722 | 98 | void setPWM(uint8_t num, uint16_t on, uint16_t off);//SetPWM(channel, on, off) |
richnash | 1:dbe78e80d722 | 99 | /** Set the start (on) and the end (off) of the part of the PWM pulse of the channel |
richnash | 1:dbe78e80d722 | 100 | * @param channel : from 0 to 15 the channel the should be update |
richnash | 1:dbe78e80d722 | 101 | * @param on: from 0 to 4095 the tick when the signal should pass from low to high |
richnash | 1:dbe78e80d722 | 102 | * @param off: from 0 to 4095 the tick when the signal should pass from high to low |
richnash | 1:dbe78e80d722 | 103 | */ |
richnash | 1:dbe78e80d722 | 104 | |
richnash | 1:dbe78e80d722 | 105 | void setPWM_ALL(uint16_t on, uint16_t off); //SetPWM(on, off) |
richnash | 1:dbe78e80d722 | 106 | |
richnash | 1:dbe78e80d722 | 107 | private: |
richnash | 1:dbe78e80d722 | 108 | void write8(uint8_t address, uint8_t data); |
richnash | 1:dbe78e80d722 | 109 | char read8(char address); |
richnash | 1:dbe78e80d722 | 110 | int _i2caddr; |
richnash | 1:dbe78e80d722 | 111 | I2C i2c; |
richnash | 1:dbe78e80d722 | 112 | }; |
richnash | 1:dbe78e80d722 | 113 | |
richnash | 1:dbe78e80d722 | 114 | #endif |