PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Examples
Devices
Sensor
Compass 3-Axis HMC5883L
Test Compass HMC5883L.c
1
/*
2
Test Compass HMC5883L.c
3
4
Test compass with SCL connected to P3 and SDA connected to P2. Display
5
measurement results in SimpleIDE Terminal.
6
7
http://learn.parallax.com/propeller-c-simple-devices/
8
*/
9
10
#include "
simpletools.h
"
// Include simpletools header
11
#include "
compass3d.h
"
// Include compass3d header
12
13
int
main
()
// main function
14
{
15
int
x
, y, z;
// Declare x, y, & z axis variables
16
17
i2c
*bus =
i2c_newbus
(3, 2, 0);
// New I2C bus SCL=P3, SDA=P2
18
compass_init
(bus);
// Initialize compass on bus.
19
20
while
(1)
// Repeat indefinitely
21
{
22
compass_read
(bus, &
x
, &y, &z);
// Compass vals -> variables
23
print
(
"%cx=%d, y=%d, z=%d%c\n"
,
// Display compass variables
24
HOME
,
x
, y, z,
CLREOL
);
25
26
float
fy = (
float
) y;
// Ints to floats
27
float
fx = (
float
)
x
;
28
float
heading = atan2(fy, fx) * 180.0/PI;
// Calculate heading with floats
29
if
(heading < 0.0)
// Convert -180...+180 to 0...360
30
heading = (360.0 + heading);
31
print
(
"heading = %.2f%c\n"
,
// Display heading
32
heading,
CLREOL
);
33
34
pause
(500);
// Wait 1/2 second
35
}
36
}
37
38
compass_read
void compass_read(i2c *bus, int *px, int *py, int *pz)
Read values from compass.
Definition:
compass3d.c:18
float
compass_init
void compass_init(i2c *bus)
Initialize the Compass.
Definition:
compass3d.c:10
HOME
#define HOME
HOME character (1) sends SimpleIDE Terminal's cursor to top-left "home" position.
Definition:
simpletools.h:233
pause
void pause(int time)
Delay cog from moving on to the next statement for a certain length of time.
Definition:
libws2812.c:125
simpletools.h
This library provides convenient functions for a variety of microcontroller I/O, timing,...
print
int print(const char *format,...) __attribute__((format(printf
Print format "..." args to the default simple terminal device. The output is limited to 256 bytes.
i2c_newbus
i2c * i2c_newbus(int sclPin, int sdaPin, int sclDrive)
Set up a simple serial driver with transmit & receive pins.
Definition:
i2c_init.c:26
main
int main(void)
Definition:
GraphicsTest.c:20
i2c_st
Definition:
simplei2c.h:25
x
int x
Definition:
07 Box and Lines.c:13
compass3d.h
This library provides convenience functions for reading measurements from the Parallax Compass Module...
CLREOL
#define CLREOL
CLREOL character (11) erases all SimpleIDE Terminal characters to the right of the cursor.
Definition:
simpletools.h:323
Generated on Fri May 20 2022 16:48:04 for PropWare by
1.8.17