8 #ifndef SENSORS_AS5600_H_
9 #define SENSORS_AS5600_H_
13 #define AS5600ADDRESS 0x36
14 #define ZMCOADDRESS 0x00
15 #define ZPOSADDRESSMSB 0x01
16 #define ZPOSADDRESSLSB 0x02
17 #define MPOSADDRESSMSB 0x03
18 #define MPOSADDRESSLSB 0x04
19 #define MANGADDRESSMSB 0x05
20 #define MANGADDRESSLSB 0x06
21 #define CONFADDRESSMSB 0x07
22 #define CONFADDRESSLSB 0x08
23 #define RAWANGLEADDRESSMSB 0x0C
24 #define RAWANGLEADDRESSLSB 0x0D
25 #define ANGLEADDRESSMSB 0x0E
26 #define ANGLEADDRESSLSB 0x0F
27 #define STATUSADDRESS 0x0B
28 #define AGCADDRESS 0x1A
29 #define MAGNITUDEADDRESSMSB 0x1B
30 #define MAGNITUDEADDRESSLSB 0x1C
31 #define BURNADDRESS 0xFF
35 enum AS5600PowerMode {NOM = 0, LPM1, LPM2, LPM3 };
37 template <u
int8_t pmode = NOM>
46 int _readSingleByte(uint8_t adr) {
47 Wire.beginTransmission(AS5600ADDRESS);
49 Wire.endTransmission();
51 Wire.requestFrom(AS5600ADDRESS, 1);
54 if(Wire.available() <=1) {
61 int _readTwoBytes(uint8_t regMSB, uint8_t regLSB)
66 _msb = _readSingleByte(regMSB);
67 _lsb = _readSingleByte(regLSB);
69 return (_msb << 8) | _lsb;
73 void _writeSingleByte(
int adr_in,
int dat_in)
75 Wire.beginTransmission(AS5600ADDRESS);
78 Wire.endTransmission();
83 As5600 () : _angle(0), _raw(0), _present(
false), _status(0) {}
85 uint8_t getConfigLo() {
86 return _readSingleByte(CONFADDRESSLSB);
89 uint8_t getConfigHi() {
90 return _readSingleByte(CONFADDRESSMSB);
93 void setPowerMode(uint8_t pm){
95 uint8_t config = _readSingleByte(CONFADDRESSLSB);
96 config &= ~((1 << 0) | (1 << 1));
98 _writeSingleByte(CONFADDRESSLSB, config);
100 DPRINTLN(F(
"setPowerMode failed: wrong mode"));
104 void setWatchDog(
bool state) {
105 uint8_t config = _readSingleByte(CONFADDRESSMSB);
107 if (state) config |= 0x20;
108 _writeSingleByte(CONFADDRESSMSB, config);
111 uint8_t getStatus() {
112 return _readSingleByte(STATUSADDRESS) & 0b00111000;
116 return _readSingleByte(AGCADDRESS);
122 uint8_t status = getStatus();
123 uint8_t agc = getAGC();
125 if (status == 0x20) {
129 DPRINT(F(
"AS5600 OK. AGC: "));DDEC(agc);DPRINT(F(
", CONFIG Lo: 0x"));DHEX(getConfigLo());DPRINT(
", Hi: 0x");DHEXLN(getConfigHi());
131 DPRINT(F(
"AS5600 FAILURE. AGC: "));DDEC(agc);DPRINT(F(
", Status: 0x"));DHEXLN(status);
141 _status = getStatus();
142 _present = (_status == 0x20);
145 _raw = _readTwoBytes(RAWANGLEADDRESSMSB, RAWANGLEADDRESSLSB);
146 _angle = (_raw > -1) ? map(_raw, 0, 4096, 0, 359) : 0xFFFF;
150 uint16_t angle () {
return _angle; }
151 int16_t raw () {
return _raw; }
152 uint8_t status() {
return _status ;}