PropWare
3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Simple-Libraries
Learn
Simple Libraries
Sensor
libcompass3d
libcompass3d.c
1
/*
2
* @file libcompass3d.c
3
*
4
* @author Andy Lindsay
5
*
6
* @copyright
7
* Copyright (C) Parallax, Inc. 2013. All Rights MIT Licensed.
8
*
9
* @brief Temporary project and test harness for the name library.
10
*
11
* SCL connected to P3 and SDA connected to P2. Display measurement results in
12
* SimpleIDE Terminal
13
*/
14
15
#include "
simpletools.h
"
// Include simpletools header
16
#include "
compass3d.h
"
// Include compass3d header
17
#include "
simplei2c.h
"
18
19
int
main
()
// Main function
20
{
21
22
23
int
x
, y, z;
// Declare x, y, & z axis variables
24
pause
(1000);
25
print
(
"c32\n"
);
26
i2c
*bus =
i2c_newbus
(3, 2, 0);
// New I2C bus SCL=P3, SDA=P2
27
compass_init
(bus);
// Initialize compass on bus.
28
29
print
(
"c54\n"
);
30
i2c
*bus2 =
i2c_newbus
(5, 4, 0);
// New I2C bus SCL=P3, SDA=P2
31
compass_init
(bus2);
// Initialize compass on bus.
32
33
print
(
"%c"
,
HOME
);
34
35
while
(1)
// Repeat indefinitely
36
{
37
print
(
"%c"
,
HOME
);
38
print
(
"c32\n"
);
39
compass_read
(bus, &
x
, &y, &z);
// Compass vals -> variables
40
41
int
*px, *py, *pz;
42
43
px = &
x
;
44
py = &y;
45
pz = &z;
46
47
*px =
x
;
48
*py = y;
49
*pz = z;
50
51
float
heading = atan2(
x
, y);
52
if
(heading < 0)
53
{
54
heading += 2.0 * 3.14;
55
}
56
57
float
headingDegrees = heading * 180/3.14;
58
59
print
(
"\nx=%d, y=%d, z=%d%c\n"
,
// Display raw compass values
60
x
, y, z,
CLREOL
);
61
print
(
"heading = %f, \n"
,
// Display raw compass values
62
headingDegrees);
63
waitcnt
(
CLKFREQ
/2+
CNT
);
64
65
print
(
"c54\n"
);
66
compass_read
(bus2, &
x
, &y, &z);
// Compass vals -> variables
67
68
px = &
x
;
69
py = &y;
70
pz = &z;
71
72
*px =
x
;
73
*py = y;
74
*pz = z;
75
76
heading = atan2(
x
, y);
77
if
(heading < 0)
78
{
79
heading += 2.0 * 3.14;
80
}
81
82
headingDegrees = heading * 180/3.14;
83
84
print
(
"\nx=%d, y=%d, z=%d%c\n"
,
// Display raw compass values
85
x
, y, z,
CLREOL
);
86
print
(
"heading = %f, \n"
,
// Display raw compass values
87
headingDegrees);
88
pause
(500);
// Wait 1/2 second
89
}
90
91
92
}
93
94
95
96
97
compass_read
void compass_read(i2c *bus, int *px, int *py, int *pz)
Read values from compass.
Definition:
compass3d.c:18
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
CLKFREQ
#define CLKFREQ
Returns the current clock frequency.
Definition:
propeller.h:46
i2c_st
Definition:
simplei2c.h:25
x
int x
Definition:
07 Box and Lines.c:13
waitcnt
#define waitcnt(a)
Wait until system counter reaches a value.
Definition:
propeller.h:176
compass3d.h
This library provides convenience functions for reading measurements from the Parallax Compass Module...
simplei2c.h
Provides simple i2c start, stop, read, and write functions. See simpletools library for additional I2...
CNT
#define CNT
The system clock count.
Definition:
propeller1.h:151
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:05 for PropWare by
1.8.17