esp-idf-lib/shiftregister

Shift register driver.

Overview

A shift register is an IC to save input and/or output pins. To control 8 LEDs with GPIO, 8 GPIO pins are used. With an 8-bit shift register, the minimum required number of GPIO pins is 3 (SI, SCK, and RCK, described later).

Types of shift registers

There are two types of shift registers: Serial-In Parallel-Out (SIPO) and Parallel-In Serial-Out (PISO). SIPO is used to control output ports and PISO is to read inputs from input ports.

Serial-In Parallel-Out (SIPO)

A Serial-In Parallel-Out shift register has output pins, often eight pins. These pins are connected to devices or other components to control, such as LEDs. Notable ICs are:

  • 74HC164

  • 74HC595

Parallel-In Serial-Out (PISO)

A Parallel-In Serial-Out shift register has input pins, often eight pins. These pins are connected to input devices, such as buttons. Notable ICs are:

  • 74HC165

Input and output pins

Different manufacturers use different names for pin names. Here, common names by major manufacturers are used. Please refer to the data sheet for the shift register.

The output pins are often referred as QA, QB ,QC, and Q${X}. If the shift register is an 8-bit shift register, the last one is QH.

The input pins are often referred as A, B, C, and so on.

Often, SIPO and PISO shift register has another output or input pin, QH". This pin is used to daisy chain shift registers. With two shift registers, 16 input or output pins are available.

Pins to control the SIPO shift register are:

  • SI: Indicates the bit value to store or read. Commonly referred as data pin.

  • SCK/SRCLK: Stores a bit value of SI into the first stage when raising. Commonly referred as clock pin.

  • RCK: All the bits are stored into storage register when raising. Commonly referred as latch pin.

  • G/OE: Enables the outputs when HIGH. Disables the outputs when LOW. Tied to HIGH when the outputs are always enabled.

  • SCLR: Clears data in the shift register when LOW. Tie the pin to HIGH when the pin is not used.

Pins to control the PISO shift register are:

  • QH: Outputs serial data. Commonly referred as data pin. Often, another pin, complementary, or inverted, serial output pin is available.

  • SH/LD/S/L: Reads input values when falling.

  • SER: Reads serial data. Used to daisy chain multiple shift registers.

  • CLK: Clock inputs.

  • CLK INH: Starts sending the data when failing.

LSBFIRST and MSBFIRST

When LSBFIRST is used, the data is sent Least Significant Bit first. That is, when the data is 0b00000001, or 0x1, Q7 is becomes HIGH.

When MSBFIRST is used, the data is sent Most Significant Bit first. That is, when the data is 0b00000001, Q0 becomes HIGH.

The default is LSBFIRST.

Enums

enum shiftregister_mode_t

Operational mode of shift register.

Parallel Out mode, or Serial-In Parallel-Out mode, is when the shift register acts as an output device, such as controlling LEDs.

Parallel In mode, or Parallel-In Serial-Out mode, is when the shift register acts as an input device, such as reading multiple button inputs.

Values:

enumerator SHIFTREGISTER_MODE_WRITE

Parallel Out.

enumerator SHIFTREGISTER_MODE_READ

Parallel In.

struct shiftregister_config_t
#include <shiftregister.h>

Configuration for GPIO.

Public Members

gpio_num_t data_io_num

DATA pin number.

gpio_num_t clk_io_num

CLK pin number.

gpio_num_t rclk_io_num

Register CLK pin number.

gpio_num_t srclr_io_num

Shift register clear pin number.

Use GPIO_NUM_NC to signal the pin is not used.

gpio_num_t oe_io_num

Output Enable pin number, Use GPIO_NUM_NC to signal the pin is not used.

shiftregister_mode_t mode

Operational mode of the shift register.

Resources

GPIO

Bit-banging with GPIOs.

C header file:

#include <shiftregister_gpio.h>

Functions

esp_err_t shiftregister_gpio_init(const shiftregister_config_t *config)

Initialize device descriptor.

Parameters:

config[in] Configuration for GPIO.

Returns:

  • ESP_OK: Successful

  • Other: Failure

esp_err_t shiftregister_gpio_transfer(const shiftregister_config_t *config, uint8_t *data, const size_t size)

Write data to the register.

Parameters:
  • config[in] Configuration for GPIO.

  • data[in] The data to write.

  • data[out] The data to read.

  • size[in] Size of data. The size should be less than or equal to the number of shift register.

Returns:

  • ESP_OK: Successful

  • Other error: Failure