-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibas.cpp
More file actions
executable file
·95 lines (86 loc) · 2.16 KB
/
Copy pathlibas.cpp
File metadata and controls
executable file
·95 lines (86 loc) · 2.16 KB
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
/*
libas.cpp - Arduino library for Austria Microsystems AS5145 and AS5045
Copyright (c) 2011 Oliver Levett. All right reserved.
*/
// include core Wiring API
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// include this library's description file
#include "libas.h"
// Default constructor, SCLK connects to CLK, CSn to ChipSelect and DI to DO on AS5X45
libas::libas(int CLK, int DI, int CS)
{
// Set pins up for output/input
clkPin = CLK;
dInPin = DI;
chipSelectPin = CS;
pinMode(CLK, OUTPUT);
pinMode(DI, INPUT);
pinMode(CS, OUTPUT);
// DataPrecision is 12bit
DataPrecision = 12;
}
// Read position off sensor and return flags (wraps ASGetPosition for code size)
ASDataFlags libas::GetDataFlags(void)
{
ASGetPosition();
return Flags;
}
// Read position off sensor, and update
int libas::GetPosition(void)
{
// Temp position
int tempPosition = 0;
// Counter
int i = 0;
// XOR for parity check (Even Parity)
byte XOR = 0;
// Temp reading byte
byte tempRead = 0;
// Select sensor
digitalWrite(chipSelectPin, LOW);
// Sensor feeds out position MSB first
for(i = DataPrecision-1; i >= 0; i--)
{
// Toggle clock line, wait, and then read the data
digitalWrite(clkPin,LOW);
// Delays must be at least 500ns
delayMicroseconds(1);
digitalWrite(clkPin,HIGH);
delayMicroseconds(1);
tempRead = digitalRead(dInPin)&0x01;
// XOR data for checksum
XOR ^= tempRead;
// Read to tempPosition so we can buffer if data is invalid
tempPosition |= (tempRead)<<i;
}
// Status bits are fed out
for(i = 0; i < 6; i++)
{
// Toggle clock line, wait, and then read the data
digitalWrite(clkPin,LOW);
delayMicroseconds(1);
digitalWrite(clkPin,HIGH);
delayMicroseconds(1);
tempRead = digitalRead(dInPin)&0x01;
// XOR data for checksum
XOR ^= tempRead;
// We don't buffer Flags, so user can check for errors
Flags.data |= (tempRead)<<i;
}
// Disable chipselect
digitalWrite(chipSelectPin, HIGH);
// ValidityCheck
if(XOR == 0
&& Flags.OCF == 1
&& Flags.COF == 0
&& Flags.LIN == 0)
{
// if valid, return flags and update position
Position=tempPosition;
}
return Position;
}