Search

Arduino and shift registers

Please let us in the comment zone any suggestions that you think will improve the article!

If you like the article click the follow button  to stay in touch with us!

 

 

1. ShiftOut function

Because the method presented in the last post about arduino and shift registers is not efficient, there is a special function which make it easier to use, and is called “shiftOut” with the syntax “shiftOut(dataPin, clockPin, bitOrder, value)”. Now…what represent those values…???

For our model ( where we have SER, RCLK, SRCLK) is shiftOut(SER,SRCLK,MSB (or LSB), a int value between 0 and 255 which is converted in binary)

2.A practical example

Let’s say we have number 3 which in binary is 00000011, at left we have MSB and right LSB:

The program below will display number 3 in binary code on LEDs:

const int SER=8;
const int RCLK=9;
const int SRCLK=10;
void setup() {

pinMode(SER, OUTPUT);
pinMode(RCLK, OUTPUT);
pinMode(SRCLK, OUTPUT);
}
void loop() {
digitalWrite(RCLK, LOW);
shiftOut(SER, SRCLK, LSBFIRST, 3);
digitalWrite(RCLK, HIGH);
}

shift register display number 3

But, you may say that, in syntax at parameter bitOrder i’ve wrote LSB or MSB and above is LSB used, so next program is with MSB

const int SER=8;
const int RCLK=9;
const int SRCLK=10;
void setup() {

pinMode(SER, OUTPUT);
pinMode(RCLK, OUTPUT);
pinMode(SRCLK, OUTPUT);
}
void loop() {
digitalWrite(RCLK, LOW);
shiftOut(SER, SRCLK, MSBFIRST, 3);
digitalWrite(RCLK, HIGH);
}

And the result

shift register display number 3 MSB

In this case the results are that number 3 is displayed from MSB to LSB like 11000000.




3.Interfacing two or more shift register

Hmmm… We can combine two of this devices to control more LEDs or peripherics??

Yes, you can and in the pin diagram we have QH'(see ). To explain this pin think that the shift register is a pipeline where SER is the input and QH’ is the output.

So, if we have two shiftOut functions, both functions will send bit after bit on SER pin of the first shift register.

When this is full (8 bits already on it) and we continue through the second function shiftOut to send bits the first eight will start to flow to second shift register throught QH’ pin.

two shift registers schematic

To understand what i want to say let’s see the next program:

const int SER=8;
const int RCLK=9;
const int SRCLK=10;
void setup() {

pinMode(SER, OUTPUT);
pinMode(RCLK, OUTPUT);
pinMode(SRCLK, OUTPUT);
}
void loop() {
digitalWrite(RCLK, LOW);
shiftOut(SER, SRCLK, MSBFIRST, 7);
shiftOut(SER, SRCLK, MSBFIRST, 3);
digitalWrite(RCLK, HIGH);
}

The first function with number 7 will flow to second shift register and, on the first shift register will remain the second function with number 3

The results are in the image below(green LEDs-first shift register, red LEDs-second shift register)

two shift registers

To make something more interactive below is a program which power on LEDs in a specific order after a rule as 1,2,4,8,16,32,64,128, and as you guessed the numbers are 2 at different exponentials(0,1,2,3,4,5,6,7- 8 LEDs)

const int SER=8;
const int RCLK=9;
const int SRCLK=10;
int i=0;
int x=0;
void setup() {
pinMode(SER, OUTPUT);
pinMode(RCLK, OUTPUT);
pinMode(SRCLK, OUTPUT);
}
void loop() {
int array[]={1,2,4,8,16,32,64,128};
i=array[x];//i takes a value from the array
x=x+1;//position in the array
if (x>7){
x=0;//start again
}
digitalWrite(RCLK, LOW);//put the shift register in register mode
shiftOut(SER, SRCLK, MSBFIRST, i);//writing the registers
shiftOut(SER, SRCLK, MSBFIRST, i);//writing the registers
digitalWrite(RCLK, HIGH);//enable outputs

delay(200);//smal delay to see LEDs blinking
}

A short video:

 

 

Facebooktwitterpinterest

Related posts

Leave a Comment

Show Buttons
Hide Buttons