예제 #1
0
 private void WriteRegister8(byte reg, byte data)
 {
     outbuf[0] = 0x40;
     outbuf[1] = reg;
     outbuf[2] = data;
     dev.Write(outbuf, 3);
 }
예제 #2
0
        /// <summary>
        /// Constructor for a chain of one or more SN74HC595 shift registers.
        /// </summary>
        /// <param name="dev">SPI device object.</param>
        /// <param name="stages">Number of stages in the chain.</param>
        /// <param name="initialstate">Initial shift register chain state.</param>
        public Device(IO.Interfaces.SPI.Device dev, int stages = 1, byte[] initialstate = null)
        {
            // Validate parameters

            if (stages < 1)
            {
                throw new System.Exception("stages paramter is invalid");
            }

            if (initialstate != null)
            {
                if (initialstate.Length != stages)
                {
                    throw new System.Exception("initialstate parameter length is invalid");
                }
            }

            // Save the SPI device object

            spidev = dev;

            // Allocate shift register state buffer

            statebuf = new byte[stages];

            // Initialize shift register state buffer

            if (initialstate == null)
            {
                for (int i = 0; i < state.Length; i++)
                {
                    statebuf[i] = 0;
                }
            }
            else
            {
                statebuf = initialstate;
            }

            // Shift out initial register state

            spidev.Write(statebuf, statebuf.Length);
        }
예제 #3
0
 /// <summary>
 /// Set a single bit in the shift register chain.
 /// </summary>
 /// <param name="index">Shift register stage number.  Zero indicates
 /// the first register stage.</param>
 /// <param name="mask">Shift register bit mask.</param>
 public void SetBit(int index, byte mask)
 {
     statebuf[index] |= mask;
     spidev.Write(statebuf, statebuf.Length);
 }