Skip to content

Commit 64e4ea4

Browse files
committed
Implemented a method for changing the pin output mode
The board is advertised as supporting both open drain and push-pull configurations for the output pins. The library currently does not expose this functionality. I suggest that a setOutputMode method is added that can change the mode.
1 parent fa51b6f commit 64e4ea4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Adafruit_PWMServoDriver.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,31 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
158158
#endif
159159
}
160160

161+
/*!
162+
* @brief Sets the output mode of the PCA9685 to either
163+
* open drain or push pull / totempole.
164+
* Warning: LEDs with integrated zener diodes should
165+
* only be driven in open drain mode.
166+
* @param pushPull totempole if true, open drain if false.
167+
*/
168+
void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
169+
uint8_t oldmode = read8(PCA9685_MODE2);
170+
uint8_t newmode;
171+
if (totempole) {
172+
newmode = (oldmode&0x7F) | 0x04;
173+
}
174+
else {
175+
newmode = (oldmode&0x7F) & ~0x04;
176+
}
177+
write8(PCA9685_MODE2, newmode);
178+
#ifdef ENABLE_DEBUG_OUTPUT
179+
Serial.print("Setting output mode: ");
180+
Serial.print(totempole ? "totempole" : "open drain");
181+
Serial.print(" by setting MODE2 to ");
182+
Serial.println(newmode);
183+
#endif
184+
}
185+
161186
/*!
162187
* @brief Gets the PWM output of one of the PCA9685 pins
163188
* @param num One of the PWM output pins, from 0 to 15

Adafruit_PWMServoDriver.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*
2020
* BSD license, all text above must be included in any redistribution
2121
*/
22-
2322
#ifndef _ADAFRUIT_PWMServoDriver_H
2423
#define _ADAFRUIT_PWMServoDriver_H
2524

@@ -31,6 +30,7 @@
3130
#define PCA9685_SUBADR3 0x4 /**< i2c bus address 3 */
3231

3332
#define PCA9685_MODE1 0x0 /**< Mode Register 1 */
33+
#define PCA9685_MODE2 0x1 /**< Mode Register 2 */
3434
#define PCA9685_PRESCALE 0xFE /**< Prescaler for PWM output frequency */
3535

3636
#define LED0_ON_L 0x6 /**< LED0 output and brightness control byte 0 */
@@ -55,6 +55,7 @@ class Adafruit_PWMServoDriver {
5555
void wakeup();
5656
void setExtClk(uint8_t prescale);
5757
void setPWMFreq(float freq);
58+
void setOutputMode(bool totempole);
5859
uint8_t getPWM(uint8_t num);
5960
void setPWM(uint8_t num, uint16_t on, uint16_t off);
6061
void setPin(uint8_t num, uint16_t val, bool invert=false);

0 commit comments

Comments
 (0)