Shop OBEX P1 Docs P2 Docs Learn Events
Change "shift" to "I2c" commends for stamp code of MMA7455L accelerometer — Parallax Forums

Change "shift" to "I2c" commends for stamp code of MMA7455L accelerometer

cheolcheol Posts: 6
edited 2011-11-27 15:39 in BASIC Stamp
Hi,
I got the example BASIC stamp code for MMA7455L 3 axis acceleromer sensor from Parallax store, which uses "Shift" commend to read and write into the sensor. Can anyone advise and comment (or modify) how I can change the code using "I2C" instead of "Shift"? I have Mobo board having BS2pe, which allow to use "I2C" commend, and I have experienced that "I2C" seems more simpler and straightforward. I am attaching the original codes and data sheet.
Thanks,
CalebMMA7455L 3-Axis Accelerometer Demo (PBasic) v1.0 DEMO#2.bs2FreescaleMMA7455LDeviceDocumentation.pdf

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-14 10:43
    Unless you're really set on using I2C, I would suggest you stick with SPI (shift) mode. The I2C mode is slower and actually more complex. It's main advantage is when you want to attach several devices to the same two I/O pins.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-14 11:01
    The only parts of the demo program that would have to change are DataIn and DataOut. Note the comments below
    DataOut:
    LOW CSPin                                              'Pull chip select pin low to start transmission
    SHIFTOUT DATAPin, CLKPin, MSBFIRST, [(Address|%1000000)<<1] 'Select register Address
    SHIFTOUT DATAPin, CLKPin, MSBFIRST, [SendData]         'Write value to Address
    HIGH CSPin                                             'End transmission
    ' For use with I2C mode, you'd have
    ' I2COUT DATAPin, $36, Address, [SendData]
    ' Note that DATAPin has to be I/O pin 0 or 8 with CLKPin as the next I/O pin (1 or 9)
    ' The CSPin has to be tied to 5V for I2C mode to work
    RETURN
    
    DataIn:
    LOW CSPin                                              'Pull chip select pin low to start transmission
    SHIFTOUT DATAPin, CLKPin, MSBFIRST, [Address<<1]       'Select register Address
    SHIFTIN DATAPin, CLKPin, MSBPRE, [ReceiveData]         'Read value from Address
    HIGH CSPin                                             'End transmission
    ' For use with I2C mode, you'd have
    ' I2CIN DATAPin, $36, Address, [ReceiveData]
    ' Note that DATAPin has to be I/O pin 0 or 8 with CLKPin as the next I/O pin (1 or 9)
    ' The CSPin has to be tied to 5V for I2C mode to work
    RETURN
    
    Follow the schematic given in the MMA7455L's Parallax documentation for I2C use.
  • cheolcheol Posts: 6
    edited 2011-11-14 22:41
    Hi, Mike;
    Thanks so much for the comment and code. it works! Somehow I couldn't make it work with shift...but it works with I2C.
    Regards,
    C
  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-15 08:21
    You didn't mention that the demo program didn't work for you. It should have. You may have wired it wrong. Since the I2C version works for you, there's no point in going back, but keep in mind:

    1) Parallax's demo programs are usually well tested. There have been occasional bugs, but they're rare. Double check your wiring.

    2) It always helps to provide more information when posting a question or complaint on the forums. Include listings of your actual code and drawings of your wiring as attachments.
  • mdelaney14mdelaney14 Posts: 1
    edited 2011-11-27 15:39
    Mike, Just read your thread with cheol on "changing shift to I2C commands".
    I just bought the new compass chip (HMC5883L) to use with my plain old BS2. I tried loading the [EMAIL="29133-CompassBS@Demo"]29133-CompassBS2Demo[/EMAIL] from the product page.
    I get errors from the I2C commands.
    Do I understand correctly that I have to change the I2C commands to shift commands for this to work with the BS2?
    Can you give me an example?
    This is just the opposite of the problem that cheol is having!!
    Thanks,
    MD
Sign In or Register to comment.