Shifting out is a method in which individual bits are "shifted" - i.e. written out - one after the other via a pin of the 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...
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#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); } } |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#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 i=0; i<8; i++) { registerWrite(i, LOW); // set specific LED delay(1000); } } void registerWrite(int _whichPin, int _whichState) { byte bitsToSend = 255; // This Byte has 8 bits: 11111111 digitalWrite(LATCH_PIN, LOW); bitWrite(bitsToSend, _whichPin, _whichState); //Here a specific bit is set (e.i 00100000) shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, bitsToSend); digitalWrite(LATCH_PIN, HIGH); } |
Wollen wir alle Ziffern korrekt anzeigen, so müssen wir uns eine kleine Tabelle erstellen, welche uns die LEDs zeigt, die für die jeweilige Ziffer eingeschaltet sein müssen. Daraus ergibt sich folgende VisualisierungIf we want to display all digits correctly, we have to create a small table that shows us the LEDs that have to be switched on for the respective digit. This results in the following visualisation.
...
Exercises
1. Stellt alle Ziffern von Displays all digits from 0-9 auf dem on the 7-Segment Display darsegment display
2. Vereinfacht euren Code, so dass ihr nur noch ein Array habt, in welchem die Ziffern und entsprechenden LEDs hinterlegt sind.
3. Ergänzt den Code, dass ihr zusätzlich zu den Ziffern auch Buchstaben anzeigen könnt. Simplify your code so that you only have one array in which the digits and the corresponding LEDs are stored.
3. Supplements the code so that you can also display letters in addition to the numbers.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#define LATCH_PIN 8 //Pin zu ST_CP vom 74HC595 #define CLOCK_PIN 12 //Pin zu SH_CP vom 74HC595 #define DATA_PIN 11 //Pin zu DS vom 74HC595 void setup() { pinMode(LATCH_PIN, OUTPUT); pinMode(CLOCK_PIN, OUTPUT); pinMode(DATA_PIN, OUTPUT); } void loop() { registerWrite(B00000011); //oder 0x03 delay(1000); registerWrite(B10011111); //oder 0x9f delay(1000); registerWrite(B00100101); //oder 0x25 delay(1000); registerWrite(B00001101); //oder 0x0d delay(1000); registerWrite(B10011001); //oder 0x99 delay(1000); registerWrite(B01001001); //oder 0x45 delay(1000); registerWrite(B01000001); //oder 0x41 delay(1000); registerWrite(B00011111); //oder 0x1f delay(1000); registerWrite(B00000001); //oder 0x01 delay(1000); registerWrite(B00011001); //oder 0x19 delay(1000); } void registerWrite(byte bitsToSend) { digitalWrite(LATCH_PIN, LOW); shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, bitsToSend); digitalWrite(LATCH_PIN, HIGH); } |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
#define LATCH_PIN 8 //Pin zufor ST_CP vomon 74HC595 #define CLOCK_PIN 12 //Pin zufor SH_CP vomon 74HC595 #define DATA_PIN 11 //Pin zufor DS vomon 74HC595 byte number[] = { B00000011, //0 B10011111, //1 B00100101, //2 B00001101, //3 B10011001, //4 B01001001, //5 B01000001, //6 B00011111, //7 B00000001, //8 B00011001, //9 }; unsigned long lastTime = 0; int delayOfDisplay = 1000; int index = 0; void setup() { pinMode(LATCH_PIN, OUTPUT); pinMode(CLOCK_PIN, OUTPUT); pinMode(DATA_PIN, OUTPUT); } void loop() { if(millis()-lastTime > delayOfDisplay) { registerWrite(number[index]); lastTime = millis(); if(index < 9) { index++; } else { index = 0; } } } void registerWrite(byte bitsToSend) { digitalWrite(LATCH_PIN, LOW); shiftOut(DATA_PIN, CLOCK_PIN, LSBFIRST, bitsToSend); digitalWrite(LATCH_PIN, HIGH); } | ||||||
Code Block | ||||||
| ||||||
//Selber herausfinden ;-) |