-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAS5045.cpp
62 lines (53 loc) · 1.25 KB
/
AS5045.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
#include "Arduino.h"
#include "AS5045.h"
AS5045::AS5045(uint16_t DataPin, uint16_t ClockPin, uint16_t ChipSelectPin)
: _data(DataPin), _clock(ClockPin), _cs(ChipSelectPin)
{
pinMode(_data, INPUT);
pinMode(_clock, OUTPUT);
pinMode(_cs, OUTPUT);
}
uint32_t AS5045::encoder_degrees(void)
{
return ((encoder_value() * 360)/4096);
}
uint32_t AS5045::encoder_value(void)
{
return (read_chip() >> 6);
}
uint32_t AS5045::encoder_error(void)
{
uint16_t error_code;
uint32_t raw_value;
raw_value = read_chip();
error_code = raw_value & 0b000000000000111111;
err_value.DECn = error_code & 2;
err_value.INCn = error_code & 4;
err_value.LIN = error_code & 8;
err_value.COF = error_code & 16;
err_value.OCF = error_code & 32;
return error_code;
}
uint32_t AS5045::read_chip(void)
{
uint32_t raw_value = 0;
uint16_t inputstream = 0;
uint16_t c;
digitalWrite(_cs, HIGH);
digitalWrite(_clock, HIGH);
delay(100);
digitalWrite(_cs, LOW);
delay(10);
digitalWrite(_clock, LOW);
delay(10);
for (c = 0; c < 18; c++)
{
digitalWrite(_clock, HIGH);
delay(10);
inputstream = digitalRead(_data);
raw_value = ((raw_value << 1) + inputstream);
digitalWrite(_clock, LOW);
delay(10);
}
return raw_value;
}