Skip to content

Commit 44de10c

Browse files
committed
Initial upload for create HMC6352 code
1 parent 2d4678f commit 44de10c

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Adafruit_HMC6352/Adafruit_HMC6352.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
from Adafruit_I2C import Adafruit_I2C
5+
6+
# ===========================================================================
7+
# HMC6352 Class
8+
# ===========================================================================
9+
10+
class HMC6352 :
11+
i2c = None
12+
13+
# HMC6352 Address
14+
address = 0x42 >> 1
15+
16+
# Commands
17+
CMD_READ_DATA = 0x41
18+
CMD_ENTER_USER_CALIBRATION = 0x45
19+
CMD_EXIT_USER_CALIBRATION = 0x4C
20+
21+
# Constructor
22+
def __init__(self):
23+
self.i2c = Adafruit_I2C(self.address)
24+
25+
def userCalibration(self):
26+
"Write 0x45 for calibration and write 0x4C for leave after 20s"
27+
self.i2c.write8(0x0, self.CMD_ENTER_USER_CALIBRATION)
28+
time.sleep(20)
29+
self.i2c.write8(0x0, self.CMD_EXIT_USER_CALIBRATION)
30+
31+
def readData(self):
32+
"Read the heading data by write a 0x41 first"
33+
self.i2c.write8(0x0, self.CMD_READ_DATA)
34+
# Wait 6 ms
35+
time.sleep(0.006)
36+
# Read 2 bytes
37+
value = self.i2c.readU16(0x0)
38+
# Reverse the data byte order
39+
value = self.i2c.reverseByteOrder(value)
40+
# Convert to 360.0 range from the raw integer value
41+
value = float('%0.1f'%value)/10
42+
43+
return value
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
3+
import time
4+
from Adafruit_HMC6352 import HMC6352
5+
6+
# ===========================================================================
7+
# Example Code
8+
# ===========================================================================
9+
10+
# Initialise the HMC6352
11+
hmc = HMC6352()
12+
13+
heading = hmc.readData()
14+
print "Compass Heading: %.1f" % heading
15+
16+
time.sleep(0.1)

Adafruit_HMC6352/Adafruit_I2C.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Adafruit_I2C/Adafruit_I2C.py

0 commit comments

Comments
 (0)