I bought my shift register here: http://www.sparkfun.com/products/733 bu there are many different places to shop, so buy it from your favorite place.
Code:
public class ShiftRegister { private SPI _dataPort; private byte[] bitsStorage; public ShiftRegister(Cpu.Pin latch = Pins.GPIO_PIN_D10) { SPI.Configuration spiConfig = new SPI.Configuration( latch, false, // active state 0, // setup time 0, // hold time false, // clock idle state true, // clock edge 1000, // clock rate SPI.SPI_module.SPI1); _dataPort = new SPI(spiConfig); } public void WriteByteArray(byte[] byteArray) { _dataPort.Write(byteArray); } public void StoreBits(string eightBits) { if (bitsStorage == null) bitsStorage = new byte[1]; // Start the storage else { byte[] temp = bitsStorage; // save it temporarily bitsStorage = new byte[bitsStorage.Length + 1]; // make room for another eight bits for (int i = 0; i < temp.Length; i++) bitsStorage[i] = temp[i]; } while (eightBits.Length < 8) eightBits = "0" + eightBits; byte data = 0x00; char[] chars = eightBits.Substring(0, 8).ToCharArray(); for (int i = 0; i < 8; i++) data += (byte)((chars[System.Math.Abs(i - 7)] == '1') ? System.Math.Pow(2, i) : 0); bitsStorage[bitsStorage.Length-1] = data; } public void SendBits(bool eraseAfterSend = true) { WriteByteArray(bitsStorage); // send sotred bits if (eraseAfterSend) bitsStorage = null; // erase all the stored bits if told to. } }
The new and improved bit writting system is now designed to work with multiple shift registers. Although it is nice for me because I like binary and stuff... its not the way to go if you're looking for speed. Its best to use the WriteByteArray(byte[] byteArray) method instead.
If you don't have the time or brain power (like me ) to memorize/learn bits to bytes to hex char and all that mess then use this: http://home2.paulsch...et/tools/xlate/
All off and on example:
ShiftRegister shifty = new ShiftRegister(); byte[] allOn = new byte[]{0xff, 0xff}; // two bytes because we got two shift registers byte[] allOff = new byte[]{0x00, 0x00}; while (true) { shifty.WriteByteArray(allOn); Thread.Sleep(1000); shifty.WriteByteArray(allOff); Thread.Sleep(1000); }
Single Shift register:
Shiftregister.png 177.05KB 443 downloads
Double shift register all the way across the breadboard (I hope you got that reference):
doubleshiftregister.PNG 175.4KB 371 downloads
Note that there are no LEDs or other outputs on that schematic, I just wanted to keep things as neat as possible.
To make it a little more clear (hopefully):
Netduino | SR 1 | SR2
---------------------
GND | 13 | 13
GND | 8 | 8
3V3 | 16 | 16
3V3 | 10 | 10
10 | 12 | 12
13 | 11 | 11
14 | 14 | X
X | 9 | 14
Datasheet for the shift register I am using: http://www.sparkfun....C/SN74HC595.pdf