SHIFT REGISTERS

SHIFT REGISTERS-Uncategorized

Dasduino has a total of 22 pins available that can serve as a digital output and in most cases, it will be quite enough for us. However, some projects may require more than 22 digital outputs. What should we do in that case?

The simplest solution comes directly in a form of a shift register (check out the datasheet), an integrated circuit that we’ll use to extend the number of Dasduino’s digital outputs. But let’s start at the beginning.

WHAT IS A SHIFT REGISTER?

Shift registers are integrated circuits that basically consist of a large number of flip-flops located in one chip. Bistable has two stable states, it can memorize one data bit, (0 or 1). If the register consists of 8 bistables, then it can remember 8 bits. Bistables can be connected in different ways so we distinguish between registers with serial or parallel data entry/printing. There are various combinations depending on the type of data entry/print and we are going to use the shift register with serial input and parallel output data such as 74HC595N. In addition, our register consists of 16 bistables, of which 8 are connected serial inputs and 8 individual outputs. In that way, 8 outputs in the register are realized using, theoretically, only one pin.

In reality, only one transferring data pin from Dasduino to the register is not enough. Besides the pin that is connected to input, we need so called “Clock pin” (SRCLK and RCLK) which symbolize sending and receiving data. Without that, communication between Dasduino and register would not be possible.

In order to be able to do anything with the register (or any other integrated circuit), it is necessary first to figure out where pins are and what they do. In the picture above, we can see the arrangement of the pins on the 74HC595N shift register.

Vcc – to 6V (it is required at the same voltage as a microcontroller, usually 3.3V or 5V)

Qa to Qh – outputs of the shift register (8 in total)

OE – (Output enable) It needs to be connected to GND to enable the output of a shift register. We can disable outputs by simply connecting this pin to HIGH.

SER – (Serial) input to the next shifting pin

SRCLK – (Serial clock) when this pin is HIGH, it will shift the register

RCLK – (Register clock) must enter the HIGH state to switch the output to the next pin of the shift register immediately after the SRCLK returns to the LOW state

SRCLR – (Serial clear) if it’s put to LOW, it’ll delete the whole shift register. That’ why it is necessary to stay HIGH.

 

So, our register consists of a total of 16 bistables. The first 8 are serially connected creating shift register’s input, the other 8 pins are parallel to the first octet so that the output of each shift register’s bistable is connected to the input of the second bistable which serves as a data container and allows sending and displaying data simultaneously. For that reason, we need 3 pins to properly use the shift register. The advantage is that we can serial connect an arbitrary number of registers (maximum 40) and 3 Dasduino pins can be extended to 8, 16, 24, etc.

In the picture above we can see two serial-connected registers, so the 3 outputs of Dasduino are extended to 16. In such a connection, the serial input of the first register (SI1) is a common input, while the input of the second register is connected to the output of the first one. The clock pins of both registers are connected in parallel as well as the latch pin. So connected registers behave like 16-bit.

 

 

HOW DOES THE SHIFT REGISTER WORK?

In this section, we’ll try to explain how 74HC595 shift register works without going into details.

As we have already said, in order to successfully manage the register, we need three pins: Data Input (SI), Clock and Latch.

The sending process is as follows:

To start, we set the Clock and Latch pin to 0 (LOW), and at the input, we bring the first bit of the data we send. After that, we set Clock to 1 (HIGH). In the moment of Clock pin’s state transition (0 to 1), the data that was previously brought to the input is stored in the first register (Figure 4.a). Now if we bring 0 to the input and trigger Clock pin again, 1 from the first register “jumps” into the second register, and at its place comes 0 at the input (Figure 4.b). That means every new bit “pushes” those in front. If the process is repeated 14 more times, we will fill all 16 bistable by a bunch of input data bits. It should be noted that we have filled upper bistable data while lower ones in our case are empty.

a) The process of sending the first bit

b) The process of sending the second bit

If we trigger the Latch pin now (transition from 0 to 1), all of 16 bits will “jump” out of upper registers to lower ones simultaneously, and each odd LED will light if they are connected to the outputs of registers.

Status of the registers after the Latch pin is triggered.

We don’t need to trigger the Latch pin at the end, but we can do it with the Clock pin. However, this configuration has an advantage that we can keep the state of the output registers the way how it is, while at the same time sending new 16 bits of data to the input registers.

With this type of register connection, we can expand the number of digital outputs of our Dasduino if needed and manage the great number of LEDs or some other consumers. However,  it should be borne in mind that the register cannot supply each LED with 20mA because it would require 160mA in total that way. The maximum total current can be around 40mA, giving us a maximum of 5mA per LED. If that is not enough for us, we’ll need to use switching transistors.

CONNECTING TO DASUINO AND CODE

Let’s suppose this “theory” is not interesting and we want to hook up the shift register at Dasduino as soon as possible to start programming. We’ll simply connect pins from Dasduino and shift register like shown in the picture, copy the attached code and use it for whatever we want. Let’s take a look at connections of one or more shift registers with Dasduino.

The comments of the enclosed code contain an explanation of how to use it. If you have been following the entire shift register tutorial, you will not have trouble understanding the code, but if you haven’t, you solved problems immediately at the start by using the code at the end. Below you can see the code that supports up to 40 connected 75HC595 shift registers.

If you want something that will have the ability to expand PWM pins, we recommend TLC5940 or similar. Check out our other tutorials, or some of our projects with registers.

int SER_Pin = 8;   //pin 14 to 75HC595
int RCLK_Pin = 9;  //pin 12 to 75HC595
int SRCLK_Pin = 10; //pin 11 to 75HC595
//broj 75HC595
#define broj_74hc595a 1
//the number of 75HC595 pins, do not change it
#define brojPinova brojPinova_74hc595a * 8
boolean shiftReg[brojPinova];
void setup(){
  pinMode(SER_Pin, OUTPUT);
  pinMode(RCLK_Pin, OUTPUT);
  pinMode(SRCLK_Pin, OUTPUT);
  //reset of the pins
  clearRegisters();
  writeRegisters();
}              
//set all the pins to LOW
void clearRegisters(){
  for(int i = brojPinova- 1; i >=  0; i--){
     shiftReg[i] = LOW;
  }
}
//setting the pins
//we recommend to set the state of each pin first, and then call the function prikazivanja (display)
void writeRegisters(){
  digitalWrite(RCLK_Pin, LOW);
  for(int i = brojPinova- 1; i >=  0; i--){
    digitalWrite(SRCLK_Pin, LOW);
    int val = shiftReg[i];
    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);
  }
  digitalWrite(RCLK_Pin, HIGH);
}
//the function sets each pin
void setRegisterPin(int index, int value){
  registers[index] = value;
}
void loop(){
// calling functions for setting the states of each and every pin
  setRegisterPin(2, HIGH); // setting pin2 75HC595a to HIGH
  setRegisterPin(3, HIGH); // setting pin3 75HC595a to HIGH
  setRegisterPin(4, LOW);  // setting pin4 75HC595a to LOW
  setRegisterPin(5, HIGH); // etc.
  setRegisterPin(7, HIGH);
  writeRegisters();  //in the end, we call the function that shows
                     //the state of set pins

The comments of the enclosed code contain an explanation of how to use it. If you have been following the entire shift register tutorial, you will not have trouble understanding the code, but if you haven’t, you solved problems immediately at the start by using the code at the end. Below you can see the code that supports up to 40 connected 75HC595 shift registers.

If you want something that will have the ability to expand PWM pins, we recommend TLC5940 or similar. Check out our other tutorials, or some of our projects with registers.