nanoFramework.Iot.Device.ShiftRegister 1.2.587

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package nanoFramework.Iot.Device.ShiftRegister --version 1.2.587                
NuGet\Install-Package nanoFramework.Iot.Device.ShiftRegister -Version 1.2.587                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.Iot.Device.ShiftRegister" Version="1.2.587" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.Iot.Device.ShiftRegister --version 1.2.587                
#r "nuget: nanoFramework.Iot.Device.ShiftRegister, 1.2.587"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nanoFramework.Iot.Device.ShiftRegister as a Cake Addin
#addin nuget:?package=nanoFramework.Iot.Device.ShiftRegister&version=1.2.587

// Install nanoFramework.Iot.Device.ShiftRegister as a Cake Tool
#tool nuget:?package=nanoFramework.Iot.Device.ShiftRegister&version=1.2.587                

Generic shift register

A shift register enables controlling multiple devices, like LEDs, using a small number of pins (minimum of 3 -- data, data clock and register latch). Shift registers can be daisy-chained without requiring using additional pins, enabling addressing a large number of devices, limited only by current and the algorithms you use.

The ShiftRegister binding abstracts the interaction with the storage register, the storage clock, the register clock and other shift register capabilities. This binding enables interaction via GPIO or SPI.

ShiftRegister is used as the base class for Sn74hc595 and Mbi5027 bindings. It can be used directly, or you can rely on it as an implementation detail of those more specific bindings. It has been tested with with SN74HC595, MBI5027, and MBI5168 shift registers.

The following image is of the popular SN74HC595 8-bit shift register:

shift-register

The following image is of the larger MBI5027 16-bit shift register:

MBI5027

The sample demonstrates how to use the shift register in some basic ways.

Using GPIO

The binding can use GpioController pins to control the shift register. It uses ShiftRegisterPinMapping to describe the pins that will be used.

The following example code demonstrates how to use a shift register with GPIO.

ShiftRegister sr = new(ShiftRegisterPinMapping.Minimal, 8);

// Light up three of first four LEDs
sr.ShiftBit(1);
sr.ShiftBit(1);
sr.ShiftBit(0);
sr.ShiftBit(1);
sr.Latch();

// Display for 1s
Thread.Sleep(1000);

// Write to all 8 registers with a byte value
// ShiftByte latches data by default
sr.ShiftByte(0b_1000_1101);

The following diagram demonstrates the required wiring for using the SN74HC595 with minimal mapping. Other shift registers will be similar.

sn74hc595-led-bar-graph-spi_bb

Using SPI

Important: make sure you properly setup the SPI pins especially for ESP32 before creating the SpiDevice, make sure you install the nanoFramework.Hardware.ESP32 nuget:

//////////////////////////////////////////////////////////////////////
// when connecting to an ESP32 device, need to configure the SPI GPIOs
// used for the bus
Configuration.SetPinFunction(21, DeviceFunction.SPI1_MOSI);
Configuration.SetPinFunction(22, DeviceFunction.SPI1_MISO);
Configuration.SetPinFunction(23, DeviceFunction.SPI1_CLOCK);
// Make sure as well you are using the right chip select

For other devices like STM32, please make sure you're using the preset pins for the SPI bus you want to use. The chip select can as well be pre setup.

The bindings can use a SpiDevice to control the shift register. The shift register timing maps to the SPI protocol, enabling SPI to be used. The wiring from is straightforward, from SPI pins to the shift register: SDI (MOSI) → SDI; SCLK → CLK; CEO → LE.

Note: The SPI protocol has terms with casual references to slavery. We're doing our part to avoid them.

The following example code demonstrates how to use a shift register with SPI.

// assuming an 8-bit shift register
ShiftRegister sr = new(SpiDevice.Create(new(1, 42)), 8);

// Light up three of first four LEDs
// The ShiftBit() method is disallowed when using SPI
sr.ShiftByte(0b_1011);

// Clear register
sr.ShiftClear();

// Write to all 8 registers with a byte value
sr.ShiftByte(0b_1010_1010);

The following diagram demonstrates the required wiring for using the SN74HC595 with SPI. Other shift registers will be similar.

sn74hc595-led-bar-graph-spi_bb

Daisy-chaining

The binding supports daisy chaining, using either GPIO or SPI. The GPIO-based example below demonstrates how to instantiate the binding for controlling/addressing two -- daisy-chained -- 8-bit shift registers. This is specified by the integer value in the constructor.

ShiftRegister sr = new(ShiftRegisterPinMapping.Minimal, 16);

The shift registers need to be correctly wired to enable daisy-chaining. On the SN74HC595, QH' in the first register would connect to SER in the second register. The pattern with the MBI5027 and MBI5168 is similar, SDO in the first register would connect to SDI in the second.

You can write values across multiple daisy chained devices in one of several ways, as demonstrated in the following code. You wouldn't typically use of all these approaches, but pick one.

// Write a value to each register bit and latch
// Only works with GPIO
for (int i = 0; i < sr.BitLength; i++)
{
    sr.ShiftBit(1);
}
sr.Latch();

// Prints the following pattern to each register: 10101010
// This pattern only works for register lengths divisible by 8 (which is common)
for (int i = 0; i < sr.BitLength / 8; i++)
{
    sr.ShiftByte(0b_1010_1010);
}

// Downshift a 32-bit number to the desired number of daisy-chained devices
// Same thing could be done with a 64-bit integer if you have more than four 8-bit shift registers (or more than two 16-bit ones)
// Prints the following pattern across two registers: 0001001110001000
int value = 0b_0001_0011_1000_1000; // 5000
for (int i = (sr.BitLength / 8) - 1; i > 0; i--)
{
    int shift = i * 8;
    int downShiftedValue = value >> shift;
    sr.ShiftByte((byte)downShiftedValue, false);
}

sr.ShiftByte((byte)value);

// Print array of bytes
// Result will be same as previous example
var bytes = new byte[] { 0b10001000, 0b00010011};
foreach (var b in bytes)
{
    sr.ShiftByte(b);
}

The following diagram demonstrates the required wiring for using the SN74HC595 with daisy-chaining. Other shift registers will be similar. This diagram uses the Minimal mapping. The Complete mapping will differ.

sn74hc595-minimal-led-bar-graph-double-up_bb

Resources

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on nanoFramework.Iot.Device.ShiftRegister:

Package Downloads
nanoFramework.Iot.Device.Sn74hc595

This package includes the .NET IoT Core binding Iot.Device.Sn74hc595 for .NET nanoFramework C# projects.

nanoFramework.Iot.Device.CharacterLcd

This package includes the .NET IoT Core binding Character LCD for .NET nanoFramework C# projects.

nanoFramework.Iot.Device.Mbi5027

This package includes the .NET IoT Core binding Iot.Device.Mbi5027 for .NET nanoFramework C# projects.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on nanoFramework.Iot.Device.ShiftRegister:

Repository Stars
nanoframework/nanoFramework.IoT.Device
📦 This repo includes .NET nanoFramework implementations for various sensors, chips, displays, hats and drivers
Version Downloads Last updated
1.2.702 239 12/18/2024
1.2.671 757 10/23/2024
1.2.633 772 8/30/2024
1.2.587 804 7/12/2024
1.2.558 779 5/29/2024
1.2.546 351 5/15/2024
1.2.424 2,373 11/9/2023
1.2.421 129 11/9/2023
1.2.419 146 11/9/2023
1.2.326 3,065 5/24/2023
1.2.301 1,432 5/10/2023
1.2.296 919 5/3/2023
1.2.287 320 4/5/2023
1.2.271 1,224 3/15/2023
1.2.202 3,282 12/28/2022
1.2.196 291 12/28/2022
1.2.195 302 12/27/2022
1.2.158 2,360 11/13/2022
1.2.149 2,475 11/3/2022
1.2.141 411 10/25/2022
1.2.121 3,944 10/12/2022
1.2.103 4,903 9/24/2022
1.2.94 2,587 9/22/2022
1.2.86 2,030 9/15/2022
1.2.72 1,701 9/8/2022
1.2.4 1,931 7/13/2022
1.1.140.3705 1,491 7/6/2022
1.1.115.33436 1,464 6/24/2022
1.1.113.2032 421 6/23/2022
1.1.26 3,396 4/26/2022
1.1.19 1,434 4/21/2022
1.1.2 1,488 4/15/2022
1.1.1 446 4/14/2022
1.0.300 1,721 3/30/2022
1.0.277-preview.126 168 3/25/2022
1.0.277-preview.125 138 3/25/2022
1.0.277-preview.112 184 3/19/2022
1.0.277-preview.102 271 3/11/2022
1.0.277-preview.99 158 3/10/2022
1.0.277-preview.85 158 2/25/2022
1.0.277-preview.60 150 2/4/2022
1.0.277-preview.32 146 1/27/2022
1.0.277-preview.17 142 1/24/2022
1.0.277-preview.1 141 1/11/2022
1.0.259 339 12/9/2021
1.0.221 175 10/19/2021
1.0.219 186 10/19/2021
1.0.218 219 10/18/2021
1.0.207 203 10/11/2021
1.0.155 185 8/31/2021
1.0.119 255 6/28/2021
1.0.112 225 6/16/2021
1.0.78 190 5/26/2021
1.0.49 204 5/24/2021