2017-09-13 02:58:18 -04:00
|
|
|
//
|
|
|
|
// FILE: hmc6352Config.ino
|
2013-09-30 11:03:06 -04:00
|
|
|
// AUTHOR: Rob Tillaart
|
2020-11-27 05:16:22 -05:00
|
|
|
// PURPOSE: test HMC6352 library for Arduino
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
|
|
|
|
#include <hmc6352.h>
|
|
|
|
|
|
|
|
hmc6352 Compass(33);
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
void setup()
|
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
Serial.begin(115200);
|
|
|
|
Serial.println(__FILE__);
|
|
|
|
Serial.println("LIB: ");
|
|
|
|
Serial.println(HMC6352_LIB_VERSION);
|
|
|
|
|
|
|
|
Compass.begin();
|
2013-09-30 11:03:06 -04:00
|
|
|
|
|
|
|
dumpEEPROM();
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
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
|
2020-11-27 05:16:22 -05:00
|
|
|
switch (cmd)
|
2013-09-30 11:03:06 -04:00
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
case 'F':
|
|
|
|
factoryReset();
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
dumpEEPROM();
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
dumpRAM();
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
askHeading();
|
|
|
|
break;
|
|
|
|
case 'Q':
|
2013-09-30 11:03:06 -04:00
|
|
|
readHeading();
|
2020-11-27 05:16:22 -05:00
|
|
|
break;
|
|
|
|
case 'W':
|
|
|
|
Serial.println(Compass.wakeUp(), DEC);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
Serial.println(Compass.sleep(), DEC);
|
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
OutPutModusMenu();
|
|
|
|
break;
|
|
|
|
case '7':
|
|
|
|
// mode , freq , reset
|
|
|
|
Compass.setOperationalModus(STANDBY, 1, true); // 10 default val
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
break;
|
|
|
|
case '8':
|
|
|
|
Compass.setOperationalModus(QUERY, 1, true); // 10 default val
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
break;
|
|
|
|
case '9':
|
|
|
|
Compass.setOperationalModus(CONT, 20, true); // 10 default val
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
break;
|
|
|
|
case 'U':
|
|
|
|
Compass.callibrationOn();
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
Compass.callibrationOff();
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
setI2Caddress();
|
|
|
|
break;
|
|
|
|
case '@':
|
|
|
|
Compass.updateOffsets();
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
while (1)
|
2013-09-30 11:03:06 -04:00
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
readHeading();
|
|
|
|
if (Serial.available() > 0)
|
|
|
|
{
|
|
|
|
Serial.read();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
delay(50); // 20 Hz
|
2013-09-30 11:03:06 -04:00
|
|
|
}
|
2020-11-27 05:16:22 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2013-09-30 11:03:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
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
|
|
|
|
2021-12-19 07:57:35 -05: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
|
|
|
}
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2017-09-13 02:58:18 -04:00
|
|
|
void readHeading()
|
2013-09-30 11:03:06 -04:00
|
|
|
{
|
2020-11-27 05:16:22 -05:00
|
|
|
int heading = Compass.readHeading();
|
2013-09-30 11:03:06 -04:00
|
|
|
Serial.print("DEGREE: ");
|
2020-11-27 05:16:22 -05:00
|
|
|
Serial.println(heading);
|
2013-09-30 11:03:06 -04:00
|
|
|
}
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
void factoryReset()
|
|
|
|
{
|
|
|
|
Compass.factoryReset();
|
|
|
|
Serial.println("Reboot Arduino");
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
void dumpRAM()
|
|
|
|
{
|
|
|
|
Serial.println("\nDUMP RAM: (decimal values)");
|
|
|
|
Serial.println("Size 256 bytes (?) meaning mostly unknown");
|
2020-11-27 05:16:22 -05:00
|
|
|
for (int i = 0; i < 256; i++)
|
2013-09-30 11:03:06 -04:00
|
|
|
{
|
|
|
|
if (i % 16 == 0)
|
|
|
|
{
|
|
|
|
Serial.println();
|
|
|
|
Serial.print(i, HEX);
|
2020-11-27 05:16:22 -05:00
|
|
|
Serial.print("\t: ");
|
2013-09-30 11:03:06 -04:00
|
|
|
}
|
2020-11-27 05:16:22 -05:00
|
|
|
int val = Compass.readRAM(i);
|
|
|
|
if (val < 0x10) Serial.print('0');
|
|
|
|
Serial.print(val, HEX);
|
2013-09-30 11:03:06 -04:00
|
|
|
Serial.print("\t ");
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
2013-09-30 11:03:06 -04:00
|
|
|
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
|
|
|
|
2021-12-19 07:57:35 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|