Last active
August 5, 2022 14:13
-
-
Save Otteri/86da94b676e55acff900af58b90d25fc to your computer and use it in GitHub Desktop.
Minimalistic MLX90363 interfacing example with Adruino board
This file contains hidden or 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
// Arduino port for MLX90363 Example rotary program | |
// Purpose: Demonstrate getting angular data from | |
// MLX90363 via GET1a message | |
// | |
// Adapts code example given on: | |
// MLX90363 Getting Started Guide (Rev 002) | |
// Tested with Arduino Nano V3 and IDE ver. 1.8.5 | |
// Date: 09.03.2019 | |
#include <stdio.h> | |
#include <SPI.h> | |
// Set correct pin numbers: | |
char pin_MOSI = 11; // Set Master Out, Slave In (MOSI) | |
char pin_MISO = 12; // Set Master In, Slave Out (MISO) | |
char pin_SLCK = 8; // Set Clock Pin (SLCK) | |
char pin_SS1 = 10; // Set Slave Select (SS) | |
char u8_spi_read_buffer[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; | |
char u8_spi_write_buffer[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; | |
float f32_angle_degrees = 0.0; | |
unsigned int u16_angle_lsb = 0; | |
char u8_error_lsb = 0; | |
char u8_rollcnt_dec = 0; | |
char u8_virtualgain_dec = 0; | |
char u8_crc_dec = 0; | |
// 360 degrees is mapped to 14 bits = 360/2^14 = 0.02197 | |
const float f32_lsb_to_dec_degrees = 0.02197; | |
// SPI settings for communicating with the MLX90363 | |
SPISettings settingsA(500000, MSBFIRST, SPI_MODE1); | |
void setup() | |
{ | |
pinMode (pin_MOSI, OUTPUT); | |
pinMode (pin_MISO, INPUT); | |
pinMode (pin_SLCK, OUTPUT); | |
pinMode (pin_SS1, OUTPUT); | |
digitalWrite(pin_SS1, HIGH); // Turn off the SS | |
// Begin serial Comm. Set the baudrate to 9600 | |
Serial.begin(9600); | |
// Init SPI | |
SPI.begin(); | |
} | |
void loop() { | |
// Issue GET1 message | |
u8_spi_write_buffer[0] = 0x00; | |
u8_spi_write_buffer[1] = 0x00; | |
u8_spi_write_buffer[2] = 0xFF; | |
u8_spi_write_buffer[3] = 0xFF; | |
u8_spi_write_buffer[4] = 0x00; | |
u8_spi_write_buffer[5] = 0x00; | |
u8_spi_write_buffer[6] = 0x13; | |
u8_spi_write_buffer[7] = 0xEA; | |
SPI.beginTransaction(settingsA); | |
digitalWrite(pin_SS1, LOW); | |
for (int i = 0; i < 8; i++) { | |
u8_spi_read_buffer[i] = SPI.transfer(u8_spi_write_buffer[i]); | |
} | |
digitalWrite(pin_SS1, HIGH); // Deselect | |
delay(1); | |
SPI.endTransaction(); | |
// Issue NOP message | |
u8_spi_write_buffer[0] = 0x00; | |
u8_spi_write_buffer[1] = 0x00; | |
u8_spi_write_buffer[2] = 0xAA; | |
u8_spi_write_buffer[3] = 0xAA; | |
u8_spi_write_buffer[4] = 0x00; | |
u8_spi_write_buffer[5] = 0x00; | |
u8_spi_write_buffer[6] = 0xD0; | |
u8_spi_write_buffer[7] = 0xAB; | |
SPI.beginTransaction(settingsA); | |
digitalWrite(pin_SS1, LOW); | |
for (int i = 0; i < 8; i++) { | |
u8_spi_read_buffer[i] = SPI.transfer(u8_spi_write_buffer[i]); | |
} | |
digitalWrite(pin_SS1, HIGH); // Deselect | |
delay(1); | |
SPI.endTransaction(); | |
// Extract and convert the angle to degrees | |
// remove error bits and shift to high byte | |
u16_angle_lsb = (u8_spi_read_buffer[1] & 0x3F) << 8; | |
// Add LSB of angle | |
u16_angle_lsb = u16_angle_lsb + u8_spi_read_buffer[0]; | |
// Convert to decimal degrees | |
f32_angle_degrees = u16_angle_lsb * f32_lsb_to_dec_degrees; | |
// Extract data | |
u8_error_lsb = u8_spi_read_buffer[1] >> 6; | |
u8_crc_dec = u8_spi_read_buffer[7]; | |
u8_virtualgain_dec = u8_spi_read_buffer[4]; | |
u8_rollcnt_dec = u8_spi_read_buffer[6] & 0x3F; | |
// Send retults to serial port | |
char output[30]; | |
sprintf(output, "Angle (Lsb): %d", u16_angle_lsb); Serial.println(output); | |
Serial.println( "Angle (Dec): " + String(f32_angle_degrees)); // print float | |
sprintf(output, "Error Bits (Dec): %d", u8_error_lsb); Serial.println(output); | |
sprintf(output, "CRC (Dec): %d", u8_crc_dec); Serial.println(output); | |
sprintf(output, "Virtual Gain (Dec): %d", u8_virtualgain_dec); Serial.println(output); | |
sprintf(output, "Rolling Counter (Dec): %d", u8_rollcnt_dec); Serial.println(output); | |
Serial.println("--------------------------------------------------------------"); | |
delay(500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment