Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Shifting out is a method in which individual bits are "shifted" - i.e. written out - one after the other via a pin of the an Arduino. This function can be used for example to control multiple LEDs with just a few pins.

...

0000 0101 5
...
1111 1111 255 

The value of the byte can either be determined by the sum of the values of the bits or each bit can be set directly. In C/C++ we have access to these values through some defined functions - through the so-called bitwise operations. For example, if you want to change the third bit in a byte, there are three options...

...

If we now imagine that each bit represents an LED, which is set ON or OFF depending on the value of the bit, then we can control eight different LEDs with the help of one byte. So it's just a matter of how we can send a byte and then decode it in such a way that we can use it to switch an LED. For this, we use a ShiftRegistershift-register, specifically the 75HC595. This allows us to send the entire byte via a one data PIN (DATA_PIN) and to switch up to eight outputs depending on its value. We can even stack shift-registers together to get even more outputs from just one pin. 



The function shiftOutshift out(DATA_PIN, CLOCK_PIN, bitOrder, value) takes over the timing and the writing out of the complete byte. DATA_PIN is the PIN via which the data is sent. CLOCK_PIN is for timing. bitOrder describes whether we want to read the byte from left to right (MSBFIRST - most significant bit first) or from right to left (LSBFIRST - last significant bit first). 

...

Code Block
languagejava
titleShiftOut example
collapsetrue
#define LATCH_PIN 8  //Pin for ST_CP on 74HC595
#define CLOCK_PIN 12 //Pin for SH_CP on 74HC595
#define DATA_PIN 11  //Pin for DS on 74HC595
 
void setup() 
{
  pinMode(LATCH_PIN, OUTPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  pinMode(DATA_PIN, OUTPUT);
}
 
void loop() 
{
  for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) 
  {
    digitalWrite(LATCH_PIN, LOW);  // LATCH_PIN to LOW = Begin
 
    shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, numberToDisplay); // Data shifted
 
    digitalWrite(LATCH_PIN, HIGH);  // LATCH_PIN auf HIGH = LEDs light up
 
    delay(500);
  }
}

T

he The first example shows how a byte is resolved at the bit level. The following example helps to really be able to address each individual pin of the register.

...