Changing the address of I2C in compass?

Discussion in 'Embedded Systems and Microcontrollers' started by SaraSa, Feb 23, 2013.

  1. SaraSa

    Thread Starter Member

    Feb 11, 2009
    25
    0
    Dear friends,
    I have a CMPS03 and I want to change the address.
    I have read the related link, but whatever I wrie the program, I could not get any reply!
    the link is below:
    http://www.robot-electronics.co.uk/htm/cmps3tech.htm

    the last idea was below, but it was not effective:
    %%%%%%

    I2caddr = &HC0
    I2cstart
    I2creg = 12
    I2csend I2caddr , I2creg
    I2creceive I2caddr , I2cdata
    'I2cstop

    I2cstart
    I2creg = 13
    I2csend I2caddr , I2creg
    I2creceive I2caddr , I2cdata
    ' I2cstop


    I2creg = 14
    I2csend I2caddr , I2creg
    I2creceive I2caddr , I2cdata
    ' I2cstop

    I2creg = 15
    I2csend I2caddr , I2creg
    I2creceive I2caddr , I2cdata
    ' I2cstop



    I2caddr = &HC2
    I2cstart
    I2creg = 2
    I2csend I2caddr , I2creg
    I2creceive I2caddr , I2cdata
    I2cstop


    %%%%%%
    Any help will be appreciated.
    Best Regards,
    Sara
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I don't see any declaration for I2cdata...

    ...also, thank you for being a person that provides a link!:D
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    I don't recognise what compiler this may be so I have to guess how these library calls work. So I'll give it in psudo calls and you'll have to stuff back into your library code.

    First, we need to address this thing at the address it is at, and that may have changed... but assume it is at the initial 0xC0 and not 0xC2, or 0xC4, or 0xC6, or 0xC8, or 0xCA, or 0xCC or even 0xCE.

    You first send the address, the register, then the data. Registers auto increment from the first setting. After each data word you send the ACK bit needs to be checked (low for true)... I have no idea if your lib calls do this.

    SendSTART;
    SendData 0xC0; // slave address
    CheckAck;
    SendData 0xc0; // register 12(=C)
    CheckAck;
    SendData 0xA0; // unlock code one
    CheckAck;
    SendData 0xAA; // unlock code two
    CheckAck;
    SendData 0xA5; // unlock code three
    Check Ack;
    SendData 0xC8; // New I2C Address... using C8 here
    SendSTOP;


    (I agree, putting a link there is most kind of you.)
     
    SaraSa likes this.
  4. SaraSa

    Thread Starter Member

    Feb 11, 2009
    25
    0
    Please tell me, Is the below lines are effective or not?
    I work with BASCOM (in Basic Language)
    Did you mean these programs?

    %%%%%

    I2cstart
    I2caddr = &HC0
    Ack
    I2creg = 12
    I2csend I2caddr , I2creg , Ack

    I2caddr = &HA0
    I2creg = 13
    I2csend I2caddr , I2creg , Ack

    I2caddr = &HAA
    I2creg = 14
    I2csend I2caddr , I2creg , Ack

    I2caddr = &HA5
    I2creg = 15
    I2csend I2caddr , I2creg , Ack

    I2caddr = &HC8
    I2creceive I2caddr , I2cdata
    I2cstop

    %%%%%%%%%


    Another question is that:
    IS not necessary to use Ack and Nack together??

    Any help will be appreciated.
    Best Regards,
    Sara
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    As it says in the data sheet "You can do this in one transaction, setting the register address to 12 and writing the four bytes. The internal register pointer is incremented automatically." So my code is attempting to do just that (meaning I've worked plenty with I2C but never with this particular compiler). One transaction means we send everything between one start and one stop signals:

    Code ( (Unknown Language)):
    1. I2CSTART
    2. I2CWBYTE 0xC0 ' slave address
    3. I2CWBYTE 0xC0 ' register 12(=C)
    4. I2CWBYTE 0xA0 ' unlock code one
    5. I2CWBYTE 0xAA ' unlock code two
    6. I2CWBYTE 0xA5 ' unlock code three
    7. I2CWBYTE 0xC8 ' New I2C Address... using C8 here
    8. I2CSTOP
    9.  
    That's it, if I understood your compiler's help file that should be all the code to set the address from C0 to C8.
     
    SaraSa likes this.
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    My previous post has a HUGE error: I set the register address incorrectly, so the unlock code and new address will not work.

    Code ( (Unknown Language)):
    1. I2CSTART
    2. I2CWBYTE 0xC0 ' slave address       <---(see note below)
    3. I2CWBYTE 0x0C ' register 12(=0x0C) [B] <--- line was in error!!![/B]
    4. I2CWBYTE 0xA0 ' unlock code one
    5. I2CWBYTE 0xAA ' unlock code two
    6. I2CWBYTE 0xA5 ' unlock code three
    7. I2CWBYTE 0xC8 ' New I2C Address... using C8 here
    8. I2CSTOP
    9.  
    Note: if the address was ever successfully changed then the new address must be used here and for all other transactions.

    Also note that an I2C device will ACK each byte of data when it is properly addresses. An oscilloscope can easily see this. If you don't have a scope you can take advantage of I2C working at any frequency, so you can slow it down and even pause the lines until you could even use a pair of LEDs to watch the bytes go by.
     
  7. SaraSa

    Thread Starter Member

    Feb 11, 2009
    25
    0
    Thanks alot.it was really helpful.
    we are very happy.
    best regards...:)
     
Loading...