Main Site Documentation

Talking to shift register IC/74HCT595


#1

Here is the code in case some one is needing it to talk to a 74HCT595. It is part of my project and the foundation for talking to the adafruit motor shield. Let me knwo if you have any questions will do my best to help.

public class HCT595 : ITransferProvider, IDisposable
   {
       private OutputPort _strobePort;
       private OutputPort _dataPort;
       private OutputPort _clockPort;
       private bool _bigEndian;
       /// 
       /// Creates new instance of shift register.
       /// 
       /// Pin used for data.
       /// Pin used for clock.
       /// Pin used for strobe/clear/RCLK
       /// Most significant bit goes first
       public HCT595(Cpu.Pin dataPin, Cpu.Pin clockPin, Cpu.Pin strobePin, bool bigEndian)
       {
           _strobePort = new OutputPort(strobePin, false);
           _dataPort = new OutputPort(dataPin, false);
           _clockPort = new OutputPort(clockPin, false);
           _bigEndian = bigEndian;
       }
       // Dispose the ShiftRegister
       public void Dispose()
       {
           _strobePort.Dispose();
           _dataPort.Dispose();
           _clockPort.Dispose();
       }
       // Send out one byte to shift register
       public void SendByte(byte data)
       {
           ShiftOut(data);
           _strobePort.Write(true);
           _strobePort.Write(false);
       }
       // Send out one byte in serial transfer.       
       private void ShiftOut(byte data)
       {
           int i, incr;
           if (_bigEndian)
           {
               i = 7; incr = -1;
           }
           else
           {
               i = 0; incr = +1;
           }
           for (var step = 0; step >= i;
               i += incr;
               _dataPort.Write((output != 0));
               _clockPort.Write(true);
               _clockPort.Write(false);
           }
       }
   }
   public interface ITransferProvider
       {
           //Sends out one byte
           void SendByte(byte data);
       }