Shop OBEX P1 Docs P2 Docs Learn Events
HMC5883l compass output — Parallax Forums

HMC5883l compass output

AGCBAGCB Posts: 327
edited 2013-10-28 10:19 in Accessories
I've been fighting w/ a HMC5883L Parallax #29133 compass module for a month. I've made
it read out the 6 registers (I think) but when I convert the 16 bit 2s compliment value to decimal,
I get way too high numbers. Like X= +65282 Y= -65535 Z= -39167. It's probably that I am
doing the conversion wrong. Although I am using a PIC 18F MC and assembly code and do not
understand the sample codes in Spin or BS2, I could write the code if I could understand the
basics of doing the math and conversions.
From what I have learned on the web, this is the way I am treating the register values
that I get from the compass.
It is a 16 bit 2s compliment value so I test the Msb bit 7 for sign (1 if negative 0 if +) and save it.
Then I compliment both registers and add 1 to the Lsb, if there is a carry, I also add 1 to
the Msb. Now having a 2 byte binary number, I convert it to decimal and add the sign.
If this is wrong, please tell me.
Page 15 of the Honeywell documentation is not easy for me to understand. It says the
range is 0xF800(decimal 63,488) to 0x07FF (decimal 2,047). These numbers are way higher
than what I expect them to be. Also I believe that the -65535 decimal that I'm getting for Y is the
error code -4096. Even this -4096 I do not understand. Is it decimal (I assume) or hex?
WHAT am I doing wrong?? Without some help, I'm about ready to give this up. Thanks
Aaron, new to this forum but been programing PICs for 4 or 5 years of winter months.

Comments

  • jazzedjazzed Posts: 11,803
    edited 2013-10-24 09:09
    If you know C, maybe the SimpleIDE Learn compass library can help.

    The C code is on-line here: https://code.google.com/p/propsideworkspace/source/browse/#hg%2FLearn%2FSimple%20Libraries%2FSensor%2Flibcompass3d

    The entire Learn Library can be downloaded from here: http://learn.parallax.com/node/658
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-24 14:59
    I don't completely understand 2s compliment stuff myself but what if you output the binary values and post those. Then we can figure out if you're converting to 2s compliment incorrectly (my suspicion) or if there's some other issue.
  • AGCBAGCB Posts: 327
    edited 2013-10-24 15:14
    jazzed wrote: »
    If you know C,
    I don't know C. Duane said " what if you output the binary values and post those." Are you saying Post the raw values or my 2s comp values converted to plain binary. I can do either one or both. Give me a little time though. Thanks. Aaron
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-24 15:28
    AGCB wrote: »
    Are you saying Post the raw values or my 2s comp values converted to plain binary.

    I was thinking the raw values and what your program generates as their decimal equivalents. This will let others see if you're converting from the raw value correctly.
  • AGCBAGCB Posts: 327
    edited 2013-10-24 16:16
    XHI always=00000000; XLO always=00000000; ZHI varies, some samples 10000011, 00110110, 00000110; YHI always = 11111111; YLO always = 11111111 a sample of my conversion of ZHI,ZLO
    ZHI       ZLO
      
      10000011  11111111  ZHI bit 7 is 1 so result will be negative
    complment= 01111100  00000000
    add 1=  01111100  00000001 =  16 bit binary
    = decimal -31745  after sign is put in
    
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-10-24 16:48
    Your two's compliment calculation looks fine.

    I don't understand this part of your post:
    AGCB wrote: »
    XHI always=00000000; XLO always=00000000;

    Is the x component magnetic field always zero? If so then there's something wrong with the way you're either reading the data from the sensor or there's a problem with your connections between the microcontroller and the sensor.

    I've used a HMC5883L sensor but I didn't write the low level code to read from the sensor so I'll need to let someone else help you if they can.
  • AGCBAGCB Posts: 327
    edited 2013-10-24 17:26
    Is the x component magnetic field always zero

    Yes. the X registers are always 0 and the Y registers are always 65535
  • AGCBAGCB Posts: 327
    edited 2013-10-24 17:28
    Except for Parallax users, this device doesn't seem to be very popular
  • edited 2013-10-28 10:19
    This thread has been moved to the Sensors forum.
Sign In or Register to comment.