Shop OBEX P1 Docs P2 Docs Learn Events
Help with I2C please — Parallax Forums

Help with I2C please

basicstampedebasicstampede Posts: 214
edited 2008-09-22 19:11 in General Discussion
I'm trying to use MCP23017 i/o expander with I2C with SX/B.

I think there is something wrong with my main code where I read the switches connected to PORTB of MCP23017.

Main:
· ' main code here
· 'read switches on GPIOB
· Reg = gpiob
· I2CSTART SDA
· I2CSEND SDA, Wr_23017, b
· I2CSEND SDA, Reg, b
· I2CSTART SDA
· I2CSEND SDA, Rd_23017, b
· I2CRECV SDA, i2cData,·1··········· 'I've tried both 1 and 0, neither works.
· inVal = i2cData

If I comment above statement, then the following statement works

'output value read to LED
· Reg = OLATA
· i2cData = inVal··· 'works if I·comment out·section above and use i2cData = %11001010 for example
· I2CSTART SDA
· I2CSEND SDA, Wr_23017, b
· I2CSEND SDA, Reg, b
· I2CSEND SDA, i2cData, b
· I2CSTOP SDA
Can someone please tell me what is wrong?

I attach my file here.

Comments

  • BeanBean Posts: 8,129
    edited 2008-09-21 12:49
    I don't see anything at first glance that is wrong with your code. But I've never used the MCP23017.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com

    ·
  • JonnyMacJonnyMac Posts: 9,393
    edited 2008-09-21 14:43
    I've not used that chip either, but I have used the MCP23016 with the BS2p series; perhaps something in this article will help you:

    www.parallax.com/Portals/0/Downloads/docs/cols/nv/vol5/col/nv109.pdf

    You'll also want to keep in mind that I2C functions are time-oriented (like PAUSE, SERIN, SEROUT) so they can chew through a lot of your code space -- this means it's best to encapsulate them in custom subroutines and functions. I've attached an I2C template with code I use in my projects. I've also attached an update template (drop into your \templates folder) as the one you're using is quite old.
  • ZootZoot Posts: 2,227
    edited 2008-09-21 16:19
    It's not clear from the datasheet if this device clock-stretches -- if it does, then the I2C commands in SX/B 1.53.x will not work. Seems like if the device *did* clock-stretch, it would be mentioned in the datasheet, but since clock-stretching is part of the official I2C spec, I guess they wouldn't have to specify, as it could be presumed.

    Also, in Jon's mention of the similar device, he shows pulling the (unused) SPI clock line low when using I2C. Also for reasons that are unclear, the datasheet for your device shows 1k pullups for SDA/SCL (kinda low, but not unheard of). Do you have the reset pin of the device pulled high? Just throwing out ideas here....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-22 01:08
    All, thanks! I found my error. And a stupid error it was indeed. I had forgotten I2CSTOP SDA. With that added, it worked just fine. I found the Debugger very useful in helping find the mistake. Using debugger, I could see that value read was OK, but it was not getting written.

    Jonny Mac, thans for the files. I will study them. YES, I was shocked to see how much memory such a paltry code of mine used up. I definitely need to examine ways to make it more efficient, memory wise.
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-22 01:08
    All, thanks! I found my error. And a stupid error it was indeed. I had forgotten I2CSTOP SDA. With that added, it worked just fine. I found the Debugger very useful in helping find the mistake. Using debugger, I could see that value read was OK, but it was not getting written.

    Jonny Mac, thans for the files. I will study them. YES, I was shocked to see how much memory such a paltry code of mine used up. I definitely need to examine ways to make it more efficient, memory wise.
  • JonnyMacJonnyMac Posts: 9,393
    edited 2008-09-22 01:19
    Remember, SX/B compiles in place so it is up to you to optimize as you desire by wrapping "big" SX/B instructions into custom subroutines and functions. By compiling in place SX/B lets us "look under the hood" and learn how BASIC is translated to Assembly.
  • basicstampedebasicstampede Posts: 214
    edited 2008-09-22 19:11
    Jonny Mac, in your code i2c_template.sxb above, there is a statement (comment actually)

    'Use: byteVal = I2C_IN AckBit
    What is AckBit? Is it a global variable?
    For example, if I want to receive a byte from I2C device then give NAK to device, is this correct usage:

    byteVal = I2C_IN Nak

    If my assumption is wrong, can you please give an example?

    - - - - -

    For I2C_OUT, is this correct usage?

    ackNak = 0 'want Ack
    I2C_OUT byVal
    IF ackNak = 1 THEN.... (check to see if ACK is correctly received, OPTIONAL)

    Thanks.
Sign In or Register to comment.