Skip to content

Commit 243fe8f

Browse files
committed
0.3.0 AS5600
1 parent 23dd265 commit 243fe8f

File tree

6 files changed

+283
-83
lines changed

6 files changed

+283
-83
lines changed

libraries/AS5600/AS5600.cpp

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: AS56000.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.0
4+
// VERSION: 0.3.0
55
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
66
// DATE: 2022-05-28
77
// URL: https://github.com/RobTillaart/AS5600
@@ -25,9 +25,13 @@
2525
// define constants for configuration functions.
2626
// fix conversion constants (4096 based)
2727
// add get- setOffset(degrees) functions. (no radians yet)
28+
// 0.2.1 notreleased add bool return to set() functions.
29+
// update Readme (analog / PWM out)
30+
// 0.3.0 2022-07-07 fix #18 invalid mask setConfigure().
31+
2832

2933
// TODO
30-
// Power-up time 1 minute
34+
// Power-up time 1 minute (need HW)
3135
// check Timing Characteristics
3236

3337

@@ -151,9 +155,11 @@ uint8_t AS5600::getZMCO()
151155
}
152156

153157

154-
void AS5600::setZPosition(uint16_t value)
158+
bool AS5600::setZPosition(uint16_t value)
155159
{
156-
writeReg2(AS5600_ZPOS, value & 0x0FFF);
160+
if (value > 0x0FFF) return false;
161+
writeReg2(AS5600_ZPOS, value);
162+
return true;
157163
}
158164

159165

@@ -164,9 +170,11 @@ uint16_t AS5600::getZPosition()
164170
}
165171

166172

167-
void AS5600::setMPosition(uint16_t value)
173+
bool AS5600::setMPosition(uint16_t value)
168174
{
169-
writeReg2(AS5600_MPOS, value & 0x0FFF);
175+
if (value > 0x0FFF) return false;
176+
writeReg2(AS5600_MPOS, value);
177+
return true;
170178
}
171179

172180

@@ -177,9 +185,11 @@ uint16_t AS5600::getMPosition()
177185
}
178186

179187

180-
void AS5600::setMaxAngle(uint16_t value)
188+
bool AS5600::setMaxAngle(uint16_t value)
181189
{
182-
writeReg2(AS5600_MANG, value & 0x0FFF);
190+
if (value > 0x0FFF) return false;
191+
writeReg2(AS5600_MANG, value);
192+
return true;
183193
}
184194

185195

@@ -190,111 +200,120 @@ uint16_t AS5600::getMaxAngle()
190200
}
191201

192202

193-
void AS5600::setConfigure(uint16_t value)
203+
bool AS5600::setConfigure(uint16_t value)
194204
{
195-
writeReg2(AS5600_CONF, value & 0x2FFF);
205+
if (value > 0x3FFF) return false;
206+
writeReg2(AS5600_CONF, value);
207+
return true;
196208
}
197209

198210

199211
uint16_t AS5600::getConfigure()
200212
{
201-
uint16_t value = readReg2(AS5600_CONF) & 0x2FFF;
213+
uint16_t value = readReg2(AS5600_CONF) & 0x3FFF;
202214
return value;
203215
}
204216

205217

206218
// details configure
207-
void AS5600::setPowerMode(uint8_t powerMode)
219+
bool AS5600::setPowerMode(uint8_t powerMode)
208220
{
209-
if (powerMode > 3) return;
221+
if (powerMode > 3) return false;
210222
uint8_t value = readReg(AS5600_CONF + 1);
211223
value &= ~AS5600_CONF_POWER_MODE;
212224
value |= powerMode;
213225
writeReg(AS5600_CONF + 1, value);
226+
return true;
214227
}
215228

216229
uint8_t AS5600::getPowerMode()
217230
{
218231
return readReg(AS5600_CONF + 1) & 0x03;
219232
}
220233

221-
void AS5600::setHysteresis(uint8_t hysteresis)
234+
bool AS5600::setHysteresis(uint8_t hysteresis)
222235
{
223-
if (hysteresis > 3) return;
236+
if (hysteresis > 3) return false;
224237
uint8_t value = readReg(AS5600_CONF + 1);
225238
value &= ~AS5600_CONF_HYSTERESIS;
226239
value |= (hysteresis << 2);
227240
writeReg(AS5600_CONF + 1, value);
241+
return true;
228242
}
229243

230244
uint8_t AS5600::getHysteresis()
231245
{
232246
return (readReg(AS5600_CONF + 1) >> 2) & 0x03;
233247
}
234248

235-
void AS5600::setOutputMode(uint8_t outputMode)
249+
bool AS5600::setOutputMode(uint8_t outputMode)
236250
{
237-
if (outputMode > 2) return;
251+
if (outputMode > 2) return false;
238252
uint8_t value = readReg(AS5600_CONF + 1);
239253
value &= ~AS5600_CONF_OUTPUT_MODE;
240254
value |= (outputMode << 4);
241255
writeReg(AS5600_CONF + 1, value);
256+
return true;
242257
}
243258

244259
uint8_t AS5600::getOutputMode()
245260
{
246261
return (readReg(AS5600_CONF + 1) >> 4) & 0x03;
247262
}
248263

249-
void AS5600::setPWMFrequency(uint8_t pwmFreq)
264+
bool AS5600::setPWMFrequency(uint8_t pwmFreq)
250265
{
251-
if (pwmFreq > 3) return;
266+
if (pwmFreq > 3) return false;
252267
uint8_t value = readReg(AS5600_CONF + 1);
253268
value &= ~AS5600_CONF_PWM_FREQUENCY;
254269
value |= (pwmFreq << 6);
255270
writeReg(AS5600_CONF + 1, value);
271+
return true;
256272
}
257273

258274
uint8_t AS5600::getPWMFrequency()
259275
{
260276
return (readReg(AS5600_CONF + 1) >> 6) & 0x03;
261277
}
262278

263-
void AS5600::setSlowFilter(uint8_t mask)
279+
bool AS5600::setSlowFilter(uint8_t mask)
264280
{
265-
if (mask > 3) return;
281+
if (mask > 3) return false;
266282
uint8_t value = readReg(AS5600_CONF);
267283
value &= ~AS5600_CONF_SLOW_FILTER;
268284
value |= mask;
269285
writeReg(AS5600_CONF, value);
286+
return true;
270287
}
271288

272289
uint8_t AS5600::getSlowFilter()
273290
{
274291
return readReg(AS5600_CONF) & 0x03;
275292
}
276293

277-
void AS5600::setFastFilter(uint8_t mask)
294+
bool AS5600::setFastFilter(uint8_t mask)
278295
{
279-
if (mask > 7) return;
296+
if (mask > 7) return false;
280297
uint8_t value = readReg(AS5600_CONF);
281298
value &= ~AS5600_CONF_FAST_FILTER;
282299
value |= (mask << 2);
283300
writeReg(AS5600_CONF, value);
301+
return true;
284302
}
285303

286304
uint8_t AS5600::getFastFilter()
287305
{
288306
return (readReg(AS5600_CONF) >> 2) & 0x07;
289307
}
290308

291-
void AS5600::setWatchDog(uint8_t mask)
309+
bool AS5600::setWatchDog(uint8_t mask)
292310
{
293-
if (mask > 1) return;
311+
if (mask > 1) return false;
294312
uint8_t value = readReg(AS5600_CONF);
295313
value &= ~AS5600_CONF_WATCH_DOG;
296314
value |= (mask << 5);
297315
writeReg(AS5600_CONF, value);
316+
return true;
298317
}
299318

300319
uint8_t AS5600::getWatchDog()
@@ -324,7 +343,7 @@ uint16_t AS5600::readAngle()
324343
{
325344
uint16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;
326345
if (_offset > 0) value = (value + _offset) & 0x0FFF;
327-
346+
328347
if ((_directionPin == 255) && (_direction == AS5600_COUNTERCLOCK_WISE))
329348
{
330349
value = (4096 - value) & 4095;
@@ -333,18 +352,18 @@ uint16_t AS5600::readAngle()
333352
}
334353

335354

336-
void AS5600::setOffset(float degrees)
355+
bool AS5600::setOffset(float degrees)
337356
{
338-
bool neg = false;
339-
if (degrees < 0)
340-
{
341-
neg = true;
342-
degrees = -degrees;
343-
}
357+
// expect loss of precision.
358+
if (abs(degrees) > 36000) return false;
359+
bool neg = (degrees < 0);
360+
if (neg) degrees = -degrees;
361+
344362
uint16_t offset = round(degrees * (4096 / 360.0));
345363
offset &= 4095;
346364
if (neg) offset = 4096 - offset;
347-
_offset = offset;
365+
_offset = offset;
366+
return true;
348367
}
349368

350369

libraries/AS5600/AS5600.h

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: AS5600.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.0
5+
// VERSION: 0.3.0
66
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
77
// DATE: 2022-05-28
88
// URL: https://github.com/RobTillaart/AS5600
@@ -12,7 +12,7 @@
1212
#include "Wire.h"
1313

1414

15-
#define AS5600_LIB_VERSION (F("0.2.0"))
15+
#define AS5600_LIB_VERSION (F("0.3.0"))
1616

1717
// setDirection
1818
const uint8_t AS5600_CLOCK_WISE = 0; // LOW
@@ -93,57 +93,76 @@ class AS5600
9393

9494
// SET CONFIGURE REGISTERS
9595
// read datasheet first
96+
97+
// 0 = AS5600_CLOCK_WISE
98+
// 1 = AS5600_COUNTERCLOCK_WISE
99+
// all other = AS5600_COUNTERCLOCK_WISE
96100
void setDirection(uint8_t direction = AS5600_CLOCK_WISE);
97101
uint8_t getDirection();
98102

99103
uint8_t getZMCO();
100104

101-
void setZPosition(uint16_t value);
105+
// 0 .. 4095
106+
// returns false if parameter out of range
107+
bool setZPosition(uint16_t value);
102108
uint16_t getZPosition();
103109

104-
void setMPosition(uint16_t value);
110+
// 0 .. 4095
111+
// returns false if parameter out of range
112+
bool setMPosition(uint16_t value);
105113
uint16_t getMPosition();
106114

107-
void setMaxAngle(uint16_t value);
115+
// 0 .. 4095
116+
// returns false if parameter out of range
117+
bool setMaxAngle(uint16_t value);
108118
uint16_t getMaxAngle();
109119

110120
// access the whole configuration register
111121
// check datasheet for bit fields
112-
void setConfigure(uint16_t value);
122+
// returns false if parameter out of range
123+
bool setConfigure(uint16_t value);
113124
uint16_t getConfigure();
114125

115126
// access details of the configuration register
116127
// 0 = Normal
117128
// 1,2,3 are low power mode - check datasheet
118-
void setPowerMode(uint8_t powerMode);
129+
// returns false if parameter out of range
130+
bool setPowerMode(uint8_t powerMode);
119131
uint8_t getPowerMode();
120132

121133
// 0 = off 1 = lsb1 2 = lsb2 3 = lsb3
122-
void setHysteresis(uint8_t hysteresis);
134+
// returns false if parameter out of range
135+
// suppresses noise when the magnet is not moving.
136+
bool setHysteresis(uint8_t hysteresis);
123137
uint8_t getHysteresis();
124138

125139
// 0 = analog 0-100%
126140
// 1 = analog 10-90%
127141
// 2 = PWM
128-
void setOutputMode(uint8_t outputMode);
142+
// returns false if parameter out of range
143+
bool setOutputMode(uint8_t outputMode);
129144
uint8_t getOutputMode();
130145

131146
// 0 = 115 1 = 230 2 = 460 3 = 920 (Hz)
132-
void setPWMFrequency(uint8_t pwmFreq);
147+
// returns false if parameter out of range
148+
bool setPWMFrequency(uint8_t pwmFreq);
133149
uint8_t getPWMFrequency();
134150

135151
// 0 = 16x 1 = 8x 2 = 4x 3 = 2x
136-
void setSlowFilter(uint8_t mask);
152+
// returns false if parameter out of range
153+
bool setSlowFilter(uint8_t mask);
137154
uint8_t getSlowFilter();
138155

139156
// 0 = none 1 = LSB6 2 = LSB7 3 = LSB9
140157
// 4 = LSB18 5 = LSB21 6 = LSB24 7 = LSB10
141-
void setFastFilter(uint8_t mask);
158+
// returns false if parameter out of range
159+
bool setFastFilter(uint8_t mask);
142160
uint8_t getFastFilter();
143161

144162
// 0 = OFF
145163
// 1 = ON (auto low power mode)
146-
void setWatchDog(uint8_t mask);
164+
// returns false if parameter out of range
165+
bool setWatchDog(uint8_t mask);
147166
uint8_t getWatchDog();
148167

149168

@@ -152,7 +171,10 @@ class AS5600
152171
uint16_t readAngle();
153172

154173
// software based offset.
155-
void setOffset(float degrees);
174+
// degrees = -359.99 .. 359.99 (preferred)
175+
// returns false if abs(parameter) > 36000
176+
// => expect loss of precision
177+
bool setOffset(float degrees);
156178
float getOffset();
157179

158180

0 commit comments

Comments
 (0)