Quote Originally Posted by richard View Post
I bought one to play with

after looking at the raw data I came up with this , works ok if device is held level , tilt compensation is elusive at this stage



Code:
'****************************************************************
'*  Name    : hmc5883l.BAS                                      *
'*  Author  : [select VIEW...EDITOR OPTIONS]                    *
'*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 29/04/2011                                        *
'*  Version :                                              *
'*  Notes   :                                                *
'*          :  12f1822     
'*          :  
             '
'****************************************************************
 #CONFIG
cfg1 = _FOSC_INTOSC
cfg1&= _WDTE_ON
cfg1&= _PWRTE_OFF
cfg1&= _MCLRE_ON
cfg1&= _CP_OFF
cfg1&= _CPD_OFF
cfg1&= _BOREN_ON
cfg1&= _CLKOUTEN_OFF
cfg1&= _IESO_ON
cfg1&= _FCMEN_ON
  __CONFIG _CONFIG1, cfg1
cfg2 = _WRT_OFF
cfg2&= _PLLEN_OFF
cfg2&= _STVREN_ON
cfg2&= _BORV_19
cfg2&= _LVP_OFF
  __CONFIG _CONFIG2, cfg2
#ENDCONFIG
 

      osccon=$6A    '4 mhz
      anselA=0       'dig i/o
        
      trisA=1110
  
  
  
  
   pause 2000
  serout2 porta.0,84, ["ready",13,10]
  lata.0=1
  
  
   SDA            var      porta.2                 'transceives to/from SDA
   SCL            var      porta.4                  'sends clock pulses
WRITE_DATA     CON     $3C  'Used to perform a Write operation
READ_DATA      CON     $3D  'Used to perform a Read operation
MODE           CON     $02  'Read/Write Register, Selects the operating mode. Default = Single measurement
                             'Send $3C $02 $00 on power up to change to continuous measurement mode.
X_MSB          CON     $03  'Read Register, Output of X MSB 8-bit value.
X_LSB          CON     $04  'Read Register, Output of X LSB 8-bit value.
Z_MSB          CON     $05  'Read Register, Output of Z MSB 8-bit value.
Z_LSB          CON     $06  'Read Register, Output of Z LSB 8-bit value.
Y_MSB          CON     $07  'Read Register, Output of Y MSB 8-bit value.
Y_LSB          CON     $08  'Read Register, Output of Y LSB 8-bit value.
Cont_Mode      CON     $0
X              VAR      byte 'x sensor measured value
Y              VAR      byte 'y sensor measured value
Z              VAR      byte 'z sensor measured value
READX         VAR      WORD 
READY         VAR      WORD
READZ         VAR      WORD
brads          VAR      Word 'angle in brads
degr           VAR      Word 'angle in degrees
Radians        VAR      Word 'value calculated using X ATN -Y

' -----[ Main Routine ]-------------------------------------------------------
PAUSE 50                                     ' Give sensor needed power up time
  I2CWRITE SDA,SCL,WRITE_DATA,MODE,[0,1,Cont_Mode]  ' Send continuous output command
PAUSE 10 
DO
  GOSUB GetRawReading                       ' Get RAW Compass reading
  serout2 porta.0,84, ["x ",SDEC x," y ",SDEC y ," z ",SDEC z,13,10]
  
    brads = -x ATN -y ;
    degr = brads */360 'convert to degrees
   ;serout2 porta.0,84, ["xy ",SDEC Radians ,","]
    serout2 porta.0,84, ["xy ",DEC degr ,13,10]
     brads = -x ATN -z ;
     degr = brads */360 'convert to degrees
   ; serout2 porta.0,84, ["xz ",SDEC Radians ,","]
    serout2 porta.0,84, ["xz ",DEC degr ,13,10]
       brads = -y ATN -z
       degr = brads */360 'convert to degrees
    ;serout2 porta.0,84, ["yz ",SDEC Radians ,","]
    serout2 porta.0,84, ["yz ",DEC degr ,13,10]
    
   PAUSE 2000 
LOOP
' -----[ Subroutines ]--------------------------------------------------------
GetRawReading:                                 'Compass module subroutine
                                    'Wait for new data to become available
    I2CREAD SDA,SCL,READ_DATA, X_MSB,[READX,READZ,READY]  'Read the data starting at x_msb 3
  
    x = readx >>2  
    Y = readY >>2
    Z = readZ >>2
RETURN
So according to your code , we get x , y , z raw readings as byte datas .... and xy xz yz calculations obtained using some trigonometry .. But what do those datas mean ? I think these xy xz yz readings are referring a direction but which one is what ? Does xy give me the magnetic north ? or is it xz or maybe yz ...

I'd be so happy if any of you guys could help me with that ...

Thanks ..