Skip to content

Commit f39eb59

Browse files
committed
added External Clock by configuring the EXTCLK bit
1 parent 12c4926 commit f39eb59

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Adafruit_PWMServoDriver.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,54 @@ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(TwoWire *i2c, uint8_t addr) {
4545
/*!
4646
* @brief Setups the I2C interface and hardware
4747
*/
48-
void Adafruit_PWMServoDriver::begin(void) {
48+
void Adafruit_PWMServoDriver::begin(uint8_t prescale) {
4949
_i2c->begin();
5050
reset();
5151
// set a default frequency
52-
setPWMFreq(1000);
52+
if (prescale) {
53+
setExtClk(prescale);
54+
} else {
55+
// set a default frequency
56+
setPWMFreq(1000);
57+
}
5358
}
5459

5560
/*!
5661
* @brief Sends a reset command to the PCA9685 chip over I2C
5762
*/
58-
void Adafruit_PWMServoDriver::reset(void) {
63+
void Adafruit_PWMServoDriver::reset() {
5964
write8(PCA9685_MODE1, 0x80);
6065
delay(10);
6166
}
6267

68+
/**************************************************************************/
69+
/*!
70+
@brief Sets EXTCLK pin to use the external clock
71+
@param prescale Configures the prescale value to be used by the external
72+
clock
73+
*/
74+
/**************************************************************************/
75+
void Adafruit_PWMServoDriver::setExtClk(uint8_t prescale) {
76+
uint8_t oldmode = read8(PCA9685_MODE1);
77+
uint8_t newmode = (oldmode & 0x7F) | 0x10; // sleep
78+
write8(PCA9685_MODE1, newmode); // go to sleep, turn off internal oscillator
79+
80+
// This sets both the SLEEP and EXTCLK bits of the MODE1 register to switch to
81+
// use the external clock.
82+
write8(PCA9685_MODE1, (newmode |= 0x40));
83+
84+
write8(PCA9685_PRESCALE, prescale); // set the prescaler
85+
86+
delay(5);
87+
write8(PCA9685_MODE1,
88+
(newmode & ~(0x10)) | 0xa0); // clear the SLEEP bit to start
89+
90+
#ifdef ENABLE_DEBUG_OUTPUT
91+
Serial.print("Mode now 0x");
92+
Serial.println(read8(PCA9685_MODE1), HEX);
93+
#endif
94+
}
95+
6396
/*!
6497
* @brief Sets the PWM frequency for the entire chip, up to ~1.6 KHz
6598
* @param freq Floating point frequency that we will attempt to match

Adafruit_PWMServoDriver.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@
4949
class Adafruit_PWMServoDriver {
5050
public:
5151
Adafruit_PWMServoDriver(TwoWire *I2C = &Wire, uint8_t addr = 0x40);
52-
void begin(void);
53-
void reset(void);
52+
void begin(uint8_t prescale = 0);
53+
void reset();
54+
void setExtClk(uint8_t prescale);
5455
void setPWMFreq(float freq);
5556
void setPWM(uint8_t num, uint16_t on, uint16_t off);
5657
void setPin(uint8_t num, uint16_t val, bool invert=false);

0 commit comments

Comments
 (0)