MMC5603NJ library created.

This commit is contained in:
2024-01-07 22:33:59 +00:00
parent 695e25eef3
commit ee1f5c7d5e
3 changed files with 235 additions and 2 deletions

133
src/MMC5603NJ.cpp Normal file
View File

@@ -0,0 +1,133 @@
#include <MMC5603NJ.h>
bool MMC5603NJ::begin(uint8_t addr, TwoWire *theWire) {
_wire = theWire;
_addr = addr;
uint8_t whoAmI = whoami();
SerialUSB.println(whoAmI);
if (whoAmI == MMC5603NJ_WHO_AM_I) {
return true;
} else {
return false;
}
}
uint8_t MMC5603NJ::whoami() {
uint8_t buffer[1];
if (readRegister(MMC5603NJ_REG_ID, buffer)) {
return buffer[0];
}
return -1;
}
uint8_t MMC5603NJ::getRawTemp(){
uint8_t out;
//set temp trigger
uint8_t conf = MMC5603NJ_REG_CONF0_MEAST;
if(!write(MMC5603NJ_REG_CONF0,&conf,1)){
return 127;
}
//check read done
uint8_t status;
while(!(status >> MMC5603NJ_REG_STATUS_MEAST)){
readRegister(MMC5603NJ_REG_STATUS1,&status);
delay(5);
}
//read temp
if(readRegister(MMC5603NJ_REG_TOUT, &out)){
return out;
}
return 127;
}
float MMC5603NJ::getTemp(){
return (float)getRawTemp() * 0.8 - 75;
}
sensorXYZ MMC5603NJ::getRawMag(){
//...
uint8_t rbuf[9];
sensorXYZ raw = {-9999,-9999,-9999};
//set temp trigger
uint8_t conf = MMC5603NJ_REG_CONF0_MEASM;
if(!write(MMC5603NJ_REG_CONF0,&conf,1)){
return raw;
}
//check read done
uint8_t status = 0;
uint8_t i = 0;
while(!(status >> MMC5603NJ_REG_STATUS_MEASM)){
readRegister(MMC5603NJ_REG_STATUS1,&status);
if(i++>5){
return raw;
}
delay(5);
}
//read Mag once
if(readRegister(MMC5603NJ_REG_XOUT0, rbuf, 9)){
//
raw.x = (uint32_t)rbuf[0] << 12 | (uint32_t)rbuf[1] << 4 | rbuf[6] >> 4;
raw.y = (uint32_t)rbuf[2] << 12 | (uint32_t)rbuf[3] << 4 | rbuf[7] >> 4;
raw.z = (uint32_t)rbuf[4] << 12 | (uint32_t)rbuf[5] << 4 | rbuf[8] >> 4;
raw.x -= (uint32_t)1 << 19;
raw.y -= (uint32_t)1 << 19;
raw.z -= (uint32_t)1 << 19;
}
return raw;
}
sensorFloatXYZ MMC5603NJ::getMag(){
sensorXYZ raw = {0,0,0};
sensorFloatXYZ out = {0,0,0};
raw = getRawMag();
out.x = (float)raw.x * 0.00625;
out.y = (float)raw.y * 0.00625;
out.z = (float)raw.z * 0.00625;
return out;
}
bool MMC5603NJ::write(uint8_t reg, uint8_t *buffer, uint8_t len) {
_wire->beginTransmission(_addr);
_wire->write(reg);
for(uint8_t i = 0; i < len; i++) {
_wire->write(buffer[i]);
}
if (_wire->endTransmission() != 0) {
return false;
}
return true;
}
bool MMC5603NJ::readRegister(uint8_t reg, uint8_t *buffer, uint8_t len) {
uint8_t rx_bytes = 0;
_wire->beginTransmission(_addr);
_wire->write(reg);
uint8_t err = _wire->endTransmission();
if (err!= 0) {
SerialUSB.println(err);
return false;
}
rx_bytes = _wire->requestFrom(_addr, len);
if (rx_bytes != len) {
SerialUSB.println("No data");
return false;
}
for (uint8_t i = 0; i < len; i++) {
buffer[i] = _wire->read();
}
return true;
}

87
src/MMC5603NJ.h Normal file
View File

@@ -0,0 +1,87 @@
/*
eMAKER MMC5603NJ driver
Copyright (C) 2023-2024 jmgiacalone for eMAKER Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MMC5603NJ_H
#define MMC5603NJ_H
#include <Arduino.h>
#include <Wire.h>
// I2C Address
#define MMC5603NJ_DEFAULT_ADDRESS (0x30)
#define MMC5603NJ_WHO_AM_I 16
#define MMC5603NJ_REG_ID (0x39)
//Registers
//Config
#define MMC5603NJ_REG_ODR (0x1A)
#define MMC5603NJ_REG_CONF0 (0x1B)
#define MMC5603NJ_REG_CONF1 (0x1C)
#define MMC5603NJ_REG_CONF2 (0x1D)
#define MMC5603NJ_REG_CONF0_MEASM 0b00000001
#define MMC5603NJ_REG_CONF0_MEAST 0b00000010
#define MMC5603NJ_REG_STATUS_MEASM 6
#define MMC5603NJ_REG_STATUS_MEAST 7
//Data
#define MMC5603NJ_REG_XOUT0 (0x00)
#define MMC5603NJ_REG_XOUT1 (0x01)
#define MMC5603NJ_REG_XOUT2 (0x06)
#define MMC5603NJ_REG_YOUT0 (0x02)
#define MMC5603NJ_REG_YOUT1 (0x03)
#define MMC5603NJ_REG_YOUT2 (0x07)
#define MMC5603NJ_REG_ZOUT0 (0x04)
#define MMC5603NJ_REG_ZOUT1 (0x05)
#define MMC5603NJ_REG_ZOUT2 (0x08)
#define MMC5603NJ_REG_TOUT (0x09)
#define MMC5603NJ_REG_STATUS1 (0x18)
typedef struct {
int32_t x;
int32_t y;
int32_t z;
} sensorXYZ;
typedef struct {
float x;
float y;
float z;
} sensorFloatXYZ;
// Class
class MMC5603NJ {
public:
bool begin(uint8_t addr = MMC5603NJ_DEFAULT_ADDRESS, TwoWire *theWire = &Wire);
uint8_t whoami();
sensorXYZ getRawMag();
sensorFloatXYZ getMag();
uint8_t getRawTemp();
float getTemp();
private:
TwoWire *_wire;
uint8_t _addr;
bool write(uint8_t reg, uint8_t *wbuf, uint8_t len);
bool readRegister(uint8_t reg, uint8_t *rbuf, uint8_t len = 1);
};
#endif

View File

@@ -1,5 +1,8 @@
#include <Arduino.h>
#include <Wire.h>
#include <MMC5603NJ.h>
MMC5603NJ mag;
void portScan();
void magwhoami();
@@ -9,12 +12,22 @@ void setup() {
Wire.begin();
delay(2000);
portScan();
magwhoami();
mag.begin();
// magwhoami();
}
void loop() {
SerialUSB.println(mag.getTemp());
sensorFloatXYZ m = mag.getMag();
SerialUSB.print("X:");
SerialUSB.print(m.x);
SerialUSB.print("uT, Y:");
SerialUSB.print(m.y);
SerialUSB.print("uT, Z:");
SerialUSB.print(m.z);
SerialUSB.print("uT.\n");
delay(1000);
}
void portScan(){