Jetzt habe ich auch den HMC6352 Kompass getestet (code aus dem Mood pill projekt):
Code:
'===CHIP SETTINGS===
$regfile = "m328pdef.dat"
$framesize = 32
$swstack = 32
$hwstack = 32
$crystal = 8000000
$baud = 38400 ' use a low baudrate, if you use internal RC-clock
$lib "i2c_twi.lbx" ' we do not use software emulated I2C but the TWI
Waitms 1000 ' give Compass some time to stabilize
' I2C-Slave-Address of the HMC6352 Digital Compass Module and Configuration values
Const Compass_address = &H42
Dim Compass_w_array(3) As Byte
Compass_w_array(1) = &H47 ' "G"-Command: write to RAM
Compass_w_array(2) = &H74 ' RAM Address H74: Operational Mode
Compass_w_array(3) = &B01110000 ' 20Hz, Set/Reset=yes, Standby mode
' Compass value read variables
Dim Compass_r_array(2) As Byte ' Byte array for the two bytes of heading value
Dim Compass_value As Word At Compass_r_array(1) Overlay ' WORD variable for the heading value
Config Scl = Portc.5 ' we need to provide the SCL pin name
Config Sda = Portc.4 ' we need to provide the SDA pin name
I2cinit ' we need to set the pins in the proper state
'use a bps rate lower than 100000, if your µC-clock-speed is
'below approx. 4 MHz
Config Twi = 100000 ' wanted clock frequency
'will set TWBR and TWSR
'Twbr = 32 'bit rate register
'Twsr = 0 ' pre scaler bits
Waitms 50
I2csend Compass_address , Compass_w_array(1) , 3 ' set Compass Configuration
Waitms 100
Do
Compass_w_array(1) = &H41 ' Command "A": Read heading value
I2csend Compass_address , Compass_w_array(1) , 1 ' send to HMC6352
Waitms 7
I2creceive Compass_address , Compass_r_array(1) , 0 , 2 ' receive the two heading bytes MSB first
Swap Compass_r_array(1) , Compass_r_array(2) ' swap MSB and LSB bytes to be read as WORD variable
Print Compass_value
Waitms 10
Loop
Funktioniert sehr schön.
Außerdem habe ich hier noch Code vom BMP085 Drucksensor, ich frage grad mal den Autor ob sein Code so auch veröffentlicht werden darf.
Lesezeichen