@@ -45,21 +45,54 @@ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(TwoWire *i2c, uint8_t addr) {
45
45
/* !
46
46
* @brief Setups the I2C interface and hardware
47
47
*/
48
- void Adafruit_PWMServoDriver::begin (void ) {
48
+ void Adafruit_PWMServoDriver::begin (uint8_t prescale ) {
49
49
_i2c->begin ();
50
50
reset ();
51
51
// set a default frequency
52
- setPWMFreq (1000 );
52
+ if (prescale) {
53
+ setExtClk (prescale);
54
+ } else {
55
+ // set a default frequency
56
+ setPWMFreq (1000 );
57
+ }
53
58
}
54
59
55
60
/* !
56
61
* @brief Sends a reset command to the PCA9685 chip over I2C
57
62
*/
58
- void Adafruit_PWMServoDriver::reset (void ) {
63
+ void Adafruit_PWMServoDriver::reset () {
59
64
write8 (PCA9685_MODE1, 0x80 );
60
65
delay (10 );
61
66
}
62
67
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
+
63
96
/* !
64
97
* @brief Sets the PWM frequency for the entire chip, up to ~1.6 KHz
65
98
* @param freq Floating point frequency that we will attempt to match
0 commit comments