Remarks

x74595
Status
Source code GitHub
NuGet package

Shift registers offer the ability to increase the number of outputs on a microcontroller by using I2C or SPI interfaces. In the case of the 74xx595 series of shift registers, the SPI interface is used to output a series of bits that are then latched to the output pins of the chip.

This class allows Meadow to control the output pins on a 74HCT595 shift register using the SPI interface.

Note that when using this chip, care should be taken to ensure that the total output load of the chip does not exceed the current and thermal dissipation properties for the specific shift register being used.

Code Example

x74595 shiftRegister;

public MeadowApp()
{
    Console.WriteLine("Initialize hardware...");

    shiftRegister = new x74595(Device, Device.CreateSpiBus(), Device.Pins.D00, 8);

    shiftRegister.Clear(true);

    Console.WriteLine("Set Pin 3 to high");
    //turn on pin 3
    shiftRegister.WriteToPin(shiftRegister.Pins.GP3, true);

    Console.WriteLine("Set Pin 4 to high");

    //get the port for Pin4
    var port4 = shiftRegister.CreateDigitalOutputPort(shiftRegister.Pins.GP4, true, Meadow.Hardware.OutputType.OpenDrain);

    Console.WriteLine("Toggle pin 4");

    Thread.Sleep(1000);
    port4.State = false;
    Thread.Sleep(1000);
    port4.State = true;
    Thread.Sleep(1000);

    Console.WriteLine("Raise all pins to high");
    while (true)
    {
        shiftRegister.Clear();

        foreach (var pin in shiftRegister.Pins.AllPins)
        {
            shiftRegister.WriteToPin(pin, true);
            Thread.Sleep(50);
        }
    }
}

Sample project(s) available on GitHub

Wiring Example

To wire a 74HCT595 to your Meadow board, connect the following:

Characteristic Locus
Inheritance System.Object > x74595
Namespace Meadow.Foundation.ICs.IOExpanders
Assembly x74595.dll

Syntax

public class x74595 : IDigitalOutputController

Constructors

x74595(IMeadowDevice, ISpiBus, IPin, Int32)

Constructor a ShiftRegister 74595 object.

Declaration
public x74595(IMeadowDevice device, ISpiBus spiBus, IPin pinChipSelect, int pins = 8)

Parameters

Type Name Description
IMeadowDevice device
ISpiBus spiBus

SpiBus object

IPin pinChipSelect
System.Int32 pins

Number of pins in the shift register (should be a multiple of 8 pins).

Properties

Pins

Declaration
public x74595.PinDefinitions Pins { get; }

Property Value

Type Description
x74595.PinDefinitions

Methods

Clear(Boolean)

Declaration
public void Clear(bool update = true)

Parameters

Type Name Description
System.Boolean update

CreateDigitalOutputPort(IPin, Boolean, OutputType)

Creates a new DigitalOutputPort using the specified pin and initial state.

Declaration
public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState, OutputType outputType)

Parameters

Type Name Description
IPin pin

The pin number to create the port on.

System.Boolean initialState

Whether the pin is initially high or low.

OutputType outputType

Returns

Type Description
IDigitalOutputPort

GetPin(String)

Declaration
public IPin GetPin(string pinName)

Parameters

Type Name Description
System.String pinName

Returns

Type Description
IPin

IsValidPin(IPin)

Checks whether or not the pin passed in exists on the chip.

Declaration
protected bool IsValidPin(IPin pin)

Parameters

Type Name Description
IPin pin

Returns

Type Description
System.Boolean

WriteToPin(IPin, Boolean)

Sets a particular pin's value.

Declaration
public void WriteToPin(IPin pin, bool value)

Parameters

Type Name Description
IPin pin

The pin to write to.

System.Boolean value

The value to write. True for high, false for low.