HMC5883L and @file libcompass3d.c
oakguy
Posts: 21
Hi
I tried running this sketch and the program seems to freeze at the second i2C instance. Can I ask why two instances are created?
int x, y, z; // Declare x, y, & z axis variables
pause(1000);
print("c32\n");
i2c *bus = i2c_newbus(3, 2, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus); // Initialize compass on bus.
print("c54\n");
i2c *bus2 = i2c_newbus(5, 4, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus2); // Initialize compass on bus.
My C sketch always freezes at the second instance. Any help is appreciated!
Oakguy
I tried running this sketch and the program seems to freeze at the second i2C instance. Can I ask why two instances are created?
int x, y, z; // Declare x, y, & z axis variables
pause(1000);
print("c32\n");
i2c *bus = i2c_newbus(3, 2, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus); // Initialize compass on bus.
print("c54\n");
i2c *bus2 = i2c_newbus(5, 4, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus2); // Initialize compass on bus.
My C sketch always freezes at the second instance. Any help is appreciated!
Oakguy
Comments
Do you mean that c54 is output and nothing else happens?
It's hard to tell, but it looks like your program ends after compass_init(bus2).
Is your code pasted into main() ?
If you copy/paste the whole file here, it would be clearer.
Thanks!
/*
* @file libcompass3d.c
*
* @author Andy Lindsay
*
* @copyright
* Copyright (C) Parallax, Inc. 2013. All Rights MIT Licensed.
*
* @brief Temporary project and test harness for the name library.
*
* SCL connected to P3 and SDA connected to P2. Display measurement results in
* SimpleIDE Terminal
*/
#include "simpletools.h" // Include simpletools header
#include "compass3d.h" // Include compass3d header
#include "simplei2c.h"
int main() // Main function
{
int x, y, z; // Declare x, y, & z axis variables
pause(1000);
print("c32\n");
i2c *bus = i2c_newbus(3, 2, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus); // Initialize compass on bus.
print("c54\n");
i2c *bus2 = i2c_newbus(5, 4, 0); // New I2C bus SCL=P3, SDA=P2
compass_init(bus2); // Initialize compass on bus.
print("%c", HOME);
while(1) // Repeat indefinitely
{
print("%c", HOME);
print("c32\n");
compass_read(bus, &x, &y, &z); // Compass vals -> variables
int *px, *py, *pz;
px = &x;
py = &y;
pz = &z;
*px = x;
*py = y;
*pz = z;
float heading = atan2(x, y);
if(heading < 0)
{
heading += 2.0 * 3.14;
}
float headingDegrees = heading * 180/3.14;
print("\nx=%d, y=%d, z=%d%c\n", // Display raw compass values
x, y, z, CLREOL);
print("heading = %f, \n", // Display raw compass values
headingDegrees);
waitcnt(CLKFREQ/2+CNT);
print("c54\n");
compass_read(bus2, &x, &y, &z); // Compass vals -> variables
px = &x;
py = &y;
pz = &z;
*px = x;
*py = y;
*pz = z;
heading = atan2(x, y);
if(heading < 0)
{
heading += 2.0 * 3.14;
}
headingDegrees = heading * 180/3.14;
print("\nx=%d, y=%d, z=%d%c\n", // Display raw compass values
x, y, z, CLREOL);
print("heading = %f, \n", // Display raw compass values
headingDegrees);
pause(500); // Wait 1/2 second
}
}
So, you have added a second compass?
I have an appointment this afternoon ... maybe Andy will see this.
...\Documents\SimpleIDE\Learn\Examples\Devices\Sensor\Compass 3-Axis HMC5883L
Andy
Your right, that sketch does run without error, however, it does not return a 0 - 360 heading value. Just so I'm running the right one:
Test Compass HMC5883L.c
Version 0.94 for use with SimpleIDE 9.40 and its Simple Libraries
Test compass with SCL connected to P3 and SDA connected to P2. Display
measurement results in SimpleIDE Terminal.
learn.parallax.com/propeller-c-tutorials
I only get a -70 to a -80 for a heading.
Thanks
Guy
I checked the math and the formulas are correct. Perhaps its my chip that is defective. I entered the y and x in Excel and ended up with the same value your sketch computed! My bad!
206
-1583
-82.6275
-76.3475
Thanks
Guy
Let's check and make sure our circuits match:
Compass GND -> Activity Board GND
Compass VIN -> Activity Board 3.3 V
Compass SCL -> 100 ohm resistor (brown-black-brown) -> Activity Board P3
Compass SDA -> 100 ohm resistor (brown-black-brown) -> Activity Board P2
NOTE: 220 ohm resistors (red-red-brown) are okay too.
Another item to check is possible interference from strong sources of magnetic fields. Sometimes a metal brace under the table can be magnetized and affects the measurements. Nearby components and wires can also affect the measurements, but not to the point where they appear to be stuck in one position. I usually hold my board well above the table when I test.
Last item, make sure you've got the latest Learn folder and SimpleIDE:
SimpleIDE v0.9.43
http://learn.parallax.com/propeller-c-set-simpleide
Learn folder 2013.11.01
http://learn.parallax.com/propeller-c-set-simpleide/update-your-learn-folder
Example Code from ....Documents\SimpleIDE\Learn\Examples\Devices\Sensor\Compass 3-Axis HMC5883L\