Got it working!
Different approach as above with success:
At startup i do:
Code:
Proc InitCompass()
BusOut 011010, 0xB,[0x01] 'Set reset period, Datasheet page 13 for application example.
DelayMS 5
BusOut 011010, 0x9,[000001] 'Continuous measurement mode
EndProc
then i read every 100ms the first 6 registers. Datasheet page 16 and calculate the heading:
Code:
Proc GetCompass()
BusIn 011011,0x00,[XCom.LowByte,XCom.HighByte, YCom.LowByte,YCom.HighByte,ZCom.LowByte,ZCom.HighByte]
Heading = ReadHeading()
EndProc
Code:
Proc ReadHeading(),WordDim x As SWord
Dim y As SWord
Dim fx As Float
Dim fy As Float
Dim Heading As SWord
Dim DeclinationAngle As Float
If XCom < Xlow Then Xlow = XCom
If XCom > XHigh Then XHigh = XCom
If YCom < Ylow Then Ylow = YCom
If YCom >YHigh Then YHigh = YCom
If Xlow = XHigh Or Ylow = YHigh Then
Heading = 0
Result = 0
Return
EndIf
DeclinationAngle = (1 + (56/60))/(180/pi) 'For Grootebroek in the Netherlands;)
x = XCom - (XHigh + Xlow) /2
y = YCom - (YHigh + Ylow) /2
fx = x/(XHigh - Xlow)
fy = y/(YHigh - Ylow)
Heading = 180 * ATan2( fy,fx)/ Pi
Heading = Heading - 80 ; Somehow i need to correct with -80 (compared to telephone compass which looks ok).
If Heading <=0 Then Heading = Heading + 360
HRSOut "Xcom= ",SDec XCom," xlow= ",SDec Xlow," xhigh=",Dec XHigh," x= ",SDec x," fx= ",SDec fx," Heading= ",Dec Heading ,"DeclinationAngle= ",SDec DeclinationAngle,"\r\n"
Result = Heading
EndProc
There is a huge error from +80 for the calculated heading which i still need to fix somehow..