I'm using a i2c expander chip (MCP23008/MCP23017) for my project which works perfectly. I later saw a page on using shift registers to expand the number of ports & started wondering what the actual advantages of a shift register are? From what I can come up with, the i2c expander:
Has a 3 bit address selection ability, so is limited to 8x16 = 128 total outputs/inputs
Is more flexible - can serve as input/output, with interrupts
can address pins individually
uses 2 wires (SDA/SCL) instead of 3
while the shift register:
is nearly infinitely expandable (if speed isn't a concern)
I'm assuming the shift register would probably have less overhead in terms of program space. Is there anything else I'm missing? I've seen a lot more written about using shift registers, but the i2c expanders seem like they would be better/more flexible in most cases.
Your are answering your own question
It is like you wrote.
Shift registers are faster (the shiftOut function can be used), and they can be daisy-chained for massive amounts of pins. But they require a few pins.
If I2C is already in your project, then adding a I2C expander I/O chip doesn't cost any extra pins.
I am not sure about whcih is faster but I suspect the the I2C as you address directly. However the shifts are a lot cheaper to my knowlegde.
As for the expansion, you can always add a 4-16 decoder for example an use these to address the I2C group allowing 16 x 128 pins. But it will have some delay due to the extra chip.
The shifts would need some algorithm to translate the pin to the correct register etc. and then shift everything out.
I2C is much more flexible. You can usually choose INPUT/OUTPUT on a pin by pin basis and adding more devices to an existing circuit is easy (providing there's a free address that your chip can use).
the MCP23008 is I2c the sister chip MCP23s08 is the SPI chip with almost identical pin-out.
the difference is the I2C has 3 address pin. the SPI version has an output for daisy chaining.
with just a minor amount of modification maybe a socket that has a pin altered ? and you can try both in the same circuit.
speed is one difference, favors the shift register
memory space also favors the shift register
with latch, you use more pins on the shift register.
if you are using both input and output, you can save pins by using the I2c
I did a bit of looking and found some shift registers that are designed to be LED drivers, so if that's your goal and you find a chip that matches the characteristics of your LEDs, it would simplify your circuit. I had mine driving a Sainsmart relay board, so I ran it through a ULN2803 darlington driver array just to make sure I didn't max out the current capacity of the MCP23008.
Haven't looked into daisy chaining the SPI version of the MCP chip; it's an interesting thought though.
I looked up the specs on the SPI interface; the default clock speed is 4 MHz, or 40x the 100KHz i2c speed. Maximum speed on the Uno/Mega would be 8MHz, but it depends on the maximum speed of the chips you're using. The relative speed advantage would also depend on the # of shift registers and the kind of addressing you're doing, so it's hard to say for sure. Regardless, shift registers require you to re-write (or read) the entire array each time you address them, so the speed is essentially fixed by the size of your array. If you're just driving LEDs for visual purposes, the human eye is slow enough that speed shouldn't matter.
There's another potential pin advantage with the MCP: if you've got other i2c devices, you're already using the i2c pins, so adding another i2c device is 'free' from a pin use perspective. You can also daisy chain physically separate devices in any order, not unlike a USB port on a computer.
I've got 27 TPIC6B595 shift registers daisy chained running at 8 MHz, is working great.
Got my code tweaked to support 45 of them updated at a 20 KHz rate, pulling data from a big (14K+) array on a '1284P. Smokin!
Another advantage of SPI is that you can read & write simutaneously on MISO & MOSI.
At 8 MHz. I'll give up a pin for CS to support that.
I2C is sooo slooow in comparison.
retronet_RIMBA1ZO:
i've heard of the terms sinking & sourcing from working with the LED matrices, but i'm not sure what "clamp currents" are.
If you exceed the 0V to 5V range the device shouldn't burn out so long as the current is less than 25ma. Internally to the chip there's a diode that will conduct from the input pin to VCC if the input voltage is greater than VCC. Similarly there's another diode connecting from GND to the input pin if the input is less than 0V. These diodes are only rated for 25ma.
Comparing the MCP23008 to the SN74HC595, the MCP23008 allows a total 125ma current source ("current into Vdd pin") or 150ma sink ("current out of Vss pin") while the SN74HC595 allows 70ma ("continuous current through Vcc or GND"). So the MCP23008 can handle, roughly, twice the power.
That is not entirely true. There is also a maximum dissipation for the chip as a whole. If you were to push all 8 pins of the MCP23008 at maximum current you will exceed the 700mW max power of a dip16 package. The chip will fry.
nicoverduin:
That is not entirely true. There is also a maximum dissipation for the chip as a whole. If you were to push all 8 pins of the MCP23008 at maximum current you will exceed the 700mW max power of a dip16 package. The chip will fry.
Or worse, and even more likely, it will work fine in the lab (just running a bit hot) and start failing in the field once it is deployed into a case and/or a hot factory environment.
retronet_RIMBA1ZO:
i've heard of the terms sinking & sourcing from working with the LED matrices, but i'm not sure what "clamp currents" are.
If you exceed the 0V to 5V range the device shouldn't burn out so long as the current is less than 25ma. Internally to the chip there's a diode that will conduct from the input pin to VCC if the input voltage is greater than VCC. Similarly there's another diode connecting from GND to the input pin if the input is less than 0V. These diodes are only rated for 25ma.
ahh, okay - thanks !
Chagrin:
Comparing the MCP23008 to the SN74HC595, the MCP23008 allows a total 125ma current source ("current into Vdd pin") or 150ma sink ("current out of Vss pin") while the SN74HC595 allows 70ma ("continuous current through Vcc or GND"). So the MCP23008 can handle, roughly, twice the power.
i guess i was looking at the wrong stats as to what was the "sink/source" stat.
thanks for clarifying the terminology.
CrossRoads:
I've got 27 TPIC6B595 shift registers daisy chained running at 8 MHz, is working great.
Got my code tweaked to support 45 of them updated at a 20 KHz rate, pulling data from a big (14K+) array on a '1284P. Smokin!
Another advantage of SPI is that you can read & write simutaneously on MISO & MOSI.
At 8 MHz. I'll give up a pin for CS to support that.
I2C is sooo slooow in comparison.
Wow! It always amazes me how much a 'little' Arduino is capable of.
I've not tried to do mass read/writes to a MCP chip, but if you need to read/write to many/all of the registers at once, like you are, then the fact that you have to write to all the registers with each access ends up being an asset rather than a liability since the 'overhead' of each access is spread over many registers. If you have a lot of registers and only need to access a few, the reverse is true.
How do you read/write simultaneously? I really haven't done much with the SPI interface, so my knowledge is limited to a cursory look at the overview page and the shiftin/shiftout functions.
I think the i2c chip probably takes a bit less thought/planning, since you can essentially define and access each pin independently, much like the individual Arduino pins.
retronet_RIMBA1ZO:
i've heard of the terms sinking & sourcing from working with the LED matrices, but i'm not sure what "clamp currents" are.
If you exceed the 0V to 5V range the device shouldn't burn out so long as the current is less than 25ma. Internally to the chip there's a diode that will conduct from the input pin to VCC if the input voltage is greater than VCC. Similarly there's another diode connecting from GND to the input pin if the input is less than 0V. These diodes are only rated for 25ma.
Comparing the MCP23008 to the SN74HC595, the MCP23008 allows a total 125ma current source ("current into Vdd pin") or 150ma sink ("current out of Vss pin") while the SN74HC595 allows 70ma ("continuous current through Vcc or GND"). So the MCP23008 can handle, roughly, twice the power.
CrossRoads said he was using the TCIP6b595
that has 125mA per pin or 500mA for the package.
regardless, if you needed to power 100 devices, you can always use these to control transistors. the fact is that the Arduino can output and control a lot of devices !
Sleepydoc:
How do you read/write simultaneously? I really haven't done much with the SPI interface, so my knowledge is limited to a cursory look at the overview page and the shiftin/shiftout functions.
ALL SPI transfers are read+write. That's the way it works. On every clock pulse the chip outputs a bit and reads one.
That's why the SPI function to send a byte is called "transfer()", not "write()" (or whatever).
Sleepydoc:
I think the i2c chip probably takes a bit less thought/planning, since you can essentially define and access each pin independently, much like the individual Arduino pins.
Saying that either one is "better" is silly. Both have their uses.
There's situations where I2C is better and situations where the shift registers (or SPI) is better.
As I understood the data sheet, any one pin can be 125mA
but for the entire chip, the sum of all pins cannot exceed 500mA, so you could not have all 8 outputs at 100mA