Shop OBEX P1 Docs P2 Docs Learn Events
MCP23S17 I/O Expansion — Parallax Forums

MCP23S17 I/O Expansion

anti-heroanti-hero Posts: 8
edited 2010-07-24 00:42 in BASIC Stamp
I've been trying to get the MCP23S17 working. Just trying to turn on LED's for now, but notta lotta success!
Here's my code:

CS PIN 0 'chip select pin 11 MCP23S17
SCK PIN 1 'serial clock pin 12 MCP23S17
SI PIN 2 'serial in pin 13 MCP23S17
SO PIN 3 'serial out pin 14 MCP23S17

LOW CS 'opcode,register,data
SHIFTOUT SI,SCK,MSBFIRST, [noparse][[/noparse]%01000000,$00,%00000000] 'make 'em all outputs IODIRA register
HIGH CS
LOW CS 'opcode,register,data
SHIFTOUT SI,SCK,MSBFIRST, [noparse][[/noparse]%01000000,$0A,%01010000] 'setup IOCON register
HIGH CS
LOW CS 'opcode,register,data
SHIFTOUT SI,SCK,MSBFIRST, [noparse][[/noparse]%01000000,$12,%11111111] 'turn on outputs GPIOA register
HIGH CS

Any idea why its not workin?
Thanks in advance.........

Comments

  • G McMurryG McMurry Posts: 134
    edited 2010-07-17 19:08
    A few people have written to me about my use of the MCP2317 so I am posting some of the code I used.

    I had some success using the MCP23S17 but subsequently I abandoned it, updated to the BS2P which is not only faster but has some built in LCD stuff.

    I wanted to find a way to write to an LCD with fewer IO lines in one of my BASIC STAMP projects. I stumbled across a board on EBAY that used the MCP23S17 and thought it would be a good start. The supplier of the board indicated that it could be used with the Basic Stamp and included sample code. It could but the sample code it came with was NOT for the Basic Stamp. Oh well. I had to figure it out myself.

    I got it all working but it was really slow with all the MCP23S17 overhead. I don't use it any longer. For normal IO stuff that is not speed sensitive, I would be fine.

    My application was a speedometer for use on my "rideable" 1/6 scale locomotive. This is the scale that is used commonly in the "Live Steam" hobby. I gather a variety of data from my locomotive and display it on the "Engineers Control Panel"

    The card would receive a string of serial commands and/or directly sample various pieces of data and display them on a 2 X 16 Backlit LCD in my handheld contrller.

    The heart of the program is the part that sends data to the 23s17 and is clearly annotated. Sorry for any stupid typos. I did not try to clean this code up for anyone... It is just the notes I took for myself when I wrote it.

    There is a section where I wrote MPH VAC AMP PSI to the display. These were static labels for the values I wanted to display.

    I believe in this version I used the sub routing speedo_raw to count pulses on a sprocket to determine MPH on my train. It took too long to write to the display and do the COUNT function so I moved on to another method of writing to the LCD. It doesn't look like I ever implemented the serin comands for the other data. Clearly I ran out of CPU time at this point and moved on to other methods.

    HOWEVER the code is attached for anyone that is interested. This was a while ago and I am not sure I can answer questions intelligently, but I will try.

    Greg

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    AUTOMATE EVERYTHING
    http://www.trainyard.net
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-07-17 22:26
    I am also having problems using the MCP23S17...

    I hooked up 8 LEDs using 470 ohm resistors on each pin on Port B and wrote a program to alternate lighting every other LED.

    It works for a while and then just stops. The program continues to loop but the MCP23S17·does not respond.

    I tried a different MCP23S17 IC, and it acts the same.

    I am using a 9 volt 1 amp power supply so it's not a battery issue. The chip does not get warm and I'm sure each LED is drawing less than 25mA, so I can't figure out what is going on. Using OLATB gives the same result as using GPIOB

    I have used a MCP23016 (I2C) and had no problem.

    Here is my code - any help would be appreciated!!
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    'GPB0 -> GPB7 PINS 1-8      LED (via 470)
    'VDD(+5v)     PIN  9        NOTE: use 0.1 uf decoupling cap
    'VSS          PIN  10
    'CS           PIN  11
    'SCK          PIN  12
    'SI           PIN  13
    'SO           PIN  14
    'A0, A1, A2   PINs 15,16,17 TO VSS TO set address 000
    ' -----[noparse][[/noparse] Revision History ]------------------------------------------------
    
    ' -----[noparse][[/noparse] I/O Definitions ]-------------------------------------------------
    CS             PIN     0                       ' SPI Chip Select
    SCK            PIN     1                       ' SPI Serial Clock input
    SI             PIN     2                       ' SPI Serial data input
    SO             PIN     3                       ' SPI Serial data output
    ' -----[noparse][[/noparse] Constants ]-------------------------------------------------------
    DevType         CON     %0100 << 4              ' Device type
    DevAddr         CON     %000 << 1               ' address = %000 -> %111
    Wr23S17         CON     DevType | DevAddr       ' write to MCP23S17
    Rd23S17         CON     Wr23S17 | 1             ' read from MCP23S17
    'MCP23S17 register addresses when IOCON.BANK = 0
    IODIRA       CON $00
    IODIRB       CON $01
    IPOLA        CON $02
    IPOLB        CON $03
    GPINTENA     CON $04
    GPINTENB     CON $05
    DEFVALA      CON $06
    DEFVALB      CON $07
    INTCONA      CON $08
    INTCONB      CON $09
    IOCON        CON $0A
    'IOCON2       CON $0B
    GPPUA        CON $0C
    GPPUB        CON $0D
    INTFA        CON $0E
    INTFB        CON $0F
    INTCAPA      CON $10
    INTCAPB      CON $11
    GPIOA        CON $12
    GPIOB        CON $13
    OLATA        CON $14
    OLATB        CON $15
    
    cnt VAR Word
    Setup:
      DEBUG CLS
      DEBUG BIN ? DevType, CR
      DEBUG BIN ? DevAddr, CR
      DEBUG BIN ? Wr23S17, " ", HEX2 Wr23S17, CR
      DEBUG BIN ? Rd23S17, " ", HEX2 Rd23S17, CR
      cnt = 0
      LOW CS
      SHIFTOUT SI,SCK,MSBFIRST,[noparse][[/noparse]Wr23S17,IODIRB,%00000000]  'make PORTB outputs
      HIGH CS
    LOOP1:
      DEBUG CRSRXY, 1,10, DEC cnt
      LOW CS
      SHIFTOUT SI,SCK,MSBFIRST,[noparse][[/noparse]Wr23S17,GPIOB, %01010101]   'set PORTB GPIO pins to 01010101
      HIGH CS
      PAUSE 100
      LOW CS
      SHIFTOUT SI,SCK,MSBFIRST,[noparse][[/noparse]Wr23S17,GPIOB, %10101010]   'set PORTB GPIO pins to 10101010
      HIGH CS
      PAUSE 100
      cnt = cnt + 1
      GOTO LOOP1
     
      END
    
    

    Post Edited (Ron Czapala) : 7/17/2010 10:31:36 PM GMT
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-07-18 03:11
    EDITED:

    I just noticed that rubbing the wooden desktop within a few inches of the breadboard causes the MCP23S17 to quit working.

    The humidity level is not too dry but I wonder if there should be some capacitors on the SI, SO,·SCK, CS pins???

    Video http://www.youtube.com/watch?v=JDQhNROvy8Q

    I've been continuing my testing and it seems to be a timing issue. I even tried using the MCP23017 I2C version with both a BS2 and BS2P Stamp.

    It works for a while and then quits. I am attaching a version which writes to the GPIOB register at intervals of 500, 400 and 300 milliseconds (50 times each). It seems to work OK. I change it to include 200 millisecond interval, it fails.

    I think I'll stick to the MCP23016 -seems more reliable.

    ·NOTE - I switched to different BS2 pins in this version

    Post Edited (Ron Czapala) : 7/18/2010 4:04:26 AM GMT
  • anti-heroanti-hero Posts: 8
    edited 2010-07-19 16:26
    OK, I get the dumb attack of the day award. Thanks for your code for the MCP23S17 I/O expansion. I compared it and it was·identical to what I had that wouldnt work. My test circuit simply had one LED connected to GPA0. Problem was, I had it connected to GPB7! Duh.

    Anyone else using the MCP23S17, be sure to connect pin 18 RESET to +5V if you're not using it externally. Also, pins 15,16,17 connect to GND. I wasn't sure about using the reset but I tested it this way and it works. Datasheets are not always crystal clear about everything!

    SI and SO pins 13 and 14 can be tied together with a 470ohm resistor and use one stamp pin.

    Thanks again for the help....

    ·
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-07-19 16:56
    anti-hero,

    ·· I'm glad you pointed out that the RESET pin needed to be connected.

    I missed the datasheet saying "Hardware reset. Must be externally biased."

    I'm pretty sure this solves my issue with the IC not responding!

    - Ron

    PS.· I found a MicroChip evaluation board document that indicated a .01 uf decoupling cap should be used across the VDD VSS pins.


    Post Edited (Ron Czapala) : 7/19/2010 5:02:04 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2010-07-19 17:15
    As you've discovered, it's very important to not have "floating" ... that is unconnected pins. Often you can just connect the pin to Vdd or Vss, but not always. Read the datasheet for details. Some pins cannot be connected directly to Vdd or Vss and have to have some kind of pullup or pulldown resistor, usually a 10K or 100K value.

    It's also important to have bypass capacitors, usually a 0.1uF ceramic capacitor between Vdd and Vss close to the chip. A well designed board will have one of these for every chip, particularly complex chips. You also need a larger, maybe 10uF tantalum capacitor near where the power enters the board.
  • anti-heroanti-hero Posts: 8
    edited 2010-07-19 17:58
    Thanks again for your time. The datasheet says it must be "externally biased". They need to be more specific. Biased to what? Again, its working fine and I do try to keep everything from floating. I did an interface recently to a Microchip 23K640 SRAM and it was really flaky about noise. I always put an o'scope on and make sure everything is clean and being pulled as close to gnd as possible. The 23K640 runs at about 3 volts and I ended up with 2k to gnd and 1k in series with the stamp output pins.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-07-19 18:23
    Mike,
    You mentioned that SI and SO can be connected together (in another MCP23S17 thread) and anti-hero said he connected them together with a 470 ohm resistor.

    Where did you find that tid-bit? I have seached all over and never found any info like that.

    These forums are a great for researching circuits, sensors, etc

    Thanks!
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-07-22 13:50
    I connected some tact switches to pins GPA0 thru GPA7 and set the internal 100k pull ups on with GPPUA command and inverted the polarity with IPOLA.

    The program reads the GPIOA register to check whether a switch button was pushed. The button on GPA7 was not being detected.

    Reading other register values did not work well either.

    I discovered that using separate connections from the SI and SO pins to the BS2 (rather sharing a pin between SI and SO·with a·resistor) fixed the problem.

    I would NOT RECOMMEND connecting SI and SO to save a BS2 pin.




    Post Edited (Ron Czapala) : 7/22/2010 1:58:27 PM GMT
  • anti-heroanti-hero Posts: 8
    edited 2010-07-23 21:55
    Maybe I wasn't clear about the connection. Come out of the stamp pin directly into SI. Connect one end of the 470 resistor to the SI pin and the other end to SO. One pin aint much, but I've got 5 various chips connected to a BS2 and it works without a glitch. I've done a "DO LOOP" and looked at the squarewaves with a scope and they look perfect. I've played with the input voltage supply, jacked up the ambient temperature, and even hit it with an electromagnetic field in an attempt to induce noise. Seems bulletproof but I'm not entirely sold on it, so experiment away and lemme know whatcha get. I'm doing an automotive application and I dont need it flakin out on me.
  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-07-24 00:42
    anti-hero,
    · I had the resistor connected as you described, but reading registers returned incorrect values.

    I think it is a timing concern trying to do a Shiftin immediately after a Shiftout on the same BS2 pin.

    The following code returned the correct value only after I switched back to separate BS2 pins for SI and SO.
      LOW CS
      SHIFTOUT SI,SCK,MSBFIRST,[noparse][[/noparse]Wr23S17,IPOLA,%11110000]  'invert inputs
      HIGH CS
    

      LOW CS
      SHIFTOUT SI,SCK,MSBFIRST,[noparse][[/noparse]Rd23S17,IPOLA]
      SHIFTIN SO,SCK,MSBPRE,[noparse][[/noparse]regin]
      HIGH CS
      DEBUG "IPOLA: ", BIN8 regin,CR
    
Sign In or Register to comment.