diff --git a/Arduino/MagAlpha-Library/README.md b/Arduino/MagAlpha-Library/README.md deleted file mode 100644 index b884392..0000000 --- a/Arduino/MagAlpha-Library/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# MagAlpha library -Arduino library for the MPS MagAlpha magnetic angle sensor. - -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. - -For more information on the MagAlpha sensor family: -* [MagAlpha Product Overview](http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview) -* [MagAlpha Support Materials](http://www.monolithicpower.com/Design-Support/Position-Sensors-Design-Support) - -Check [Arduino SPI library reference page](https://www.arduino.cc/en/Reference/SPI) for the SPI signal connections. - -| Warning | -| ------- | -| 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.| - -Written by Mathieu Kaelin for Monolithic Power Systems. -MIT license, all text above must be included in any redistribution - -Place the MagAlpha library folder in your arduinosketchfolder/libraries/ folder. -You may need to create the libraries subfolder if its your first library. Restart the IDE. - -You can also check this tutorial on Arduino library installation: -* [All About Arduino Libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use) diff --git a/Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino b/Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino deleted file mode 100644 index efcd5b9..0000000 --- a/Arduino/MagAlpha-Library/examples/edit-magalpha-config/edit-magalpha-config.ino +++ /dev/null @@ -1,99 +0,0 @@ -#include - -//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections - -#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud) -#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock freqency in Hz -#define SPI_CS_PIN 0 //SPI CS pin - -MagAlpha magAlpha; - -void setup() { - // put your setup code here, to run once: - //Set the SPI SCLK frequency, SPI Mode and CS pin - magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN); - //Set the Serial Communication used to report the angle - Serial.begin(UART_BAUDRATE); -} - -void loop() { - // put your main code here, to run repeatedly: - //======================================================================== - //Read the angle using different methods - //======================================================================== - uint16_t angle; - uint8_t angle8bit; - double angleInDegree; - - Serial.println("Read Angle using differents methods:"); - - //Read the angle (16-bit raw angle value) - angle = magAlpha.readAngle(); - Serial.print(" magAlpha.readAngle() = "); - Serial.println(angle, DEC); - - //Read the angle (16-bit raw angle value), equivalent to magAlpha.readAngle() function - angle = magAlpha.readAngle16(); - Serial.print(" magAlpha.readAngle16() = "); - Serial.println(angle, DEC); - - //Read the angle (8-bit raw angle value) - angle8bit = magAlpha.readAngle8(); - Serial.print(" magAlpha.readAngle8() = "); - Serial.println(angle, DEC); - - //Read the angle in degree (Read 16-bit raw angle value and then convert it to the 0-360 degree range) - angleInDegree = magAlpha.readAngleInDegree(); - Serial.print(" magAlpha.readAngleInDegree() = "); - Serial.println(angleInDegree, 3); - - //======================================================================== - //Read the zero settings in register 0 and 1 - //======================================================================== - uint8_t readbackRegister0Value, readbackRegister1Value; - //Read MagAlpha Gen3 Zero Settings (Registers 0 and 1 - readbackRegister0Value = magAlpha.readRegister(0); - readbackRegister1Value = magAlpha.readRegister(1); - Serial.println("Read Zero Setting:"); - Serial.print(" Read Register[0] = 0x"); - Serial.println(readbackRegister0Value, HEX); - Serial.print(" Read Register[1] = 0x"); - Serial.println(readbackRegister1Value, HEX); - - //======================================================================== - //Write MagAlpha Gen3 Zero Settings with value 0x7FFF (Registers 0 and 1) - //======================================================================== - readbackRegister0Value = magAlpha.writeRegister(0, 0xFF); - readbackRegister1Value = magAlpha.writeRegister(1, 0x7F); - Serial.println("Write Zero Setting:"); - Serial.print(" Write Register[0] = 0x"); - Serial.println(readbackRegister0Value, HEX); - Serial.print(" Write Register[1] = 0x"); - Serial.println(readbackRegister1Value, HEX); - if ((readbackRegister0Value == 0xFF) && (readbackRegister1Value == 0x7F)) - { - Serial.println(" Write Process Succeed"); - } - else - { - Serial.println(" Write Process Fail"); - } - - //======================================================================== - //Change MagAlpha Gen3 Rotation Direction (Register 9, bit 7) - //======================================================================== - uint8_t readbackRegister9Value; - //Read register 9 and toggle RD state - readbackRegister9Value = magAlpha.readRegister(9); - if ((readbackRegister9Value & 0x80) == 0){ - //Set RD to 1 - magAlpha.writeRegister(9, 0x80); - } - else{ - //Set RD to 0 - magAlpha.writeRegister(9, 0x00); - } - Serial.println("Write Rotation Direction Setting:"); - Serial.print(" Write Register[9] = 0x"); - Serial.println(readbackRegister9Value, HEX); -} diff --git a/Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino b/Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino deleted file mode 100644 index f7e15cf..0000000 --- a/Arduino/MagAlpha-Library/examples/read-magalpha-angle/read-magalpha-angle.ino +++ /dev/null @@ -1,27 +0,0 @@ -#include - -//Check https://www.arduino.cc/en/reference/SPI for SPI signals connections - -#define UART_BAUDRATE 115200 //UART data rate in bits per second (baud) -#define SPI_SCLK_FREQUENCY 10000000 //SPI SCLK Clock freqency in Hz -#define SPI_CS_PIN 0 //SPI CS pin - -MagAlpha magAlpha; - -void setup() { - // put your setup code here, to run once: - //Set the SPI SCLK frequency, SPI Mode and CS pin - magAlpha.begin(SPI_SCLK_FREQUENCY, MA_SPI_MODE_3, SPI_CS_PIN); - //Set the Serial Communication used to report the angle - Serial.begin(UART_BAUDRATE); -} - -void loop() { - // put your main code here, to run repeatedly: - uint16_t angle; - //Read the angle (16-bit raw angle value) - angle = magAlpha.readAngle(); - Serial.println(angle, DEC); - //Wait before the next angle measurement (not needed by the sensor, only targeted to make the output easier to read) - delay(25); -} diff --git a/Arduino/MagAlpha-Library/keywords.txt b/Arduino/MagAlpha-Library/keywords.txt deleted file mode 100644 index 8e9f59e..0000000 --- a/Arduino/MagAlpha-Library/keywords.txt +++ /dev/null @@ -1,37 +0,0 @@ -####################################### -# Syntax Coloring Map For MagAlpha-Library -####################################### - -####################################### -# Datatypes (KEYWORD1) -####################################### - -MagAlpha KEYWORD1 - -####################################### -# Methods and Functions (KEYWORD2) -####################################### - -begin KEYWORD2 -end KEYWORD2 -readAngle KEYWORD2 -readAngle16 KEYWORD2 -readAngle8 KEYWORD2 -readAngleInDegree KEYWORD2 -readRegister KEYWORD2 -writeRegister KEYWORD2 -setSpiClockFrequency KEYWORD2 -setSpiDataMode KEYWORD2 -setSpiChipSelectPin KEYWORD2 -convertRawAngleToDegree KEYWORD2 - -####################################### -# Instances (KEYWORD2) -####################################### -magAlpha KEYWORD2 - -####################################### -# Constants (LITERAL1) -####################################### -MA_SPI_MODE_0 LITERAL1 -MA_SPI_MODE_3 LITERAL1 diff --git a/Arduino/MagAlpha-Library/library.properties b/Arduino/MagAlpha-Library/library.properties deleted file mode 100644 index 99e5d0b..0000000 --- a/Arduino/MagAlpha-Library/library.properties +++ /dev/null @@ -1,10 +0,0 @@ -name=MagAlpha library -version=1.0.0 -author=Mathieu Kaelin, Monolithic Power Systems -maintainer=Mathieu Kaelin -sentence=Arduino library for the MPS MagAlpha magnetic angle sensor. -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. -category=Sensors -url=https://github.com/monolithicpower/MagAlpha-Code-Samples -architectures=* -includes=SPI.h diff --git a/Arduino/MagAlpha-Library/src/MagAlpha.cpp b/Arduino/MagAlpha-Library/src/MagAlpha.cpp deleted file mode 100644 index dfeec2d..0000000 --- a/Arduino/MagAlpha-Library/src/MagAlpha.cpp +++ /dev/null @@ -1,113 +0,0 @@ -/*************************************************** - Arduino library for the MPS MagAlpha magnetic angle sensor - 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. - ----> http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview - Written by Mathieu Kaelin for Monolithic Power Systems. - MIT license, all text above must be included in any redistribution -****************************************************/ - -#include "MagAlpha.h" - -//MagAlpha Read/Write Register Command -#define READ_REG_COMMAND (0b010 << 13) -#define WRITE_REG_COMMAND (0b100 << 13) - -MagAlpha::MagAlpha(){ -} - -void MagAlpha::begin(int32_t spiSclkFrequency, uint8_t spiMode, uint8_t spiChipSelectPin){ - setSpiChipSelectPin(spiChipSelectPin); - _speedMaximum = spiSclkFrequency; - _spiMode = spiMode; - SPI.begin(); - SPI.beginTransaction(SPISettings(_speedMaximum, MSBFIRST, _spiMode)); -} - -void MagAlpha::begin(uint8_t spiChipSelectPin){ - setSpiChipSelectPin(spiChipSelectPin); - _speedMaximum = 10000000; - _spiMode = MA_SPI_MODE_3; - SPI.begin(); - SPI.beginTransaction(SPISettings(_speedMaximum, MSBFIRST, _spiMode)); -} - -void MagAlpha::end(){ - SPI.end(); -} - -uint16_t MagAlpha::readAngle(){ - return readAngle16(); -} - -uint16_t MagAlpha::readAngle16(){ - uint16_t angle; - digitalWrite(_spiChipSelectPin, LOW); - angle = SPI.transfer16(0x0000); //Read 16-bit angle - //angle = SPI.transfer(0x00); //Read 8-bit angle - digitalWrite(_spiChipSelectPin, HIGH); - return angle; -} - -uint8_t MagAlpha::readAngle8(){ - uint8_t angle; - digitalWrite(_spiChipSelectPin, LOW); - angle = SPI.transfer(0x00); //Read 8-bit angle - digitalWrite(_spiChipSelectPin, HIGH); - return angle; -} - -double MagAlpha::readAngleInDegree(){ - uint16_t angle; - double angleInDegree; - angle = readAngle16(); - angleInDegree = (angle*360.0)/65536.0; - return angleInDegree; -} - -uint8_t MagAlpha::readRegister(uint8_t address){ - uint8_t readbackRegisterValue; - digitalWrite(_spiChipSelectPin, LOW); - SPI.transfer16(READ_REG_COMMAND | ((address & 0x1F) << 8) | 0x00); - digitalWrite(_spiChipSelectPin, HIGH); - digitalWrite(_spiChipSelectPin, LOW); - readbackRegisterValue = ((SPI.transfer16(0x0000) & 0xFF00) >> 8); - digitalWrite(_spiChipSelectPin, HIGH); - return readbackRegisterValue; -} - -uint8_t MagAlpha::writeRegister(uint8_t address, uint8_t value){ - uint8_t readbackRegisterValue; - digitalWrite(_spiChipSelectPin, LOW); - SPI.transfer16(WRITE_REG_COMMAND | ((address & 0x1F) << 8) | value); - digitalWrite(_spiChipSelectPin, HIGH); - delay(20); //Wait for 20ms - digitalWrite(_spiChipSelectPin, LOW); - readbackRegisterValue = ((SPI.transfer16(0x0000) & 0xFF00) >> 8); - digitalWrite(_spiChipSelectPin, HIGH); - //readbackRegisterValue should be equal to the written value - return readbackRegisterValue; -} - -void MagAlpha::setSpiClockFrequency(uint32_t speedMaximum){ - _speedMaximum = speedMaximum; - SPI.beginTransaction(SPISettings(_speedMaximum, MSBFIRST, _spiMode)); -} - -void MagAlpha::setSpiDataMode(uint8_t spiMode){ - _spiMode = spiMode; - SPI.setDataMode(_spiMode); -} - -void MagAlpha::setSpiChipSelectPin(uint8_t spiChipSelectPin){ - _spiChipSelectPin = spiChipSelectPin; - pinMode(_spiChipSelectPin, OUTPUT); - digitalWrite(_spiChipSelectPin, HIGH); -} - -double MagAlpha::convertRawAngleToDegree(uint8_t rawAngleDataBitLength, uint16_t rawAngle){ - double angleInDegree; - angleInDegree = (rawAngle*360.0)/((double)pow(2, rawAngleDataBitLength)); - return angleInDegree; -} diff --git a/Arduino/MagAlpha-Library/src/MagAlpha.h b/Arduino/MagAlpha-Library/src/MagAlpha.h deleted file mode 100644 index ab45dc5..0000000 --- a/Arduino/MagAlpha-Library/src/MagAlpha.h +++ /dev/null @@ -1,46 +0,0 @@ -/*************************************************** - Arduino library for the MPS MagAlpha magnetic angle sensor - 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. - ----> http://www.monolithicpower.com/Products/Position-Sensors/Products-Overview - Written by Mathieu Kaelin for Monolithic Power Systems. - MIT license, all text above must be included in any redistribution - ****************************************************/ - -#ifndef MAGALPHA_H -#define MAGALPHA_H - -#if (ARDUINO >= 100) - #include "Arduino.h" -#else - #include "WProgram.h" -#endif -#include - - //SPI Mode: MagAlpha Gen3 support SPI mode 3 and 0 [SPI_MODE3, SPI_MODE0] -#define MA_SPI_MODE_0 SPI_MODE0 -#define MA_SPI_MODE_3 SPI_MODE3 - -class MagAlpha { -public: - MagAlpha(); - void begin(int32_t spiSclkFrequency, uint8_t spiMode, uint8_t spiChipSelectPin); - void begin(uint8_t spiChipSelectPin); - void end(); - uint16_t readAngle(); - uint16_t readAngle16(); - uint8_t readAngle8(); - double readAngleInDegree(); - uint8_t readRegister(uint8_t address); - uint8_t writeRegister(uint8_t address, uint8_t value); - void setSpiClockFrequency(uint32_t speedMaximum); - void setSpiDataMode(uint8_t spiMode); - void setSpiChipSelectPin(uint8_t spiChipSelectPin); - double convertRawAngleToDegree(uint8_t rawAngleDataBitLength, uint16_t rawAngle); -private: - uint32_t _speedMaximum; - uint8_t _spiMode; - uint8_t _spiChipSelectPin; -}; -#endif //MAGALPHA_H diff --git a/README.md b/README.md index 55dcb5a..5d2007d 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,18 @@ All code samples are released under the MIT License. # Supported Hardware We currently have code examples for the following hardware platforms: -* [Arduino](https://www.arduino.cc/) (works with the following 3.3V boards) +* [Arduino](https://www.arduino.cc/) (works best with the following 3.3V boards) * [Arduino ZERO](https://www.arduino.cc/en/Main/ArduinoBoardZero) (use SPI connector and USB programming port) - * [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue) (use SPI connector and USB programming port) + * [Arduino MKRZero](https://www.arduino.cc/en/Main/ArduinoBoardMKRZero) (not tested) * [Adafruit Feather M0 Basic Proto](https://www.adafruit.com/products/2772) (should also works with other feather M0 boards) * [Arduino 101](https://www.arduino.cc/en/Main/ArduinoBoard101) (not tested) - * [Arduino MKRZero](https://www.arduino.cc/en/Main/ArduinoBoardMKRZero) (not tested) + * [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue) (use SPI connector and USB programming port) * [Adafruit FT232H Breakout](https://www.adafruit.com/products/2264) (use Python) +| Information | +| ------- | +| Don't forget to check out the [MagAlpha Angle Sensor Arduino Library](https://github.com/monolithicpower/MagAlpha-Arduino-Library)| # About MagAlpha Sensor Familiy MagAlpha sensor family is based on Hall devices that are directly integrated with the signal treatment. These sensors are extremely compact and can instantaneously detects and delivers the angle value in digital format.