Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Compass module and pic asm code

Status
Not open for further replies.

AGCB

Member
I've searched this forum and the web in general but can't seem to find any examples of PIC asm code for the Honeywell 3 axis compass module. I'm just getting started with this chip and the only code that's readily available is for the basic stamp. Just looking for some snippets or code segments. I use 18F but can convert from 16F. If you've done something yourself or seen something, please let me know.

Thanks, Aaron
 
Thanks. That is a nice little chip.

The interface is apparently I2C. You will be reading two, 8-bit numbers and then doing what ever you need to them. Awhile back, I put up a blog in which I read an accelerometer's 16-bit data, and then used a table to calculate what I wanted (inclination). I did not deal with an I2C, and obviously, it is not exactly what you are doing. But, it is in MPASM, if that helps. Here's the link: https://www.electro-tech-online.com/blog-entries/memsic-inclinometer-assembly-language.254/ (NB: The links to Memsic don't work anymore, at least not when I last tried them.) Parallax sells a similar device based on a Memsic accelerometer

John

Edit: Also noted some of the special characters are screwed up in the blog post, but the content is probably still clear. I give a reference to the Parallax device therein.
 
Thanks. That is a nice little chip.

The interface is apparently I2C. You will be reading two, 8-bit numbers and then doing what ever you need to them. Awhile back, I put up a blog in which I read an accelerometer's 16-bit data, and then used a table to calculate what I wanted (inclination). I did not deal with an I2C, and obviously, it is not exactly what you are doing. But, it is in MPASM, if that helps. Here's the link: https://www.electro-tech-online.com/blog-entries/memsic-inclinometer-assembly-language.254/ (NB: The links to Memsic don't work anymore, at least not when I last tried them.) Parallax sells a similar device based on a Memsic accelerometer

John

Edit: Also noted some of the special characters are screwed up in the blog post, but the content is probably still clear. I give a reference to the Parallax device therein.
Thanks John. I'll study that out and see if I can figure it out.
Aaron
 
Post deleted so as not to confuse anyone or waste their time. See posts hear after. Thanks. Aaron
 
Last edited:
I've made progress this morning!

I removed the restart and pointing to next register in the compass plus some cleanup and am now getting X, Y, and Z numbers on the LCD And it keeps looping w/o stopping!

Latest code
Code:
;================ Main loop==================================
 
Main
    call    hello             
    call    beep
    call    del_1s
    call    lcd_clr 
read    ;loop 
    btg    porta,5     
    call    READ_COMPASS 
    movff    XHI,numh
    movff    XLO,numl
    putstr    "X =  "
    call    convert
    call    LCD_Decimal
    call    Del_2S
    call    lcd_clr     
 
    putstr    "Z =  "
    movff    ZHI,numh
    movff    ZLO,numl
    call    convert
    call    LCD_Decimal
    call    Del_2S
    call    lcd_clr 
 
    putstr    "Y =  "
    movff    YHI,numh
    movff    YLO,numl
    call    convert
    call    LCD_Decimal
    call    Del_2S
    call    lcd_clr 
 
    goto    read
 
READ_COMPASS        ;SINGLE-Measurement mode                 
    call    beep
    call    s_start
    MOVLW    slave_write            ;0x3c  control in (write)
    CALL    send_i2c_byte 
    call    chk_ack
 
    movlw    0x02        ;Mode register
    call    send_i2c_byte
    call    CHK_ACK
 
    movlw    0x01        ;(single-measurement mode)
    call    send_i2c_byte
    call    CHK_ACK
 
    delaycy (100*msecs)
 
    call    s_restart
    movlw    slave_write            ;0x3c  control in (write)
    call    send_i2c_byte
    call    chk_ack 
;read Xhi 
    movlw    XHIREG        ;point to XHI register     
    call    send_i2c_byte
    call    CHK_ACK         
 
;restart and send control byte (READ)
    call    s_restart
    movlw    slave_read            ;0x3d  control out (read) 
    call    SEND_I2C_BYTE 
    call    chk_ack 
 
;Configure MSSP for reading
    bsf    sspcon2,rcen        ;enable i2c read
    call    waitMSSP        ;waits for the last I2C operation to complete
    bcf    sspcon2,ackdt
    bsf    sspcon2,acken        ;send ack to slave
 
    movff    sspbuf,XHI
    delaycy (10*msecs) 
 
;Configure MSSP for reading
    bsf    sspcon2,rcen        ;enable i2c read
    call    waitMSSP        ;waits for the last I2C operation to complete
    bcf    sspcon2,ackdt
    bsf    sspcon2,acken        ;send ack to slave
;    bsf    porta,1
    movff    sspbuf,XLO
;read ZHI 
    delaycy (10*msecs)
 
;Configure MSSP for reading
    bsf    sspcon2,rcen        ;enable i2c read
    call    waitMSSP        ;waits for the last I2C operation to complete
    bcf    sspcon2,ackdt
    bsf    sspcon2,acken        ;send ack to slave
 
    movff    sspbuf,ZHI
 
;read ZLO 
    delaycy (10*msecs)
 
;Configure MSSP for reading
    bsf    sspcon2,rcen        ;enable i2c read
    call    waitMSSP        ;waits for the last I2C operation to complete
    bcf    sspcon2,ackdt
    bsf    sspcon2,acken        ;send ack to slave
 
    movff    sspbuf,ZLO
 
;read YHI 
    delaycy (10*msecs)
 
;Configure MSSP for reading
    bsf    sspcon2,rcen        ;enable i2c read
    call    waitMSSP        ;waits for the last I2C operation to complete
    bcf    sspcon2,ackdt
    bsf    sspcon2,acken        ;send ack to slave
 
    movff    sspbuf,YHI
 
;Configure MSSP for reading
    bsf    sspcon2,rcen        ;enable i2c read
    call    waitMSSP        ;waits for the last I2C operation to complete
 
    bsf    sspcon2,acken        ;send ack to slave
 
    movff    sspbuf,YLO
    call    S_STOP 
    return

The compass data sheet says that the x, y and z values are 16 bit values in 2s compliment. I'm using a convert routine which takes a hi 8 bit value and a lo 8 bit value and converts them to a 5 digit decimal. This may not be what is needed. What is a "16 bit value in 2s compliment" ?

The X value is either 0 or 65535 , the Z value changes as the compass is rotated and the Y value is always 65535.

Aaron
 
I looked into google on 2s compliment. I now know that my previous calcs were wrong and that it will take much more study. Taking shop math in high school allowed me to learn much more as an adult/old fart.I'll be back soon! Aaron
 
IS this right?
To convert a 16 bit 2s compliment to 2 8 bit binarys
After determining sign, complment both Msb and Lsb, add 1 to Lsb and if there is a carry add 1 to Msb, convert to hex or decimal and put in sign.

Sound good?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top