2017-09-13 02:58:18 -04:00
|
|
|
//
|
|
|
|
// FILE: hmc6352Config.ino
|
2013-09-30 11:03:06 -04:00
|
|
|
// AUTHOR: Rob Tillaart
|
2017-09-13 02:58:18 -04:00
|
|
|
// VERSION: 0.1.1
|
2013-09-30 11:03:06 -04:00
|
|
|
// PURPOSE: test app HMC6352 library for Arduino
|
|
|
|
//
|
2017-09-13 02:58:18 -04:00
|
|
|
// HISTORY:
|
|
|
|
// 0.1.0 - 2011-04-12 initial version
|
|
|
|
// 0.1.1 - 2017-09-13 renamed to .ino;
|
|
|
|
//
|
2013-09-30 11:03:06 -04:00
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <Wire.h>
|
|
|
|
#include <hmc6352.h>
|
|
|
|
|
|
|
|
hmc6352 Compass(33);
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(19200);
|
|
|
|
Serial.println("HMC6352: Config");
|
|
|
|
Serial.println(HMC_LIB_VERSION);
|
|
|
|
|
|
|
|
dumpEEPROM();
|
|
|
|
}
|
|
|
|
int x;
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
// SHOWMENU
|
|
|
|
Serial.println("\n\n\t\tHMC6352 MENU\n");
|
|
|
|
Serial.println("F : Factory Reset");
|
|
|
|
Serial.println("E : Dump EEPROM & 2 RAM addresses");
|
|
|
|
|
|
|
|
Serial.println("R : Dump RAM");
|
|
|
|
|
|
|
|
Serial.println("A : ask sampling");
|
|
|
|
Serial.println("Q : read sample");
|
|
|
|
Serial.println("Z : continuous read (loop) - X to stop");
|
|
|
|
|
|
|
|
Serial.println("W : WakeUp");
|
|
|
|
Serial.println("S : Sleep");
|
|
|
|
|
|
|
|
Serial.println("O : Output Modus Menu");
|
|
|
|
Serial.println("7 : STANDBY MODE");
|
|
|
|
Serial.println("8 : QUERY MODE");
|
|
|
|
Serial.println("9 : CONTINUOUS MODE");
|
|
|
|
|
|
|
|
Serial.println("C : Callibration On (!!)");
|
|
|
|
Serial.println("X : Callibration Off (!!)");
|
|
|
|
|
|
|
|
Serial.println("I : set I2C address (!!)");
|
|
|
|
Serial.println("@ : reset (!!)");
|
|
|
|
|
|
|
|
Serial.println("Enter your choice ...");
|
|
|
|
|
|
|
|
// WAIT FOR COMMAND
|
|
|
|
while (Serial.available() == 0);
|
|
|
|
char cmd = Serial.read();
|
|
|
|
|
|
|
|
// EXECUTE COMMAND
|
|
|
|
switch(cmd)
|
|
|
|
{
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'F':
|
2013-09-30 11:03:06 -04:00
|
|
|
factoryReset();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'E':
|
2013-09-30 11:03:06 -04:00
|
|
|
dumpEEPROM();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'R':
|
2013-09-30 11:03:06 -04:00
|
|
|
dumpRAM();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'A':
|
2013-09-30 11:03:06 -04:00
|
|
|
askHeading();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'Q':
|
2013-09-30 11:03:06 -04:00
|
|
|
readHeading();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'W':
|
2013-09-30 11:03:06 -04:00
|
|
|
x = Compass.wakeUp();
|
|
|
|
Serial.println(x, DEC);
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'S':
|
2013-09-30 11:03:06 -04:00
|
|
|
x = Compass.sleep();
|
|
|
|
Serial.println(x, DEC);
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'O':
|
2013-09-30 11:03:06 -04:00
|
|
|
OutPutModusMenu();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case '7':
|
2013-09-30 11:03:06 -04:00
|
|
|
// mode , freq , reset
|
|
|
|
Compass.setOperationalModus(STANDBY, 1, true); // 10 default val
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case '8':
|
2013-09-30 11:03:06 -04:00
|
|
|
Compass.setOperationalModus(QUERY, 1, true); // 10 default val
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case '9':
|
2013-09-30 11:03:06 -04:00
|
|
|
Compass.setOperationalModus(CONT, 20, true); // 10 default val
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'U':
|
2013-09-30 11:03:06 -04:00
|
|
|
Compass.callibrationOn();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'X':
|
2013-09-30 11:03:06 -04:00
|
|
|
Compass.callibrationOff();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'I':
|
2013-09-30 11:03:06 -04:00
|
|
|
setI2Caddress();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case '@':
|
2013-09-30 11:03:06 -04:00
|
|
|
Compass.updateOffsets();
|
|
|
|
break;
|
2017-09-13 02:58:18 -04:00
|
|
|
case 'Z':
|
2013-09-30 11:03:06 -04:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
readHeading();
|
2017-09-13 02:58:18 -04:00
|
|
|
if (Serial.available()>0)
|
2013-09-30 11:03:06 -04:00
|
|
|
{
|
|
|
|
Serial.read();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
delay(50); // 20 Hz
|
2017-09-13 02:58:18 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-09-30 11:03:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutPutModusMenu()
|
|
|
|
{
|
|
|
|
Serial.println(Compass.getOutputModus());
|
|
|
|
Serial.println("output modus value :");
|
|
|
|
Serial.println("0 Heading (default)");
|
2017-09-13 02:58:18 -04:00
|
|
|
Serial.println("1 Raw Magnetometer X");
|
|
|
|
Serial.println("2 Raw Magnetometer Y");
|
|
|
|
Serial.println("3 Magnetometer X");
|
2013-09-30 11:03:06 -04:00
|
|
|
Serial.println("4 Magnetometer Y");
|
|
|
|
// WAIT FOR PARAM
|
|
|
|
while (Serial.available() == 0);
|
|
|
|
char cmd = Serial.read();
|
|
|
|
cmd -= '0'; // make a digit out of it
|
|
|
|
int x = Compass.setOutputModus(cmd);
|
|
|
|
Serial.print("Returns: ");
|
|
|
|
Serial.println(x);
|
2017-09-13 02:58:18 -04:00
|
|
|
}
|
2013-09-30 11:03:06 -04:00
|
|
|
|
2017-09-13 02:58:18 -04:00
|
|
|
void askHeading()
|
2013-09-30 11:03:06 -04:00
|
|
|
{
|
|
|
|
int x = Compass.askHeading();
|
|
|
|
Serial.print("Returns: ");
|
2017-09-13 02:58:18 -04:00
|
|
|
Serial.println(x);
|
2013-09-30 11:03:06 -04:00
|
|
|
}
|
|
|
|
|
2017-09-13 02:58:18 -04:00
|
|
|
void readHeading()
|
2013-09-30 11:03:06 -04:00
|
|
|
{
|
|
|
|
int x = Compass.readHeading();
|
|
|
|
Serial.print("DEGREE: ");
|
|
|
|
Serial.println(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void factoryReset()
|
|
|
|
{
|
|
|
|
Compass.factoryReset();
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpRAM()
|
|
|
|
{
|
|
|
|
Serial.println("\nDUMP RAM: (decimal values)");
|
|
|
|
Serial.println("Size 256 bytes (?) meaning mostly unknown");
|
|
|
|
for (int i = 0; i< 256; i++)
|
|
|
|
{
|
|
|
|
if (i % 16 == 0)
|
|
|
|
{
|
|
|
|
Serial.println();
|
|
|
|
Serial.print(i, HEX);
|
|
|
|
Serial.print(" : ");
|
|
|
|
}
|
|
|
|
Serial.print(Compass.readRAM(i), HEX);
|
|
|
|
Serial.print("\t ");
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
void dumpEEPROM()
|
|
|
|
{
|
|
|
|
Serial.println("\nDUMP EEPROM: (decimal values)");
|
|
|
|
Serial.println("Read datasheet for meaning\n");
|
|
|
|
|
|
|
|
Serial.print(" I2C ADDRESS: 0x00 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(0), DEC);
|
|
|
|
Serial.print("MAGN X OFFSET MSB: 0x01 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(1), DEC);
|
|
|
|
Serial.print("MAGN X OFFSET LSB: 0x02 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(2), DEC);
|
|
|
|
Serial.print("MAGN Y OFFSET MSB: 0x03 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(3), DEC);
|
|
|
|
Serial.print("MAGN Y OFFSET LSB: 0x04 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(4), DEC);
|
|
|
|
Serial.print(" Time Delay: 0x05 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(5), DEC);
|
|
|
|
Serial.print(" Measurement Sum: 0x06 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(6), DEC);
|
|
|
|
Serial.print(" Software Version: 0x07 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(7), DEC);
|
|
|
|
Serial.print(" Operational Mode: 0x08 : ");
|
|
|
|
Serial.println(Compass.readEEPROM(8), DEC);
|
|
|
|
|
|
|
|
Serial.println("REGISTERS RAM");
|
|
|
|
Serial.print("Operational Mode Control Byte: 0x74 : ");
|
|
|
|
Serial.println(Compass.getOperationalModus(), BIN);
|
|
|
|
Serial.print("Output Data Mode: 0x4E : ");
|
|
|
|
Serial.println(Compass.getOutputModus(), BIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setI2Caddress()
|
|
|
|
{
|
|
|
|
Serial.print("Current I2C address : ");
|
|
|
|
Serial.println(Compass.getI2CAddress());
|
2017-09-13 02:58:18 -04:00
|
|
|
Serial.println();
|
|
|
|
Serial.println("Give new value: (decimal, 2 digits)");
|
|
|
|
Serial.println("00 to escape");
|
2013-09-30 11:03:06 -04:00
|
|
|
|
|
|
|
while (Serial.available() < 2);
|
|
|
|
int v1 = Serial.read();
|
|
|
|
v1 -= '0';
|
|
|
|
v1 *= 10;
|
|
|
|
int v2 = Serial.read();
|
|
|
|
v2 -= '0';
|
|
|
|
v1 += v2;
|
|
|
|
if (v1 == 0) return;
|
|
|
|
Serial.println(v1);
|
|
|
|
int x = Compass.setI2CAddress(v1);
|
|
|
|
Serial.println(x);
|
|
|
|
Serial.print("Current I2C address : ");
|
|
|
|
Serial.println(Compass.getI2CAddress());
|
|
|
|
|
2017-09-13 02:58:18 -04:00
|
|
|
}
|
2013-09-30 11:03:06 -04:00
|
|
|
|
|
|
|
// END OF FILE
|