Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ic2_test_bus by
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 = 0x80); 00081 00082 void frequencyI2C(int freq); 00083 00084 void begin(void); //Initialize the controller 00085 void reset(void); //Reset the controller 00086 void setPrescale(uint8_t prescale);//setPrescale(prescale) 00087 /** Set prescale 00088 * 00089 * @param prescale: set scale for the PWM frequency 00090 * 00091 */ 00092 void setPWMFreq(float freq);//Set the pwm frequency 00093 /** Set frequency 00094 * 00095 * @param frequency in Hz 00096 * 00097 */ 00098 void setPWM(uint8_t num, uint16_t on, uint16_t off);//SetPWM(channel, on, off) 00099 /** Set the start (on) and the end (off) of the part of the PWM pulse of the channel 00100 * @param channel : from 0 to 15 the channel the should be update 00101 * @param on: from 0 to 4095 the tick when the signal should pass from low to high 00102 * @param off: from 0 to 4095 the tick when the signal should pass from high to low 00103 */ 00104 00105 void setPWM_ALL(uint16_t on, uint16_t off); //SetPWM(on, off) 00106 00107 private: 00108 void write8(uint8_t address, uint8_t data); 00109 char read8(char address); 00110 int _i2caddr; 00111 I2C i2c; 00112 }; 00113 00114 #endif
Generated on Thu Jul 14 2022 02:59:55 by
1.7.2
