I2C Comm with Digital Compass HMC6352


Closed Thread
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Mar 2008
    Posts
    5

    Default I2C Comm with Digital Compass HMC6352

    This is my first post to the group.

    I'm designing a heading hold autopilot for a free flight glider. I'm basing it on a PIC16F88, and using an HMC6352 digital compass (http://www.sparkfun.com/datasheets/C...ts/HMC6352.pdf).

    I've never played with I2C before, and figured that I would try something simple first, like reading an EEPROM register in the compass module. My problem is that no matter what I do, I seem to always read back a value that is exactly 1/2 of what it should be.

    So then I tried writting a value to a RAM address and reading it back. Again, same issue, the value read back is 1/2 the value written.

    I'm using PIC BASIC PRO, on a LABX18 development board. Here's the code I'm using for the EEPROM read, which is modified from some of the example programs:
    Code:
    Define  LCD_DREG        PORTA
    Define  LCD_DBIT        0
    Define  LCD_RSREG       PORTB
    Define  LCD_RSBIT       6
    Define  LCD_EREG        PORTB
    Define  LCD_EBIT        3
    
    ' Define I2C Interface
    
    SDA    Var    PORTB.1                    ' Data pin
    SCL    Var    PORTB.4                    ' Clock pin
    
    cmd  Var Byte
    addr Var Byte
    cont Var Byte
    
        ANSEL = 0                      ' PORTA is digital
        CMCON = 7                      ' PORTA is digital
    
        Pause 100                    ' Wait for LCD to start up
            
    loop1:
        pauseus 1000
        cmd = $72                    ' Read from compass EEPROM
        addr= $00                    ' EPROM address $00
        I2CWRITE SDA, SCL, $42, [cmd, addr], nowrite
    
        pauseus 1000
         I2CREAD SDA, SCL, $42, [cont], noread
        Lcdout $fe, 1, "Data: ", cont
        
        goto loop1
        
        End
    
    nowrite:
        Lcdout $fe, 1, "Write Err"
        goto nowrite
    
    noread:
        Lcdout $fe, 1, "Read Err"
        goto noread
    If anyone can point me to the error of my ways, it would be greatly appreciated.

    John Ihlein
    Albuqueruqe, NM
    Last edited by ScaleRobotics; - 26th May 2010 at 17:07. Reason: code brackets added

  2. #2
    skimask's Avatar
    skimask Guest

    Default

    I'm fairly you don't have the I2C command syntax's correct.
    Double check the PBP manual and see what you come up with.

  3. #3
    Join Date
    Mar 2008
    Posts
    5

    Default

    Skimask,

    The manual shows the addr parameter as option. Since this isn't an EEPROM, and from looking at the HMC6352 data sheet, I figured that the commands should be sent as data arguements.

    I can try sending what I'm calling cmd as the address argument:

    I2CWRITE SDA, SCL, $42, [cmd, addr], nowrite

    becomes

    I2CWRITE, SDA, SCL, $42, cmd, [addr], nowrite

    later today. It does seem like my write commands are somewhat working, however. If I change the commands to write and read RAM rather than just read EEPROM, I can see the RAM data change with the read command, it's just off by 1/2.

    Looking at how the module responds to commands, I'm not sure how I would restructure the I2CREAD command. Maybe it needs a dummy addr argument??

    At the moment I can't tell if the issue is on the write or the read side, but since I've tried to read known values from EEPROM and they are off by 1/2, I tend to think the read side is where my problem lies. I'm wondering if it's a data type issue, although the documentation says the data will be transmitted in bytes.

    Thanks,

    John

  4. #4
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by jihlein View Post
    The manual shows the addr parameter as option. Since this isn't an EEPROM, and from looking at the HMC6352 data sheet, I figured that the commands should be sent as data arguements.
    Yep, I got that.
    But I still don't think they're right for what you want.
    And is the module getting the 70uSec delay it requires after requesting the data from the eeprom?

  5. #5
    Join Date
    Mar 2008
    Location
    Texas, USA
    Posts
    114

    Default

    Values that come in as 1/2 what they should have been shifted right one bit. You are loosing the LSB somewhere? I don't know the in's and out's of exactly what PBP does when shifting in I2C, but it might be that PBP is expecting one more bit. Does that compass send out 8 bits... or does it send out 7? Maybe it's sending out 9?

    Anywho, it's a byte shifted right... some where, some how.

    Does this from the datsheet have anything to do with your problem?

    "Negative binary values will be in two’s complement form"
    Last edited by JD123; - 13th March 2008 at 17:25.

  6. #6
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by JD123 View Post
    Values that come in as 1/2 what they should have been shifted right one bit.
    Or maybe...
    North 360 / 2 = 180 South ..... 1/2 the value
    Are those values always exactly 1/2? Or are they just close to 1/2...

  7. #7
    Join Date
    Mar 2008
    Posts
    5

    Default

    It sure does look like a byte shift. For reading EEPROM/RAM locations, I believe it is just sending back an 8 bit byte. If you choose to read raw magnetometer data rather than computed heading, that's where the 2's complement comes into play. I haven't got that far yet, and FWIW, plan to read computed heading. Thought it would be a good simple test to just try to read an write some simple values to the unit before trying to take measurements....

    The delay I put in is consdierbaly longer than the delays mentioned in the data sheet. From my understanding og the I2C protocol, the extra wait time should not be a factor. Whether or not it "bugs" the compass processing I can't see. Can reduce the wait time to the recommended 70 usecs and see if that changes anything.

    If else fails, I'll take it to work and watch the transaction on a digital storage scope and see what's occuring.

Similar Threads

  1. I2C Master/Slave 16F88/16F767 working code
    By DanPBP in forum Code Examples
    Replies: 2
    Last Post: - 23rd October 2012, 22:31
  2. HARDWARE I2C SAMPLE CODE question
    By Michael Wakileh in forum Code Examples
    Replies: 2
    Last Post: - 16th June 2009, 21:07
  3. I2C Master Slave issues.
    By cpayne in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th March 2008, 19:33
  4. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21
  5. Digital Compass Project
    By BJSmith in forum Serial
    Replies: 2
    Last Post: - 7th April 2006, 19:32

Members who have read this thread : 1

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

Posting Permissions

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