Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
MLX90316.cpp - Library to use with Melexis MLX90316 rotary encoder chip. | ||
Created by Martin Nawrath KHM 2010. | ||
Remix by Patrick Bouffel to ajust to Datasheet - 2018-01-27 | ||
Released into the public domain. | ||
*/ | ||
|
||
#if defined(ARDUINO) && ARDUINO >= 100 | ||
#include "Arduino.h" | ||
#else | ||
#include "WProgram.h" | ||
#endif | ||
#include "MLX90316.h" | ||
|
||
|
||
// constructor | ||
MLX90316::MLX90316() { | ||
}; | ||
|
||
// attach | ||
void MLX90316::attach(byte pinSS, byte pinSCK, byte pinMOSI ) { | ||
|
||
_pinSS = pinSS; | ||
_pinSCK = pinSCK; | ||
_pinMOSI = pinMOSI; | ||
|
||
pinMode(_pinSS , OUTPUT); | ||
pinMode(_pinSCK , OUTPUT); | ||
pinMode(_pinMOSI , OUTPUT); | ||
} | ||
|
||
//****************************************************** | ||
int MLX90316::readAngle() { | ||
|
||
byte bb; | ||
long int ret=-1; | ||
unsigned int rr=-1; | ||
unsigned int cc=-1; | ||
|
||
digitalWrite(_pinSS,0); // Select Chip | ||
|
||
_spiWByte(0x55); // send sync byte ( AA reversed order = 55?) | ||
_spiWByte(0xFF); // send sync byte FF) | ||
|
||
rr = (_spiRByte() << 8) + _spiRByte(); // receive 1. byte of mlx data | ||
cc = (_spiRByte() << 8) + _spiRByte(); // receive 1. byte of mlx data (inverted) | ||
|
||
if (rr+cc+1!=0) ret=-1; // check if bit Errors | ||
else { | ||
|
||
if ((rr & 3) == 2) { | ||
if ( rr & (1 << 4)) ret=-2; // Magnetic Field Too Strong | ||
if ( rr & (1 << 5)) ret=-3; // Magnetic Field Too Weak | ||
} | ||
else if ((rr & 3) == 1) { // valid angle data ? | ||
rr = (rr >> 2); | ||
ret = rr ; | ||
ret = ret * 3600 / 16384; // scale output to 360 deg, untit in tens of deg. | ||
} | ||
} | ||
|
||
digitalWrite(_pinSS,1); // Deselect Chip | ||
|
||
return((int)ret); | ||
} | ||
|
||
//************************************************************* | ||
// send and receive SPI byte to/from MLX90316 Chip | ||
void MLX90316::_spiWByte(uint8_t tx) { | ||
|
||
pinMode(_pinMOSI,OUTPUT); // switch MOSI pin to output | ||
|
||
for (int ix = 0; ix < 8; ix++){ // receive/transmit 8 SPI bits | ||
|
||
// write SPI transmit bit to sensor | ||
if ((tx & 1) != 0) digitalWrite(_pinMOSI,1); | ||
else digitalWrite(_pinMOSI,0); | ||
tx= ( tx >> 1); | ||
|
||
digitalWrite(_pinSCK,1); // clocksignal positive slope | ||
digitalWrite(_pinSCK,0); // clocksignal negative slope | ||
} | ||
delayMicroseconds(30); | ||
|
||
pinMode(_pinMOSI,INPUT_PULLUP); // switch MOSI pin to input | ||
} | ||
|
||
//************************************************************* | ||
// receive SPI byte to/from MLX90316 Chip | ||
uint8_t MLX90316::_spiRByte() { | ||
|
||
byte rxb=0; | ||
|
||
for (int ix = 0; ix < 8; ix++){ // receive/transmit 8 SPI bits | ||
|
||
digitalWrite(_pinSCK,1); // clocksignal positive slope | ||
delayMicroseconds(5); | ||
rxb = ( rxb << 1 ); // shift received byte left | ||
if (digitalRead(_pinMOSI) ==1) rxb = rxb | 1; // read respose bit from sensor | ||
digitalWrite(_pinSCK,0); // clocksignal negative slope | ||
} | ||
delayMicroseconds(30); | ||
return(rxb); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
MLX90316.cpp - Library to use with Melexis MLX90316 rotary encoder chip. | ||
Created by Martin Nawrath KHM 2010. | ||
Remix by Patrick Bouffel to ajust to Datasheet - 2018-01-27 | ||
Released into the public domain. | ||
*/ | ||
|
||
#ifndef MLX90316_h | ||
#define MLX90316_h | ||
|
||
class MLX90316 | ||
{ | ||
public: | ||
|
||
MLX90316(); // constructor | ||
void attach(byte pin1, byte pin2, byte pin3); // attach | ||
|
||
int readAngle(); // read sensor angle value | ||
|
||
private: | ||
|
||
byte _pinSCK; | ||
byte _pinSS; | ||
byte _pinMOSI; | ||
|
||
void _spiWByte(uint8_t tx); | ||
uint8_t _spiRByte(); | ||
}; | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,34 @@ | ||
# MLX90316 | ||
Arduino MLX90316 library | ||
|
||
``` | ||
#include <Metro.h> //Include Metro library | ||
#include <MLX90316.h> | ||
byte pinSS = 5; | ||
byte pinSCK = 3; | ||
byte pinMOSI = 4; | ||
int angle; | ||
Metro mlxMetro = Metro(5); | ||
MLX90316 mlx_1 = MLX90316(); | ||
void setup(){ | ||
Serial.begin(115200); | ||
mlx_1.attach(pinSS,pinSCK,pinMOSI); | ||
Serial.println("MLX90316 Rotary Position Sensor"); | ||
} | ||
void loop() { | ||
if (mlxMetro.check() == 1) { | ||
angle = mlx_1.readAngle(); | ||
switch (angle) { | ||
case -1 : Serial.println("Transmission Error"); break; | ||
case -2 : Serial.println("Magnetic Field Too Strong"); break; | ||
case -3 : Serial.println("Magnetic Field Too Weak"); break; | ||
default: Serial.println((float)angle/10); | ||
} | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* MLX90316 Rotary Position Sensor */ | ||
|
||
#include <Metro.h> //Include Metro library | ||
#include <MLX90316.h> | ||
|
||
byte pinSS = 5; | ||
byte pinSCK = 3; | ||
byte pinMOSI = 4; | ||
|
||
int angle; | ||
|
||
Metro mlxMetro = Metro(5); | ||
MLX90316 mlx_1 = MLX90316(); | ||
|
||
void setup(){ | ||
Serial.begin(115200); | ||
mlx_1.attach(pinSS,pinSCK,pinMOSI); | ||
Serial.println("MLX90316 Rotary Position Sensor"); | ||
} | ||
|
||
void loop() { | ||
if (mlxMetro.check() == 1) { | ||
angle = mlx_1.readAngle(); | ||
switch (angle) { | ||
case -1 : Serial.println("Transmission Error"); break; | ||
case -2 : Serial.println("Magnetic Field Too Strong"); break; | ||
case -3 : Serial.println("Magnetic Field Too Weak"); break; | ||
default: Serial.println((float)angle/10); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
####################################### | ||
# Syntax Coloring Map For Test | ||
####################################### | ||
|
||
####################################### | ||
# Datatypes (KEYWORD1) | ||
####################################### | ||
|
||
MLX90316 KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
|
||
attach KEYWORD2 | ||
readAngle KEYWORD2 | ||
|
||
####################################### | ||
# Instances (KEYWORD2) | ||
####################################### | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
|