HMC5883L Magnetometer chip Possibilities.


Closed Thread
Results 1 to 40 of 58

Hybrid View

  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    you can do a similar thing with y axis too ,
    my routine will give the same result as the scale offset method its just much quicker and simpler.

    don't forget the ATN result is in radians so deg = atnresult*360/256

    I suspect that without using floats all round for these calcs the resulting resolution may be a little disappointing , but its worth a look

  2. #2
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Thank you for reading

    Hi Have tried suggestions...

    But now the I2CRead command is failing on on the HIGHBYE and LOWBYTE locations, I think it due to the X declare beng a Byte now not a Word.

    My solution was to just use the MSB part and comment out the LSB putting the result into X.

    This compiled OK but raw numbers refuse to move.... \i guess it needs the whole word.

    HELP !!

    BR
    Andy

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    But now the I2CRead command is failing on on the HIGHBYE and LOWBYTE locations, I think it due to the X declare beng a Byte now not a Word
    the i2c read uses the word var readingx the byte var x is the "scaled" result from that reading

    if you can't work that out post your code so we can see what your doing {in code tags }
    Last edited by richard; - 31st December 2014 at 10:57. Reason: edit

  4. #4
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi Richard

    Thank you for reply... Here is code to date.... With most recent error.

    Code:
    '****************************************************************
    '*  Name    : COMPASS_818.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 24/08/2014                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    
    
    
    ' LCD should be connected as follows:
    '       LCD     PIC
    '       DB4     PortA.0
    '       DB5     PortA.1
    '       DB6     PortA.2
    '       DB7     PortA.3
    '       RS      PortA.4 (add 4.7K (10k)pullup resistor to 5 volts)
    '       E       PortB.3
    '       RW      Ground
    '       Vdd     5 volts
    '       Vss     Ground
    '       Vo      5K potentiometer (or ground)
    '       DB0-3   No connect
    
    
    
    
    
        adcon1 = 7     ' Turns Analogs off
        
        DEFINE OSC 12	'Set oscillator in MHz
        'OSCCON = $70
        DEFINE LCD_EREG PORTB
        DEFINE LCD_EBIT 6
        DEFINE LCD_RSREG PORTB
        DEFINE LCD_RSBIT 7 
    
    ' -----[ Pins/Constants/Variables ]-------------------------------------------
    SDA            var      portb.1                  'transceives to/from SDA
    SCL            var      portb.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
    SCALEFACTOR    CON     4081 'SCALEFACTOR = 65536 * (255/4095) = 4081
    SCALEOFFSET    CON     2048 'The output range of the HMC5883L is -2048 to 2047; therefore to get a 0 (LOWRANGE)to 4095 range
                                'to 4095 (HIGHRANGE) range 2048 must be added to the sensor value.
    ATNFACTOR      CON     127  'the ATN function input range is -127 TO 127. In order to get the positive scaled range of
                                '0 to 255 into the range of -127 to 127, 127 must be subtracted.
    LOWRANGE       CON     0    'see SCALEOFFSET
    HIGHRANGE      CON     4095 'see SCALEOFFSET
    
    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
    azimuth        VAR      Word 'value calculated using X ATN -Y
    
    
    ' -----[ Main Routine ]-------------------------------------------------------
    
    PAUSE 500                                     ' Give sensor needed power up time
    
      I2CWRITE SDA,SCL,WRITE_DATA,MODE,[Cont_Mode]  ' Send continuous output command
    
    DO
    
      GOSUB GetRawReading                       ' Get RAW Compass reading
    
      'RAW X,Y,Z data...
      
    
            Lcdout $fe, 1 ' Clear LCD screen
            Lcdout "X=",SDEC X  ,"   Y=",SDEC Y 
            Lcdout $fe, $C0, "Z=",SDEC Z
            
            
      'DEBUG HOME, "RAW X,Y,Z", CR
      'DEBUG       "X = ",11, SDEC x, CR
      'DEBUG       "Y = ",11, SDEC y, CR
      'DEBUG       "Z = ",11, SDEC z, CR         ' Print angle
      'DEBUG CR
    
      'Calculate Azimuth
    
      GOSUB GetAzimuth
    
      'Calculated Heading in Degrees
    
      brads = azimuth     'get angle
      degr = brads */ 361 'convert to degrees
      
      'DEBUG CR
      'DEBUG "Heading in NORTH, EAST, SOUTH, and WEST clockwise from NORTH:", CLREOL, CR, CR
      'DEBUG "Degree " ,11, SDEC degr, CR 'display degrees
      GOSUB GetHeading
    
    LOOP
    
    
    ' -----[ Subroutines ]--------------------------------------------------------
    
    GetRawReading:                                 'Compass module subroutine
    
    
        PAUSE 400                                  'Wait for new data to become available
    
    
        I2CREAD SDA,SCL,READ_DATA, X_MSB,[X.HIGHBYTE]  'Read the data starting at x_msb 3
    
    
        I2CREAD SDA,SCL,READ_DATA ,X_LSB,[X.lowBYTE]
    
    'Do the same for Z and Y:
    
        I2CREAD SDA,SCL,READ_DATA, Z_MSB,[Z.HIGHBYTE]
    
    
        I2CREAD SDA,SCL,READ_DATA ,Z_LSB,[Z.lowBYTE]
    
    
    
        I2CREAD SDA,SCL,READ_DATA, Y_MSB,[Y.HIGHBYTE]
    
    
        I2CREAD SDA,SCL,READ_DATA ,Y_LSB,[Y.lowBYTE]
    
    RETURN
    
    GetAzimuth:
    'The output range of the HMC5883L is -2048 to 2047 and the input range of the ATN function
    'is -127 to 127.  I used the method to scale the output to the input discribed in Parallax's Smart
    'Sensors Student Guide v1 starting on page 76.
    'SCALEFACTOR = 65536 * (255/4095) = 4081
    'SCALEOFFSET = 2048
    'ATNFACTOR = 127
    'LOWRANGE = 0
    'HIGRANGE = 4095 (2048 + 2047 = 4095)
    
        'x = x + SCALEOFFSET
        'x = (x MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR
    
        'y = y + SCALEOFFSET
        'y = (y MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR
        
    x = readx >>4  
    Y = readY >>4
    'Z = readZ >>4
    
      'DEBUG       "SCALED X,Y", CR
      'DEBUG       "X = ",11, SDEC x, CR
      'DEBUG       "Y = ",11, SDEC y, CR
    
        azimuth = x ATN -y
        
    
    
      'DEBUG "Azimuth ",11, SDEC azimuth, CR ' display scaled azimuth
      'DEBUG "COS Azimuth ",11, SDEC COS azimuth, CR ' display scaled cos azimuth
    RETURN
    
    GetHeading:
    'Gives a heading in North, South, East and West.
    
        IF degr = 90 THEN
            'DEBUG "NORTH"
            'DEBUG CLREOL
        ENDIF
    
    
        IF degr = 0 THEN
             'DEBUG "EAST"
             'DEBUG CLREOL
        ENDIF
    
    
        IF degr = 270 THEN
             'DEBUG "SOUTH"
             'DEBUG CLREOL
        ENDIF
    
    
        IF degr = 180 THEN
             'DEBUG "WEST"
             'DEBUG CLREOL
        ENDIF
    
    RETURN
    Excuse my issues I am still learning....

    BR
    Andy

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    this should fix the i2c errors
    Code:
    '****************************************************************
    '*  Name    : COMPASS_818.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 24/08/2014                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
     
    ' LCD should be connected as follows:
    '       LCD     PIC
    '       DB4     PortA.0
    '       DB5     PortA.1
    '       DB6     PortA.2
    '       DB7     PortA.3
    '       RS      PortA.4 (add 4.7K (10k)pullup resistor to 5 volts)
    '       E       PortB.3
    '       RW      Ground
    '       Vdd     5 volts
    '       Vss     Ground
    '       Vo      5K potentiometer (or ground)
    '       DB0-3   No connect
     
     
        adcon1 = 7     ' Turns Analogs off
        
        DEFINE OSC 12 'Set oscillator in MHz
        'OSCCON = $70
        DEFINE LCD_EREG PORTB
        DEFINE LCD_EBIT 6
        DEFINE LCD_RSREG PORTB
        DEFINE LCD_RSBIT 7 
    ' -----[ Pins/Constants/Variables ]-------------------------------------------
    SDA            var      portb.1                  'transceives to/from SDA
    SCL            var      portb.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
    'SCALEFACTOR    CON     4081 'SCALEFACTOR = 65536 * (255/4095) = 4081
    'SCALEOFFSET    CON     2048 'The output range of the HMC5883L is -2048 to 2047; therefore to get a 0 (LOWRANGE)to 4095 range
                                'to 4095 (HIGHRANGE) range 2048 must be added to the sensor value.
    'ATNFACTOR      CON     127  'the ATN function input range is -127 TO 127. In order to get the positive scaled range of
                                '0 to 255 into the range of -127 to 127, 127 must be subtracted.
    'LOWRANGE       CON     0    'see SCALEOFFSET
    'HIGHRANGE      CON     4095 'see SCALEOFFSET
    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
    azimuth        VAR      Word 'value calculated using X ATN -Y
    
    ' -----[ Main Routine ]-------------------------------------------------------
    PAUSE 500                                     ' Give sensor needed power up time
      I2CWRITE SDA,SCL,WRITE_DATA,MODE,[Cont_Mode]  ' Send continuous output command
    DO
      GOSUB GetRawReading                       ' Get RAW Compass reading
      'RAW X,Y,Z data...
      
            Lcdout $fe, 1 ' Clear LCD screen
            Lcdout "X=",SDEC X  ,"   Y=",SDEC Y 
            Lcdout $fe, $C0, "Z=",SDEC Z
            
            
      'DEBUG HOME, "RAW READX,READY,READZ", CR
      'DEBUG       "X = ",11, SDEC x, CR
      'DEBUG       "Y = ",11, SDEC y, CR
      'DEBUG       "Z = ",11, SDEC z, CR         ' Print angle
      'DEBUG CR
      'Calculate Azimuth
      GOSUB GetAzimuth
      'Calculated Heading in Degrees
      brads = azimuth     'get angle
      degr = brads */ 361 'convert to degrees
      
      'DEBUG CR
      'DEBUG "Heading in NORTH, EAST, SOUTH, and WEST clockwise from NORTH:", CLREOL, CR, CR
      'DEBUG "Degree " ,11, SDEC degr, CR 'display degrees
      GOSUB GetHeading
    LOOP
    
    ' -----[ Subroutines ]--------------------------------------------------------
    GetRawReading:                                 'Compass module subroutine
    
        PAUSE 400                                  'Wait for new data to become available
    
        I2CREAD SDA,SCL,READ_DATA, X_MSB,[READX.HIGHBYTE]  'Read the data starting at x_msb 3
    
        I2CREAD SDA,SCL,READ_DATA ,X_LSB,[READX.lowBYTE]
    'Do the same for Z and Y:
        I2CREAD SDA,SCL,READ_DATA, Z_MSB,[READZ.HIGHBYTE]
    
        I2CREAD SDA,SCL,READ_DATA ,Z_LSB,[READZ.lowBYTE]
     
        I2CREAD SDA,SCL,READ_DATA, Y_MSB,[READY.HIGHBYTE]
    
        I2CREAD SDA,SCL,READ_DATA ,Y_LSB,[READY.lowBYTE]
    RETURN
    GetAzimuth:
    'The output range of the HMC5883L is -2048 to 2047 and the input range of the ATN function
    'is -127 to 127.  I used the method to scale the output to the input discribed in Parallax's Smart
    'Sensors Student Guide v1 starting on page 76.
    'SCALEFACTOR = 65536 * (255/4095) = 4081
    'SCALEOFFSET = 2048
    'ATNFACTOR = 127
    'LOWRANGE = 0
    'HIGRANGE = 4095 (2048 + 2047 = 4095)
        'x = x + SCALEOFFSET
        'x = (x MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR
        'y = y + SCALEOFFSET
        'y = (y MIN LOWRANGE MAX HIGHRANGE) ** SCALEFACTOR - ATNFACTOR
        
    x = readx >>4  
    Y = readY >>4
    'Z = readZ >>4
      'DEBUG       "SCALED X,Y", CR
      'DEBUG       "X = ",11, SDEC x, CR
      'DEBUG       "Y = ",11, SDEC y, CR
        azimuth = x ATN -y
        
    
      'DEBUG "Azimuth ",11, SDEC azimuth, CR ' display scaled azimuth
      'DEBUG "COS Azimuth ",11, SDEC COS azimuth, CR ' display scaled cos azimuth
    RETURN
    GetHeading:
    'Gives a heading in North, South, East and West.
        IF degr = 90 THEN
            'DEBUG "NORTH"
            'DEBUG CLREOL
        ENDIF
    
        IF degr = 0 THEN
             'DEBUG "EAST"
             'DEBUG CLREOL
        ENDIF
    
        IF degr = 270 THEN
             'DEBUG "SOUTH"
             'DEBUG CLREOL
        ENDIF
    
        IF degr = 180 THEN
             'DEBUG "WEST"
             'DEBUG CLREOL
        ENDIF
    RETURN

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,645


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    to get the azimuth result in degrees

    azimuth = ( x ATN -y) */ 360

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Not important at this point but you have East and North reversed.

    I'm try to do the same thing with HMC5983, I can read data which seems reasonable ie very different and repeatable for the four directions, but I can't seem to produce a heading less than about 180.

    Typical raw numbers

    x y
    N 215 -176
    E -76 7
    S -265 -287
    W -12 -474

    Seems like an awful lot of negative numbers? I wonder if I've killed it?

    George

Similar Threads

  1. 3 axis digital compass sensor HMC5883L
    By ScaleRobotics in forum General
    Replies: 1
    Last Post: - 8th June 2014, 22:19
  2. How to use this IO chip?
    By wdmagic in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th April 2013, 19:06
  3. compass chip
    By Macgman2000 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th October 2009, 01:42
  4. Do you use ICD possibilities of MicroCode Studio
    By octal in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 14th December 2007, 17:22
  5. Which chip do i use?
    By The Master in forum Off Topic
    Replies: 77
    Last Post: - 2nd December 2007, 22:28

Members who have read this thread : 0

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts