Arduino驱动QMC6308地磁传感器调试笔记(2)
#include <Wire.h> // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Wire.begin(); Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { int num; Wire.beginTransmission(44); //address 0x2ch Wire.write(10); //配置Control Register 1 Wire.write(0xc3); Wire.endTransmission(true); delay(200); Wire.beginTransmission(44); Wire.write(10); Wire.endTransmission(false); Wire.requestFrom(44,1,1); //读取Control Register 1 char conctrl_1reg = Wire.read(); Serial.println(conctrl_1reg,DEC); delay(200); Wire.beginTransmission(44); //address 0x2ch Wire.write(11); //配置Control Register 2 Wire.write(0x00); Wire.endTransmission(true); delay(200); Wire.beginTransmission(44); Wire.write(11); Wire.endTransmission(false); Wire.requestFrom(44,1,1); //读取Control Register 2 char conctrl_2reg = Wire.read(); Serial.println(conctrl_2reg,DEC); while(1) { delay(200); Wire.beginTransmission(44); Wire.write(9); Wire.endTransmission(false); Wire.requestFrom(44,1,1); //读取Status Register 1 DRDY标志 char staflg = Wire.read(); if(staflg & 0x01) //数据准备就绪 { staflg = 0; delay(200); //读取寄存器1-6 Wire.beginTransmission(44); Wire.write(2); Wire.endTransmission(false); Wire.requestFrom(44,1,1); int x_out = Wire.read(); Wire.beginTransmission(44); Wire.write(1); Wire.endTransmission(false); Wire.requestFrom(44,1,1); x_out = (x_out << 8)| (Wire.read()); Wire.beginTransmission(44); Wire.write(4); Wire.endTransmission(false); Wire.requestFrom(44,1,1); int y_out = Wire.read(); Wire.beginTransmission(44); Wire.write(3); Wire.endTransmission(false); Wire.requestFrom(44,1,1); y_out = (y_out << 8)|( Wire.read()); Wire.beginTransmission(44); Wire.write(6); Wire.endTransmission(false); Wire.requestFrom(44,1,1); int z_out = Wire.read(); Wire.beginTransmission(44); Wire.write(5); Wire.endTransmission(false); Wire.requestFrom(44,1,1); z_out = (z_out << 8)|( Wire.read()); //Serial.println("x"); //Serial.print(','); //输出寄存器xyz数据 Serial.print(x_out,DEC); Serial.print(','); //Serial.println("y"); Serial.print(y_out,DEC); Serial.print(','); //Serial.println("z"); Serial.print(z_out,DEC); Serial.println(","); } } }
打开arduino IDE串口绘图窗口可以看到数据输出,并且跟随地磁传感器的方向按一定的规律变化
快去体验下功能实现初步正确带来的欣喜。
项目洽谈合作:18665321219