Skip to content

Commit e7237cc

Browse files
committed
Initial commit:
- Add MagAlpha Class for SPI communication - Add MagAlphaSsi Class for SSI communication (use Arduino SPI interface to emulate the SSI interface) - Add various examples (SPI and SSI, Read angle, read/write registers, check parity bit)
1 parent d169d9f commit e7237cc

File tree

11 files changed

+638
-1
lines changed

11 files changed

+638
-1
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 MPS Tech Switzerland LLC
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
# MagAlpha-Arduino-Library
1+
# MagAlpha library
2+
Arduino library for the MPS MagAlpha magnetic angle sensor.
3+
4+
Supports MagAlpha 3rd generation Sensors. MagAlpha sensor detects the absolute angular position of a permanent magnet, typically a diametrically magnetized cylinder on the rotating shaft.
5+
6+
For more information on the MagAlpha sensor family:
7+
* [MagAlpha Product Overview](http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview)
8+
* [MagAlpha Support Materials](http://www.monolithicpower.com/Design-Support/Position-Sensors-Design-Support)
9+
10+
Check [Arduino SPI library reference page](https://www.arduino.cc/en/Reference/SPI) for the SPI signal connections.
11+
12+
| Warning |
13+
| ------- |
14+
| Unlike most Arduino & Genuino boards, the MagAlpha runs at 3.3V. Even if the I/O can tolerate 5V, check that the voltage applied to VDD is at 3.3V. Applying a voltages higher than 3.3V to the VDD pin could damage the sensor.|
15+
16+
Written by Mathieu Kaelin for Monolithic Power Systems.
17+
MIT license, all text above must be included in any redistribution
18+
19+
Place the MagAlpha library folder in your arduinosketchfolder/libraries/ folder.
20+
You may need to create the libraries subfolder if its your first library. Restart the IDE.
21+
22+
You can also check this tutorial on Arduino library installation:
23+
* [All About Arduino Libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <MagAlpha.h>
2+
3+
//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections
4+
5+
#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud)
6+
#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock frequency in Hz
7+
#define SPI_CS_PIN 0 //SPI CS pin
8+
9+
MagAlpha magAlpha;
10+
11+
void setup() {
12+
// put your setup code here, to run once:
13+
//Set the SPI SCLK frequency, SPI Mode and CS pin
14+
magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN);
15+
//Set the Serial Communication used to report the angle
16+
Serial.begin(UART_BAUDRATE);
17+
}
18+
19+
void loop() {
20+
// put your main code here, to run repeatedly:
21+
//========================================================================
22+
//Read the angle using different methods
23+
//========================================================================
24+
double angle;
25+
uint16_t angleRaw16;
26+
uint8_t angleRaw8;
27+
28+
Serial.println("Read Angle using differents methods:");
29+
30+
//Read the angle in degree (Read 16-bit raw angle value and then convert it to the 0-360 degree range)
31+
angle = magAlpha.readAngle();
32+
Serial.print(" magAlpha.readAngle() = ");
33+
Serial.println(angle, 3);
34+
35+
//Read the angle (16-bit raw angle value)
36+
angleRaw16 = magAlpha.readAngleRaw();
37+
Serial.print(" magAlpha.readAngleRaw() = ");
38+
Serial.println(angleRaw16, DEC);
39+
40+
//Read the angle (16-bit raw angle value), equivalent to magAlpha.readAngleRaw() function
41+
angleRaw16 = magAlpha.readAngleRaw16();
42+
Serial.print(" magAlpha.readAngleRaw16() = ");
43+
Serial.println(angleRaw16, DEC);
44+
45+
//Read the angle (8-bit raw angle value)
46+
angleRaw8 = magAlpha.readAngleRaw8();
47+
Serial.print(" magAlpha.readAngleRaw8() = ");
48+
Serial.println(angleRaw8, DEC);
49+
50+
//Read the angle (16-bit raw angle value) and check the parity bit to detect possible communication error
51+
bool error;
52+
angleRaw16 = magAlpha.readAngleRaw16(&error);
53+
Serial.print(" magAlpha.readAngleRaw16(&error) = ");
54+
Serial.print(angleRaw16, DEC);
55+
if (error == true){
56+
Serial.print("\t => Communication error detected");
57+
}
58+
else{
59+
Serial.print("\t => Operation Succeed");
60+
}
61+
Serial.println();
62+
63+
64+
65+
//========================================================================
66+
//Read the zero settings in register 0 and 1
67+
//========================================================================
68+
uint8_t readbackRegister0Value, readbackRegister1Value;
69+
//Read MagAlpha Gen3 Zero Settings (Registers 0 and 1
70+
readbackRegister0Value = magAlpha.readRegister(0);
71+
readbackRegister1Value = magAlpha.readRegister(1);
72+
Serial.println("Read Zero Setting:");
73+
Serial.print(" Read Register[0] = 0x");
74+
Serial.println(readbackRegister0Value, HEX);
75+
Serial.print(" Read Register[1] = 0x");
76+
Serial.println(readbackRegister1Value, HEX);
77+
78+
//========================================================================
79+
//Write MagAlpha Gen3 Zero Settings with value 0x7FFF (Registers 0 and 1)
80+
//========================================================================
81+
readbackRegister0Value = magAlpha.writeRegister(0, 0xFF);
82+
readbackRegister1Value = magAlpha.writeRegister(1, 0x7F);
83+
Serial.println("Write Zero Setting:");
84+
Serial.print(" Write Register[0] = 0x");
85+
Serial.println(readbackRegister0Value, HEX);
86+
Serial.print(" Write Register[1] = 0x");
87+
Serial.println(readbackRegister1Value, HEX);
88+
if ((readbackRegister0Value == 0xFF) && (readbackRegister1Value == 0x7F))
89+
{
90+
Serial.println(" Write Process Succeed");
91+
}
92+
else
93+
{
94+
Serial.println(" Write Process Fail");
95+
}
96+
97+
//========================================================================
98+
//Change MagAlpha Gen3 Rotation Direction (Register 9, bit 7)
99+
//========================================================================
100+
uint8_t readbackRegister9Value;
101+
//Read register 9 and toggle RD state
102+
readbackRegister9Value = magAlpha.readRegister(9);
103+
if ((readbackRegister9Value & 0x80) == 0){
104+
//Set RD to 1
105+
magAlpha.writeRegister(9, 0x80);
106+
}
107+
else{
108+
//Set RD to 0
109+
magAlpha.writeRegister(9, 0x00);
110+
}
111+
Serial.println("Write Rotation Direction Setting:");
112+
Serial.print(" Write Register[9] = 0x");
113+
Serial.println(readbackRegister9Value, HEX);
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <MagAlpha.h>
2+
3+
//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections
4+
5+
#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud)
6+
#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock frequency in Hz
7+
#define SPI_CS_PIN 0 //SPI CS pin
8+
9+
MagAlpha magAlpha;
10+
11+
void setup() {
12+
// put your setup code here, to run once:
13+
//Set the SPI SCLK frequency, SPI Mode and CS pin
14+
magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN);
15+
//Set the Serial Communication used to report the angle
16+
Serial.begin(UART_BAUDRATE);
17+
}
18+
19+
void loop() {
20+
// put your main code here, to run repeatedly:
21+
uint16_t angleRaw16;
22+
bool error;
23+
//Read the angle
24+
angleRaw16 = magAlpha.readAngleRaw(&error);
25+
Serial.print(angleRaw16, DEC);
26+
if (error){
27+
Serial.print("\t => Communication Error Detected");
28+
}
29+
else{
30+
Serial.print("\t => Communication Succeeded");
31+
}
32+
Serial.println();
33+
//Wait before the next angle measurement (not needed by the sensor, only targeted to make the output easier to read)
34+
delay(25);
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <MagAlpha.h>
2+
3+
//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections
4+
//Connect SSI SSCK to SPI SCLK signal
5+
//Connect SSI SSD to SPI MISO signal
6+
7+
#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud)
8+
#define SSI_SSCK_FREQUENCY 1000000 //SSI SSCK Clock frequency in Hz
9+
10+
MagAlphaSSI magAlphaSsi;
11+
12+
void setup() {
13+
// put your setup code here, to run once:
14+
//Set the SSI SSCK frequency
15+
magAlphaSsi.begin(SSI_SSCK_FREQUENCY);
16+
//Set the Serial Communication used to report the angle
17+
Serial.begin(UART_BAUDRATE);
18+
}
19+
20+
void loop() {
21+
// put your main code here, to run repeatedly:
22+
uint16_t angleRaw16;
23+
bool error;
24+
//Read the angle
25+
angleRaw16 = magAlphaSsi.readAngleRaw(&error);
26+
Serial.print(angleRaw16, DEC);
27+
if (error){
28+
Serial.print("\t => Communication Error Detected");
29+
}
30+
else{
31+
Serial.print("\t => Communication Succeeded");
32+
}
33+
Serial.println();
34+
//Wait before the next angle measurement (not needed by the sensor, only targeted to make the output easier to read)
35+
delay(25);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <MagAlpha.h>
2+
3+
//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections
4+
//Connect SSI SSCK to SPI SCLK signal
5+
//Connect SSI SSD to SPI MISO signal
6+
7+
#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud)
8+
#define SSI_SSCK_FREQUENCY 1000000 //SSI SSCK Clock frequency in Hz
9+
10+
MagAlphaSSI magAlphaSsi;
11+
12+
void setup() {
13+
// put your setup code here, to run once:
14+
//Set the SSI SSCK frequency
15+
magAlphaSsi.begin(SSI_SSCK_FREQUENCY);
16+
//Set the Serial Communication used to report the angle
17+
Serial.begin(UART_BAUDRATE);
18+
}
19+
20+
void loop() {
21+
// put your main code here, to run repeatedly:
22+
double angle;
23+
//Read the angle
24+
angle = magAlphaSsi.readAngle();
25+
Serial.println(angle, 3);
26+
//Wait before the next angle measurement (not needed by the sensor, only targeted to make the output easier to read)
27+
delay(25);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <MagAlpha.h>
2+
3+
//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections
4+
5+
#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud)
6+
#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock frequency in Hz
7+
#define SPI_CS_PIN 0 //SPI CS pin
8+
9+
MagAlpha magAlpha;
10+
11+
void setup() {
12+
// put your setup code here, to run once:
13+
//Set the SPI SCLK frequency, SPI Mode and CS pin
14+
magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN);
15+
//Set the Serial Communication used to report the angle
16+
Serial.begin(UART_BAUDRATE);
17+
}
18+
19+
void loop() {
20+
// put your main code here, to run repeatedly:
21+
double angle;
22+
//Read the angle
23+
angle = magAlpha.readAngle();
24+
Serial.println(angle, 3);
25+
//Wait before the next angle measurement (not needed by the sensor, only targeted to make the output easier to read)
26+
delay(25);
27+
}

keywords.txt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#######################################
2+
# Syntax Coloring Map For MagAlpha-Library
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
MagAlpha KEYWORD1
10+
MagAlphaSSI KEYWORD1
11+
12+
#######################################
13+
# Methods and Functions (KEYWORD2)
14+
#######################################
15+
16+
begin KEYWORD2
17+
end KEYWORD2
18+
readAngle KEYWORD2
19+
readAngleRaw KEYWORD2
20+
readAngleRaw16 KEYWORD2
21+
readAngleRaw8 KEYWORD2
22+
readRegister KEYWORD2
23+
writeRegister KEYWORD2
24+
setSpiClockFrequency KEYWORD2
25+
setSpiDataMode KEYWORD2
26+
setSpiChipSelectPin KEYWORD2
27+
convertRawAngleToDegree KEYWORD2
28+
setSsiClockFrequency KEYWORD2
29+
30+
#######################################
31+
# Instances (KEYWORD2)
32+
#######################################
33+
magAlpha KEYWORD2
34+
magAlphaSsi KEYWORD2
35+
36+
#######################################
37+
# Constants (LITERAL1)
38+
#######################################
39+
MA_SPI_MODE_0 LITERAL1
40+
MA_SPI_MODE_3 LITERAL1

library.properties

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=MagAlpha Angle Sensor Library
2+
version=1.0.0
3+
author=Mathieu Kaelin, Monolithic Power Systems <sensors@monolithicpower.com>
4+
maintainer=Mathieu Kaelin <sensors@monolithicpower.com>
5+
sentence=Arduino library for the MPS MagAlpha magnetic angle sensor.
6+
paragraph=Supports MagAlpha 3rd generation Sensors. MagAlpha sensor detects the absolute angular position of a permanent magnet, typically a diametrically magnetized cylinder on the rotating shaft.
7+
category=Sensors
8+
url=https://github.com/monolithicpower/MagAlpha-Arduino-Library
9+
architectures=*
10+
includes=SPI.h

0 commit comments

Comments
 (0)