@@ -46,7 +46,6 @@ Adafruit_PWMServoDriver::Adafruit_PWMServoDriver(uint8_t addr, TwoWire *i2c) {
46
46
* @brief Setups the I2C interface and hardware
47
47
* @param prescale
48
48
* Sets External Clock (Optional)
49
- *
50
49
*/
51
50
void Adafruit_PWMServoDriver::begin (uint8_t prescale) {
52
51
_i2c->begin ();
@@ -63,7 +62,7 @@ void Adafruit_PWMServoDriver::begin(uint8_t prescale) {
63
62
* @brief Sends a reset command to the PCA9685 chip over I2C
64
63
*/
65
64
void Adafruit_PWMServoDriver::reset () {
66
- write8 (PCA9685_MODE1, 0x80 );
65
+ write8 (PCA9685_MODE1, MODE1_RESTART );
67
66
delay (10 );
68
67
}
69
68
@@ -72,7 +71,7 @@ void Adafruit_PWMServoDriver::reset() {
72
71
*/
73
72
void Adafruit_PWMServoDriver::sleep () {
74
73
uint8_t awake = read8 (PCA9685_MODE1);
75
- uint8_t sleep = awake | 0x10 ; // set sleep bit high
74
+ uint8_t sleep = awake | MODE1_SLEEP ; // set sleep bit high
76
75
write8 (PCA9685_MODE1, sleep);
77
76
delay (5 ); // wait until cycle ends for sleep to be active
78
77
}
@@ -82,31 +81,29 @@ void Adafruit_PWMServoDriver::sleep() {
82
81
*/
83
82
void Adafruit_PWMServoDriver::wakeup () {
84
83
uint8_t sleep = read8 (PCA9685_MODE1);
85
- uint8_t wakeup = sleep & ~0x10 ; // set sleep bit low
84
+ uint8_t wakeup = sleep & ~MODE1_SLEEP ; // set sleep bit low
86
85
write8 (PCA9685_MODE1, wakeup);
87
86
}
88
87
89
- /* *************************************************************************/
90
88
/* !
91
- @brief Sets EXTCLK pin to use the external clock
92
- @param prescale Configures the prescale value to be used by the external
93
- clock
94
- */
95
- /* *************************************************************************/
89
+ * @brief Sets EXTCLK pin to use the external clock
90
+ * @param prescale
91
+ * Configures the prescale value to be used by the external clock
92
+ */
96
93
void Adafruit_PWMServoDriver::setExtClk (uint8_t prescale) {
97
94
uint8_t oldmode = read8 (PCA9685_MODE1);
98
- uint8_t newmode = (oldmode & 0x7F ) | 0x10 ; // sleep
95
+ uint8_t newmode = (oldmode & ~MODE1_RESTART ) | MODE1_SLEEP ; // sleep
99
96
write8 (PCA9685_MODE1, newmode); // go to sleep, turn off internal oscillator
100
97
101
98
// This sets both the SLEEP and EXTCLK bits of the MODE1 register to switch to
102
99
// use the external clock.
103
- write8 (PCA9685_MODE1, (newmode |= 0x40 ));
100
+ write8 (PCA9685_MODE1, (newmode |= MODE1_EXTCLK ));
104
101
105
102
write8 (PCA9685_PRESCALE, prescale); // set the prescaler
106
103
107
104
delay (5 );
108
- write8 (PCA9685_MODE1,
109
- (newmode & ~( 0x10 )) | 0xa0 ); // clear the SLEEP bit to start
105
+ // clear the SLEEP bit to start
106
+ write8 (PCA9685_MODE1, (newmode & ~MODE1_SLEEP) | MODE1_RESTART | MODE1_AI);
110
107
111
108
#ifdef ENABLE_DEBUG_OUTPUT
112
109
Serial.print (" Mode now 0x" );
@@ -124,8 +121,7 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
124
121
Serial.println (freq);
125
122
#endif
126
123
127
- freq *=
128
- 0.9 ; // Correct for overshoot in the frequency setting (see issue #11).
124
+ freq *= 0.9 ; // Correct for overshoot in the frequency setting (see issue #11).
129
125
float prescaleval = 25000000 ;
130
126
prescaleval /= 4096 ;
131
127
prescaleval /= freq;
@@ -143,14 +139,13 @@ void Adafruit_PWMServoDriver::setPWMFreq(float freq) {
143
139
#endif
144
140
145
141
uint8_t oldmode = read8 (PCA9685_MODE1);
146
- uint8_t newmode = (oldmode & 0x7F ) | 0x10 ; // sleep
142
+ uint8_t newmode = (oldmode & ~MODE1_RESTART ) | MODE1_SLEEP ; // sleep
147
143
write8 (PCA9685_MODE1, newmode); // go to sleep
148
144
write8 (PCA9685_PRESCALE, prescale); // set the prescaler
149
145
write8 (PCA9685_MODE1, oldmode);
150
146
delay (5 );
151
- write8 (PCA9685_MODE1,
152
- oldmode |
153
- 0xa0 ); // This sets the MODE1 register to turn on auto increment.
147
+ // This sets the MODE1 register to turn on auto increment.
148
+ write8 (PCA9685_MODE1, oldmode | MODE1_RESTART | MODE1_AI);
154
149
155
150
#ifdef ENABLE_DEBUG_OUTPUT
156
151
Serial.print (" Mode now 0x" );
@@ -169,10 +164,10 @@ void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
169
164
uint8_t oldmode = read8 (PCA9685_MODE2);
170
165
uint8_t newmode;
171
166
if (totempole) {
172
- newmode = ( oldmode& 0x7F ) | 0x04 ;
167
+ newmode = oldmode | MODE2_OUTDRV ;
173
168
}
174
169
else {
175
- newmode = ( oldmode& 0x7F ) & ~0x04 ;
170
+ newmode = oldmode & ~MODE2_OUTDRV ;
176
171
}
177
172
write8 (PCA9685_MODE2, newmode);
178
173
#ifdef ENABLE_DEBUG_OUTPUT
@@ -189,7 +184,7 @@ void Adafruit_PWMServoDriver::setOutputMode(bool totempole) {
189
184
* @return requested PWM output value
190
185
*/
191
186
uint8_t Adafruit_PWMServoDriver::getPWM (uint8_t num) {
192
- _i2c->requestFrom ((int )_i2caddr, LED0_ON_L + 4 * num, (int )4 );
187
+ _i2c->requestFrom ((int )_i2caddr, PCA9685_LED0_ON_L + 4 * num, (int )4 );
193
188
return _i2c->read ();
194
189
}
195
190
@@ -210,7 +205,7 @@ void Adafruit_PWMServoDriver::setPWM(uint8_t num, uint16_t on, uint16_t off) {
210
205
#endif
211
206
212
207
_i2c->beginTransmission (_i2caddr);
213
- _i2c->write (LED0_ON_L + 4 * num);
208
+ _i2c->write (PCA9685_LED0_ON_L + 4 * num);
214
209
_i2c->write (on);
215
210
_i2c->write (on >> 8 );
216
211
_i2c->write (off);
0 commit comments