为与Arduino兼容的PNI RM3100创建驱动程序

时间:2018-05-23 21:14:44

标签: arduino

我目前正在开发RM3100磁力计的驱动程序,但我遇到了读取和写入寄存器的问题。目前,我正在尝试读取STATUS寄存器以确定传感器是否正在收集数据;但是,我每次都得零。我试图访问寄存器的方式有问题吗?或者我的代码中有错误吗?

C ++文件:

#include "Arduino.h"
#include "QRM3100.h"
#include <Wire.h>
#include <math.h>

#define CALIBRATION_TIMEOUT 5000 //timeout in milliseconds
#define DEG_PER_RAD (180.0/3.14159265358979)

QRM3100::QRM3100() {
    //Just some random initial values
}

void QRM3100::initialize() {
    Wire.begin();

}

uint8_t QRM3100::readRegister(uint8_t address){
  uint8_t output;

  Wire.beginTransmission(QRM3100_I2C_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();

  delayMicroseconds(20);

  Wire.requestFrom(QRM3100_I2C_ADDRESS, 1);
  while(Wire.available())
  {
    output = Wire.read();
  }

  return output;
}

void QRM3100::writeRegister(uint8_t address, uint8_t value){
  Wire.beginTransmission(QRM3100_I2C_ADDRESS);
  Wire.write(address);
  Wire.write(value);
  Wire.endTransmission();
}

bool QRM3100::dataReady() {
    return ((readRegister(QRM3100_STATUS_REG) & 0x80) >> 7);
}


void QRM3100::start() {
    exitStandby();
}


void QRM3100::exitStandby(){
    writeRegister(QRM3100_CMM, 0b011100001);
}

标题文件:

#ifndef QRM3100_h
#define QRM3100_h

#include "Arduino.h"
#include "Wire.h"

#define QRM3100_CMM         0x01 //initiates continuous measurement mode
#define QRM3100_STATUS_REG      0x34 //status of DRDY
#define QRM3100_I2C_ADDRESS     0x21
#define QRM3100_POLL        0x00 //poll

class QRM3100
{
    public:
    QRM3100();
    void initialize();

    uint8_t readRegister(uint8_t address);
    void writeRegister(uint8_t address, uint8_t value);
    bool dataReady();


    void start();
    void exitStandby();

    bool error;
};

#endif

Arduino文件:

#include <C:\Users\daehy\OneDrive\Documents\Arduino\libraries\src\QRM3100X.h>
#include <C:\Users\daehy\OneDrive\Documents\Arduino\libraries\src\QRM3100X.cpp>

QRM3100 mag = QRM3100();

void setup() {
  Serial.begin(9600);
  mag.initialize();
  mag.start();
}

void loop() {
 Serial.println( mag.dataReady());
}

1 个答案:

答案 0 :(得分:0)

您好像没有先设置CMM寄存器。连续模式的默认值可能不正确。尝试将CMM寄存器(0x01)设置为以下内容:

(CMM_ALL_AXIS_ON | DRDY_WHEN_ALL_AXIS_MEASURED | CM_START)

(0x70 | 0x08 | 0x01)
这应该给你一个最终的二进制值,

| LDM | CMZ | CMY | CMX | DRDM1 | DRDM0 | ALARM | START |

| 0 | 1 | 1 | 1 | 1 | 0 | 0 | 1 |

或十六进制中的0x79