HMC5883L Magnetometer chip Possibilities.


Closed Thread
Results 1 to 40 of 58

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi

    Have tided code.... And it does seem to work better now. But Degrees are reversed (360 to 0) Clockwise. Or am I reading the sensor upside down?

    Azimuth is renamed Radians... the resulting calculation now used to give degrees.

    Z... is not used at all... Maybe I can add or subtract the locale degree error (Unknown at time of writing) into code once degrees working the right way round.

    Latest code show below.... Bear in mind I am not a software guy so any suggestions of further advice welcome.

    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
    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 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 READX,READY,READZ data...
      
            'Lcdout $fe, 1 ' Clear LCD screen
            'Lcdout "X=",SDEC READX  ,"   Y=",SDEC READY 
            'Lcdout $fe, $C0, "Z=",SDEC READZ
            
    'Calculate Azimuth
    
      GOSUB GetRadians
    
      'Calculated Heading in Degrees
      brads = Radians     'get angle
      degr = brads */360 'convert to degrees
      
    
    Lcdout $fe, $C0, "Degrees =",SDEC degr
    
    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]
    
        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
    
    GetRadians:
    'The output range of the HMC5883L is -2048 to 2047 and the input range of the ATN function
    'is -127 to 127.
    
    x = readx >>4  
    Y = readY >>4
    Z = readZ >>4
    
       'Lcdout $fe, 1 ' Clear LCD screen
       'Lcdout "X=",SDEC X  ,"   Y=",SDEC Y 
       'Lcdout $fe, $C0, "Z=",SDEC Z
    
    Radians = x ATN -y
    
    Lcdout $fe, 1 ' Clear LCD screen 
    Lcdout "Radians =",SDEC Radians 
    
    RETURN
    
    GetHeading:
    'Gives a heading in North, South, East and West.
    
        IF degr = 90 THEN
        'Lcdout "North"
        ENDIF
    
        IF degr = 0 THEN
        'Lcdout "East"
        ENDIF
    
        IF degr = 270 THEN
        'Lcdout "South"
        ENDIF
        
        IF degr = 180 THEN
        'Lcdout "West"
        ENDIF
    
    RETURN

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Andy, you still have North and East reversed and South and West reversed as well, North is 0, East is 90, South is 180 and West is 270.
    http://commons.wikimedia.org/wiki/File:Compass_Card.png Although I guess you'd be better off saying greater than 315 or less than 45 it's North.

    George

    BTW I'm trying and failing to get a 5983 (supposedly updated version) working with little or no success, I think I killed it!

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


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi George

    I think the reversal on the last bit of the code is to do with headings and angles etc.. So not all that worried.
    Is the 5983 the same layout as mine... If so will the code not crow bar into it ?

    Am also not to sure about gain settings etc.... Have left mine at default.

    BR
    Andy

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    My understanding (perhaps wrong) is that 5983 is an improved version of 5883. It is temperature compensated and will output at a greater rate appearently. I'm working on the assumption that it is software compatible - but you know what they say about assume.

    George

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    I can't edit my last post and I wanted to add

    My understanding is that you want the gain as high as possible without saturating. X,Y and Z values are in the range -2048 to 2047 so I guess you're looking for something like -1500 to 1500.

    I see you don't allow for declination, the difference between magnetic and true north. You can get your local declination from http://www.magnetic-declination.com/

    I'm assuming the the X coordinate points North, although now I think of it I can't see why. the only reference I can find is "Arrow indicates direction of magnetic field that generates a positive output reading in Normal Measurement configuration" take from that what you will.

    George

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


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Hi George

    I agree with gain... Reading the datasheet seems to be way to go ... If it over saturates you get the -4096 figure.

    I also am assuming that X is north... Certainly shows like that at in most of places I have looked.

    Y seems to be either West (left) or East (right).... depending on what you look at... I did find a useful Honeywell document yesterday, will post a little later on.

    There is certainly a shortage of info around as to how to use these devices with PicBasic

    BR
    Andy

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: HMC5883L Magnetometer chip Possibilities.

    Are you using one of the break-out boards, GY0281 or GY282? Could you post a circuit diagram? The two break-out boards above both have 3.3v regulators on them, I'm curious as to what Vcc you run the PIC at and if you use level changers. If X is North Y must be West. 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