-
Notifications
You must be signed in to change notification settings - Fork 0
/
AS5600.cpp
437 lines (396 loc) · 11.1 KB
/
AS5600.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/****************************************************
AMS 5600 class for Arduino platform
Author: Tom Denton
Date: 15 Dec 2014
File: AMS_5600.cpp
Version 1.00
www.ams.com
Description: This class has been designed to
access the AMS 5600 “potuino” shield.
*****************************************************/
#include "Arduino.h"
#include "AS5600.h"
#include "Wire.h"
/****************************************************
Method: AMS_5600
In: none
Out: none
Description: constructor class for AMS 5600
*****************************************************/
AMS_5600::AMS_5600(int i2cAddress)
{
/* set i2c address */
_ams5600_Address = i2cAddress;
/* load register values */
/* c++ class forbids pre loading of variables */
_zmco = 0x00;
_zpos_hi = 0x01;
_zpos_lo = 0x02;
_mpos_hi = 0x03;
_mpos_lo = 0x04;
_mang_hi = 0x05;
_mang_lo = 0x06;
_conf_hi = 0x07;
_conf_lo = 0x08;
_raw_ang_hi = 0x0c;
_raw_ang_lo = 0x0d;
_ang_hi = 0x0e;
_ang_lo = 0x0f;
_stat = 0x0b;
_agc = 0x1a;
_mag_hi = 0x1b;
_mag_lo = 0x1c;
_burn = 0xff;
}
/* mode = 0, output PWM, mode = 1 output analog (full range from 0% to 100% between GND and VDD */
void AMS_5600::setOutPut(uint8_t mode)
{
uint8_t config_status;
config_status = readOneByte(_conf_lo);
if (mode == 1)
{
config_status = config_status & 0xcf;
}
else
{
uint8_t config_status;
config_status = readOneByte(_conf_lo);
if (mode == 1)
{
config_status = config_status & 0xcf;
}
else
{
config_status = config_status & 0xef;
}
writeOneByte(_conf_lo, lowByte(config_status));
}
}
/****************************************************
Method: AMS_5600
In: none
Out: i2c address of AMS 5600
Description: returns i2c address of AMS 5600
****************************************************/
int AMS_5600::getAddress()
{
return _ams5600_Address;
}
/*******************************************************
Method: setMaxAngle
In: new maximum angle to set OR none
Out: value of max angle register
Description: sets a value in maximum angle register.
If no value is provided, method will read position of
magnet. Setting this register zeros out max position
register.
*******************************************************/
word AMS_5600::setMaxAngle(word newMaxAngle)
{
word retVal;
_maxAngle = newMaxAngle;
writeOneByte(_mang_hi, highByte(_maxAngle));
delay(2);
writeOneByte(_mang_lo, lowByte(_maxAngle));
delay(2);
retVal = readTwoBytes(_mang_hi);
return retVal;
}
/*******************************************************
Method: getMaxAngle
In: none
Out: value of max angle register
Description: gets value of maximum angle register.
*******************************************************/
word AMS_5600::getMaxAngle()
{
return readTwoBytes(_mang_hi);
}
/*******************************************************
Method: setStartPosition
In: new start angle position
Out: value of start position register
Description: sets a value in start position register.
If no value is provided, method will read position of
magnet.
*******************************************************/
word AMS_5600::setStartPosition(word startAngle)
{
_rawStartAngle = startAngle;
writeOneByte(_zpos_hi, highByte(_rawStartAngle));
delay(2);
writeOneByte(_zpos_lo, lowByte(_rawStartAngle));
delay(2);
_zPosition = readTwoBytes(_zpos_hi);
return (_zPosition);
}
/*******************************************************
Method: getStartPosition
In: none
Out: value of start position register
Description: gets value of start position register.
*******************************************************/
word AMS_5600::getStartPosition()
{
return readTwoBytes(_zpos_hi);
}
/*******************************************************
Method: setEndtPosition
In: new end angle position
Out: value of end position register
Description: sets a value in end position register.
If no value is provided, method will read position of
magnet.
*******************************************************/
word AMS_5600::setEndPosition(word endAngle)
{
_rawEndAngle = endAngle;
writeOneByte(_mpos_hi, highByte(_rawEndAngle));
delay(2);
writeOneByte(_mpos_lo, lowByte(_rawEndAngle));
delay(2);
_mPosition = readTwoBytes(_mpos_hi);
return (_mPosition);
}
/*******************************************************
Method: getEndPosition
In: none
Out: value of end position register
Description: gets value of end position register.
*******************************************************/
word AMS_5600::getEndPosition()
{
word retVal = readTwoBytes(_mpos_hi);
return retVal;
}
/*******************************************************
Method: getRawAngle
In: none
Out: value of raw angle register
Description: gets raw value of magnet position.
start, end, and max angle settings do not apply
*******************************************************/
word AMS_5600::getRawAngle()
{
return readTwoBytes(_raw_ang_hi);
}
/*******************************************************
Method: getScaledAngle
In: none
Out: value of scaled angle register
Description: gets scaled value of magnet position.
start, end, or max angle settings are used to
determine value
*******************************************************/
word AMS_5600::getScaledAngle()
{
return readTwoBytes(_ang_hi);
}
/*******************************************************
Method: detectMagnet
In: none
Out: 1 if magnet is detected, 0 if not
Description: reads status register and examines the
MH bit
*******************************************************/
int AMS_5600::detectMagnet()
{
int magStatus;
int retVal = 0;
/*0 0 MD ML MH 0 0 0*/
/* MD high = magnet detected*/
/* ML high = AGC Maximum overflow, magnet to weak*/
/* MH high = AGC minimum overflow, Magnet to strong*/
magStatus = readOneByte(_stat);
if (magStatus & 0x20)
retVal = 1;
return retVal;
}
/*******************************************************
Method: getMagnetStrength
In: none
Out: 0 if no magnet is detected
1 if magnet is to weak
2 if magnet is just right
3 if magnet is to strong
Description: reads status register andexamins the MH,ML,MD bits
*******************************************************/
int AMS_5600::getMagnetStrength()
{
int magStatus;
int retVal = 0;
/*0 0 MD ML MH 0 0 0*/
/* MD high = magnet detected */
/* ML high = AGC Maximum overflow, magnet to weak*/
/* MH high = AGC minimum overflow, Magnet to strong*/
magStatus = readOneByte(_stat);
if (detectMagnet() == 1)
{
retVal = 2; /*just right */
if (magStatus & 0x10)
retVal = 1; /*to weak */
else if (magStatus & 0x08)
retVal = 3; /*to strong */
}
return retVal;
}
/*******************************************************
Method: get Agc
In: none
Out: value of AGC register
Description: gets value of AGC register.
*******************************************************/
int AMS_5600::getAgc()
{
return readOneByte(_agc);
}
/*******************************************************
Method: getMagnitude
In: none
Out: value of magnitude register
Description: gets value of magnitude register.
*******************************************************/
word AMS_5600::getMagnitude()
{
return readTwoBytes(_mag_hi);
}
/*******************************************************
Method: getConf
In: none
Out: value of CONF register
Description: gets value of CONF register.
*******************************************************/
word AMS_5600::getConf()
{
return readTwoBytes(_conf_hi);
}
/*******************************************************
Method: setConf
In: value of CONF register
Out: none
Description: sets value of CONF register.
*******************************************************/
void AMS_5600::setConf(word _conf)
{
writeOneByte(_conf_hi, highByte(_conf));
delay(2);
writeOneByte(_conf_lo, lowByte(_conf));
delay(2);
}
/*******************************************************
Method: getBurnCount
In: none
Out: value of zmco register
Description: determines how many times chip has been
permanently written to.
*******************************************************/
int AMS_5600::getBurnCount()
{
return readOneByte(_zmco);
}
/*******************************************************
Method: burnAngle
In: none
Out: 1 success
-1 no magnet
-2 burn limit exceeded
-3 start and end positions not set (useless burn)
Description: burns start and end positions to chip.
THIS CAN ONLY BE DONE 3 TIMES
*******************************************************/
int AMS_5600::burnAngle()
{
int retVal = 1;
_zPosition = getStartPosition();
_mPosition = getEndPosition();
_maxAngle = getMaxAngle();
if (detectMagnet() == 1)
{
if (getBurnCount() < 3)
{
if ((_zPosition == 0) && (_mPosition == 0))
retVal = -3;
else
writeOneByte(_burn, 0x80);
}
else
retVal = -2;
}
else
retVal = -1;
return retVal;
}
/*******************************************************
Method: burnMaxAngleAndConfig
In: none
Out: 1 success
-1 burn limit exceeded
-2 max angle is to small, must be at or above 18 degrees
Description: burns max angle and config data to chip.
THIS CAN ONLY BE DONE 1 TIME
*******************************************************/
int AMS_5600::burnMaxAngleAndConfig()
{
int retVal = 1;
_maxAngle = getMaxAngle();
if (getBurnCount() == 0)
{
if (_maxAngle * 0.087 < 18)
retVal = -2;
else
writeOneByte(_burn, 0x40);
}
else
retVal = -1;
return retVal;
}
/*******************************************************
Method: readOneByte
In: register to read
Out: data read from i2c
Description: reads one byte register from i2c
*******************************************************/
int AMS_5600::readOneByte(int in_adr)
{
int retVal = -1;
Wire.beginTransmission(_ams5600_Address);
Wire.write(in_adr);
Wire.endTransmission();
Wire.requestFrom(_ams5600_Address, 1);
while (Wire.available() == 0)
;
retVal = Wire.read();
return retVal;
}
/*******************************************************
Method: readTwoBytes
In: two registers to read
Out: data read from i2c as a word
Description: reads two bytes register from i2c
*******************************************************/
word AMS_5600::readTwoBytes(int in_adr)
{
/* Read 2 Bytes */
Wire.beginTransmission(_ams5600_Address);
Wire.write(in_adr);
Wire.endTransmission();
Wire.requestFrom(_ams5600_Address, 2);
while (Wire.available() < 2);
int high = Wire.read();
int low = Wire.read();
return ( high << 8 ) | low;
}
/*******************************************************
Method: writeOneByte
In: address and data to write
Out: none
Description: writes one byte to a i2c register
*******************************************************/
void AMS_5600::writeOneByte(int adr_in, int dat_in)
{
Wire.beginTransmission(_ams5600_Address);
Wire.write(adr_in);
Wire.write(dat_in);
Wire.endTransmission();
}
/********** END OF AMS 5600 CALSS *****************/