HMC5843 Dataread issues
Hello I modified the code for the HMC6343 so that it might work with the HMC5843. I am having some trouble getting the HMC5843 into continuous measurement mode. The code currently returns a constant value over serial.
Mag XYZ: 16 8192 -301
I am pretty sure that this is due to the continous measurement mode not being set right, so the constant values are some other registers that I am not intending to read.
On a positive not the divice is recognized on the bus, and does return different values depending on different commands sent...I am just trying to get the values that should be returned back.
Anyways any help is appreciated I will continue to work on it myself. Let me know whats wrong....
Well I am getting a runtime error for attachments so I will just add the files that I modified below the rest are on OBEX...
HMC5843test.spin
HMC5843Object.spin
Mag XYZ: 16 8192 -301
I am pretty sure that this is due to the continous measurement mode not being set right, so the constant values are some other registers that I am not intending to read.
On a positive not the divice is recognized on the bus, and does return different values depending on different commands sent...I am just trying to get the values that should be returned back.
Anyways any help is appreciated I will continue to work on it myself. Let me know whats wrong....
Well I am getting a runtime error for attachments so I will just add the files that I modified below the rest are on OBEX...
HMC5843test.spin
{{
HMC5843 driver test
Tim Moore Aug 08
Edit from HMC 6343 Driver by Jeff
}}
CON
_clkmode = xtal1 + pll16x
_xinfreq = 6_000_000 'NOTE SPEED
HMC5843_Write = $3C
OBJ
'1 Cog here
uarts : "pcFullDuplexSerial4FC" '1 COG for 4 serial ports
config : "configHMC5843" 'no COG required
i2cObject : "basic_i2c_driver" '0 COG
hmc : "hmc5843object" '0 COG
lis : "lis302dlobject" '0 COG
i2cScan : "i2cScan" '0 COG
VAR
long i2cSCL
Pub Start | X, Y, Z
config.Init(@pininfo,@i2cinfo)
waitcnt(clkfreq*3 + cnt) 'delay for debugging
i2cSCL := config.GetPin(CONFIG#I2C_SCL1)
' setup i2cObject
i2cObject.Initialize(i2cSCL)
uarts.Init
uarts.AddPort(0,config.GetPin(CONFIG#DEBUG_RX),config.GetPin(CONFIG#DEBUG_TX),{
} UARTS#PINNOTUSED,UARTS#PINNOTUSED,UARTS#DEFAULTTHRESHOLD, {
} UARTS#NOMODE,UARTS#BAUD115200) 'Add debug port
uarts.Start 'Start the ports
uarts.str(0,string("HMCTest",13))
i2cScan.i2cScan(i2cSCL)
uarts.tx(0,10)
'Set the device for continuous measurement mode
hmc.ContHMC5843(i2cSCL)
repeat
hmc.GetMag(i2cSCL, HMC5843_Write,@X,@Y,@Z)
uarts.str(0,string("Mag XYZ: "))
uarts.dec(0,X)
uarts.tx(0," ")
uarts.dec(0,Y)
uarts.tx(0," ")
uarts.dec(0,Z)
uarts.tx(0,10)
uarts.tx(0,13)
waitcnt(clkfreq + cnt) 'delay for debugging
DAT
'pin configuration table for this project
pininfo word CONFIG#NOT_USED 'pin 0
word CONFIG#NOT_USED 'pin 1
word CONFIG#NOT_USED 'pin 2
word CONFIG#NOT_USED 'pin 3
word CONFIG#NOT_USED 'pin 4
word CONFIG#NOT_USED 'pin 5
word CONFIG#NOT_USED 'pin 6
word CONFIG#NOT_USED 'pin 7
word CONFIG#NOT_USED 'pin 8
word CONFIG#NOT_USED 'pin 9
word CONFIG#NOT_USED 'pin 10
word CONFIG#NOT_USED 'pin 11
word CONFIG#NOT_USED 'pin 12
word CONFIG#NOT_USED 'pin 13
word CONFIG#NOT_USED 'pin 14
word CONFIG#NOT_USED 'pin 15
word CONFIG#NOT_USED 'pin 16
word CONFIG#NOT_USED 'pin 17
word CONFIG#NOT_USED 'pin 18
word CONFIG#NOT_USED 'pin 19
word CONFIG#NOT_USED 'pin 20
word CONFIG#NOT_USED 'pin 21
word CONFIG#NOT_USED 'pin 22
word CONFIG#NOT_USED 'pin 23
word CONFIG#NOT_USED 'pin 24
word CONFIG#NOT_USED 'pin 25
word CONFIG#NOT_USED 'pin 26
word CONFIG#NOT_USED 'pin 27
word CONFIG#I2C_SCL1 'pin 28 - I2C - eeprom, sensors, rtc, fpu
word CONFIG#I2C_SDA1 'pin 29
word CONFIG#DEBUG_TX 'pin 30
word CONFIG#DEBUG_RX 'pin 31
i2cinfo byte CONFIG#HMC6343 'HMC5843 compass
byte %0011_1100
byte CONFIG#LIS3LV02DQ_0 'LIS3LV02DQ accelometer
byte %0011_1010
byte CONFIG#NOT_USED
byte CONFIG#NOT_USED
HMC5843Object.spin
{{
┌──────────────────────────────────────────
│ HMC6343 Driver 1.0
│ Author: Tim Moore
│ Copyright (c) Aug 2008 Tim Moore
│ See end of file for terms of use.
└──────────────────────────────────────────
This code is a modified version of the HMC6343 Object for the HMC5843
3-axis MAG HMC5843 by Jeff
}}
CON
'I2C registers for the HMC5843
HMC5843_POST_MAG = $3D
HMC5843_Write = $3C
HMC5843_Add_B = $02
HMC5843_Dat_Cont = $00
OBJ
i2cObject : "basic_i2c_driver" '0 COG
PRI GetData(i2cSCL, _deviceAddress, register, XPtr, YPtr, ZPtr) | resp
resp := 0
i2cObject.start(i2cSCL)
resp |= i2cObject.write(i2cSCL,_deviceAddress | 0)
resp |= i2cObject.write(i2cSCL,register)
waitcnt(clkfreq/1000 + cnt) '1 MS
i2cObject.start(i2cSCL)
resp |= i2cObject.write(i2cSCL,_deviceAddress | 1)
long[noparse][[/noparse]XPTR] := (i2cObject.read(i2cSCL,i2cObject#ACK) << 8) | (i2cObject.read(i2cSCL,i2cObject#ACK) & $ff)
~~long[noparse][[/noparse]XPTR]
long[noparse][[/noparse]YPTR] := (i2cObject.read(i2cSCL,i2cObject#ACK) << 8) | (i2cObject.read(i2cSCL,i2cObject#ACK) & $ff)
~~long[noparse][[/noparse]YPTR]
long[noparse][[/noparse]ZPTR] := (i2cObject.read(i2cSCL,i2cObject#ACK) << 8) | (i2cObject.read(i2cSCL,i2cObject#NAK) & $ff)
~~long[noparse][[/noparse]ZPTR]
i2cObject.stop(i2cSCL)
return resp
PUB GetId(i2cSCL, _deviceAddress)
i2cObject.start(i2cSCL)
i2cObject.write(i2cSCL,_deviceAddress | 0)
i2cObject.start(i2cSCL)
i2cObject.write(i2cSCL,_deviceAddress | 1)
result := i2cObject.read(i2cSCL,i2cObject#NAK)
i2cObject.stop(i2cSCL)
PUB GetMag(i2cSCL, _deviceAddress, XPtr, YPtr, ZPtr)
GetData(i2cSCL, _deviceAddress, HMC5843_POST_MAG, XPtr, YPtr, ZPtr)
PUB ContHMC5843(i2cSCL)
i2cObject.start(i2cSCL)
i2cObject.write(i2cSCL,HMC5843_Write)
i2cObject.write(i2cSCL,HMC5843_Add_B)
i2cObject.write(i2cSCL,HMC5843_Dat_Cont)
i2cObject.stop(i2cSCL)

Comments
To change the measurement mode to continuous measurement mode, after the 5 milli-second power-up time send the
three bytes:
0x3C 0x02 0x00
This writes the 00 into the second register or mode register to switch from single to continuous measurement mode
setting. With the data rate at the factory default of 10Hz updates (100 milli-seconds interval), a 100 milli-second delay
should be made by the I2C master before querying the HMC5843 data registers for new measurements. To clock out the
new data, send:
0x3D, and clock out DXRA, DXRB, DYRA, DYRB, DZRA, DZRB located in registers 3 through 8. The HMC5843 will
automatically re-point back to register 3 for the next 0x3D query, expected 100 milli-seconds or later.
So I thought that I should write the 3D to start clocking out the data.
I see what you are saying from page 10, I was just following the above.
I will try to send a three and see what happens.