Wie unser Computer tickt - alles logisch? - AZ-Delivery

Basics of digital technology using the example of the Sn74HC595N Sn74HC595N

One of the stupidest sayings from my youth was: do not teach others until you are not scholarship yourself. Such nonsense. Who knows everything? And are we not obliged to pass on what we have acquired in the past decades if it is still relevant? So in the following, I will try to convey the basics of the way of working of our Micro Controllers, with a focus on the logic, although I really don't know everything about it.

The first hurdle for many beginners represents the different number systems. Those who have ten fingers are best expected in 2022 in the decimal system. The Roman-Latin number system, after we live in MMXXII in the year, has not proven itself. But our computer has no ten fingers; He only knows 0 or 1, light from or light. Each memory address and every register content are just a sequence of zeros and one. We call the unit for this memory, which can only take these two states. (No creep advertising!). Because of this knowledge, Konrad Zuse is rightly referred to as the inventor of the computer, even if his Z2 still worked with a telephone relay because the transistor was not yet invented.

So while we represent our numbers with ten different digits, the electronics are content with a two-system, also called a dual system or binary system. In order to keep an overview, the programmers quickly inserted spaces in the almost infinite sequence of zeros and one, after each fourth sign. Such a four -successe was called a nibble. For processors and data buses, you first selected 8-bit for the processing and these 8 bits were called a byte. Our microcontroller is still working today, while our PCs have arrived at a data width of 64 bits. The individual steps of this evolution (as we will logically) follow the two potencies.

The spaces after four bits each make it easier to type, but a shorter spelling would be desirable. That is why the programmers have introduced another number system: the hexadecimal system. What a word: the first part of the name Hexa comes from Greek (for 6), and the second part from Latin (Decem = 10).

With four characters in the dual system, you can basically describe 16 different numbers. Since the count begins at zero, the largest number is initially 15. The hexadecimal system first uses the same digits as the decimal system, from 0 to 9. Then letters are used: a = 10, b = 11, c = 12, d = 13, E = 14 and f = 15. Maybe this little donkey bridge will help: ..., CWolf, DStimmeal, ..., Ffifth

In order to recognize which number system it is, the numbers are marked with ambiguity, e.g. 11. In many programming languages, binary numbers are labeled by a preceding 0b (zero b), the hexadecimal number by 0x (zero X).

Comparison of the number systems:

Dec

0

1

2

3

4

5

6

7

AM

0000

0001

0010

0011

0100

0101

0110

0111

Hex

0

1

2

3

4

5

6

7


Dec

8

9

10

11

12

13

14

15

AM

1000

1001

1010

1011

1100

1101

1110

1111

Hex

8

9

A

B

C

D

E

F

If you represent the 8-bit as a binary number in a row, the value of each bit increases from one side to the other by a factor of 2.

Example of conversion:

Value decimal

128

64

32

16

8

4

2

1

Converted decimal number

binary

0

1

0

0

1

1

0

1

1+4+8+64 = 77

binary

1

0

0

0

1

0

1

0

128+8+2 = 138

binary

0

0

1

1

0

1

1

1

32+16+4+2+1 = 55


You simply add up the value of the places that are "set" with 1. So you can quickly convert from binary into decimal. In addition, you can already see whether it is a straight or odd decimal number, namely if the bit is set on the right with the value 1.

As we have seen, the decreased value of a digit, the further left it is in the sequence of digits, rises by factor 2, for Hexadecimal numbers by a factor of 16. The highest number that can be displayed with a byte is 1111 1111 or in Hexadecimal ff, converted with f = 15 results in 16*15 + 1*15 = 255. Programmer joke with beard: Christmas is 19 hex (= 25 Dec).

Two bytes are reserved for the 16-bit number 16 we most used. The highest number is therefore FF FF Hex = 16 high3 * 15 + 16hoch2 * 15 + 16hoch1* 15 + 16hoch0 * 15 = 2 high16 -1 = 65.535. Instead of the figure area from 0 to 65,535, the area from -32,768 to + 32,767 is used for the Signed Int -number.

The very right bit is also called LSB = Least Significant Bit. Its value is 2 high 0 = 1. We remember: The bits are usually counted (see the following two paragraphs) from right to left and the count begins at 0. The seventh bit is then the very left bit, called MSB = most significant bit) and has the value 2 high 7 = 128.

If you have already stumbled through the terms big-endian and Little-Endian, here briefly an explanation: essentially it is about the serial (bit) transmission of data.

One speaks of big-endian when the highest quality bit of bytes is first transmitted (e.g. atI²C), and by Little-Endian, if the lowest quality bit of bytes is first transmitted (e.g. atRS-232).

Now again about the tip that I in Blog post for the Logic Analyzer, have already shown: the use of the Windows computer for the conversion between the number systems. You choose the programmer mode on the left next to the word standard by clicking on the three horizontal strips and receive the following image, in which the decimal number 255 is also shown in the other number formats.

But the computer can be more than just converted into the different number systems: the programmer would like to "manipulate" the numbers, i.e. carry out arithmetic operations in order to achieve a certain effect. We will see examples of this later.

If you press the "Bit's" button, you get access to five functions with which you can link two numbers, as well as the sixth operator, with which a number is negated.

The logical functions are bit by and, or, not, nand, nor, and Xor. The best way to work is actually understandable in binary format since every bit is displayed there. After each example, I will show the respective C/C ++ function that we would use for the same in the Arduino IDE:

And: The and function always results in 1 when both numbers had a 1.

Logic table:

A

B

AWAY

0

0

0

0

1

0

1

0

0

1

1

1


Example: Any register content should be changed in such a way that the fifth bit (counted from behind) is set to zero. To do this, I use the link:

0111 1110 & 1110 1111 = 0110 1110

source

0

1

1

1

1

1

1

0

&

mask

1

1

1

0

1

1

1

1

Result

0

1

1

0

1

1

1

0


This is called "bitmask" in language use. In this way, individual bits can be "manipulated" without changing the remaining bits.

The sign in the Arduino IDE (C ++) is the commercial and sign (ampersand, &)

Example from the Arduino aid for direct port manipulation to configure the GPIOs by registering without functional calls:

Portd = portd & b00000011; // Delete the bits 2 - 7 and pins PD0 and PD1 (XX & 11 == XX) do not change.

So far you knew the double sign &&? This is not the bit of and linking, but the link between two logical expressions, i.e. the logical and from the Boolean algebra, e.g. when asked whether a number is larger or equal to zero and less than 10.
Code snippets: if (number> = 0 && number <10)

OR: The Oder function always results in 1 when one of the two numbers has a 1.

Logic table:

A

B

A | B

0

0

0

0

1

1

1

0

1

1

1

1


Example: Any register content should be changed in such a way that only the fifth bit (counted from behind) is set to one. To do this, I use the Oder link:

0110 1110  | 0001 0000 = 0111 1110 

source

0

1

1

0

1

1

1

0

|

mask

0

0

0

1

0

0

0

0

Result

0

1

1

1

1

1

1

0

The sign of the Bit-style OR operator in C ++ is the vertical bar symbol |.

You may also know this sign in double execution ||. Again not a bit of comparison, but the Oder link between two logical expressions.

Need: The emergency function negates the value, i.e. from 1 to 0, and 0 becomes 1, the best example is cyclical on and off of an LED by negating the current state. But here it refers to every single bit.

The bitter emergency operator in C ++ is the Tilde sign ~. In contrast to & and | If the bitter emergency operator is applied to a single operator on the right.

Danger: With this function, abstruse can be obtained, because depending on the length of the input value, the preceding and not displayed zeros become one.

In the Windows calculator, therefore, three times the mouse clicks on QWORD (it initially appears DWord, then Word) to byte. Then you get "only" eight characters, even if you have entered a smaller number, see below:

The example on the Arduino aid shows an integer number consisting of two bytes:

int a = 103; // binary: 0000000001100111

int b = ~ a; // binary: 111111110011000 = -104

The highest bit in a signed int-variable is the so-called sign bit. If the highest bit 1 is, the number is interpreted as negative. This coding of positive and negative numbers is referred to as a two complement.

In order to emphasize the difference between the bit of negation to the Boolean algebra, here is the completeness of the negation of a logical expression: this takes place in C ++ with the exclamation mark! Example code: If (! Condition) means: if the condition is not fulfilled.

Nand and Nor have quickly explained: at the end of the and or function, the values ​​are negated, i.e. nand = not and nor = not or.

Xor, the exclusive or, is once again an interesting operator: a bit of XOR operation only leads to a 1 if the input bits are different. Otherwise, there is a 0.

In C ++ the bitter XOR operator is written with the Caret symbol^(also called Zirkumflex).

Logic table:

A

B

A ^ b

0

0

0

0

1

1

1

0

1

1

1

0


Bit shift: The shift operators cause the bits to be moved to the left or right either. The left operator is the source variable, and the right operator indicates the width of how many places are postponed. In the Windows computer, the shift is moved by pressing the corresponding button several times.

In the following picture, the cyclical shift is first displayed in the Windows calculator, i.e. the rotation of all eight bits (including the non-displayed zeros). In the right picture I entered the binary number 0001 1001 (= 25 dec = 19 hex) and once clicked the shift to the left <<.

Important note: This shift around a bit to the left corresponds to the multiplication with 2

Interestingly, there are other functions for bit operations that are in the Arduino reference are displayed:

Bits and bytes

bit()        Calculates the value of the specified bits (Bit 0 is 1, Bit 1 is 2, Bit 2 is 4, etc.).
syntax bit (n)

BitClear ()    Deletes the value (writes 0) of bits of a number variable.
syntax BitClear (X, N)

Bitread ()    Reads a bit from a number variable.
syntax Bitread (X, N)

Bitset ()        Sets (writes a 1) a bit of a number variable.
Syntax Bitset (X, N)

Bitwrite ()    Write a bit (0 or 1) of a figure variable in the Nen place.
Syntax Bitwrite (X, N, B)

Highbyte ()    Reads the Most-Significant (left) byte of a data word (or the second smallest byte of a larger data type).
Syntax Highbyte (X)

Lowbyte ()    Reads the Least Significant (right) byte of a data word or larger data type. Syntax lowbyte (x)

As a summary example, I would like to work on the ICS SN74HC595N Explain, which is better known as the "sliefer".

An example on the Internet shows how eight LEDs are controlled individually without using eight outputs of the Micro Controller, but only three. And the nice thing is: you can cascade this building block, i.e. switch on in a row, and still only require the three connections. In this way, you get 16 or even 24 outputs. The number of ICS connected in succession is only limited by transmission rate and clock frequency.

The three essential entrances to the IC are usually called DS (= Data Serial), SHCP (= Shift Register Clock PIN), and STCP (= Store Register Clock PIN), but before I explain the functions of these three pins, here first the circuit picture of the dip- 16 ICS and the different names according to data sheet:

The heart of the ICS are two registers (= storage places), the slabber (Shift Register), which receives the 8 bits data in a serial manner, and the storage register (Storage Register) which takes over the data and provides the outputs. As I said, the data transfer from the Micro Controller to the SN74HC595 takes place in serial at the PIN DS, clocked with every positive flank of the Shift Register Clock to SRCP. If all bits are transferred to the sliding or Shift register, a positive flank on the storage register Clock (STCP) ensures that all bits are transferred to the memory register at the same time.

If the connection OE (= output enable)-the cross line above the letters stands for the negative logic-the data is issued when high is on OE, and the outputs are "high-impedance state". ((annotation: The high-resistance state is issued by a component if it has no active input. With digital circuits, this means that the output signal is neither logical nor 1, but high -unom. One signal ensures that the component behaves as if its outcome was temporarily separated from the circuit.) For the normal output, we put this pin permanently on GND.

The PIN 10 MR = Master Reset (SRClr = Shift Register Clear) also has negative logic. Therefore, we put this connection on VCC to rule out a reset.

The voltage supply takes place to PIN 16 for VCC (3.3 or 5V) and PIN 8 for GND.

The outputs are called Q0 To Q7 (Pins 15 and 1 to 7).

The last PIN 9 (Q7S) is the outcome for cascading. Here DS of the next ICS is connected.

With the following circuit and the associated sketch, an integer number entered in the serial monitor (Signed Int, between -32768 and +32767) is displayed as a dual number with the help of 16 LEDs:

circuit diagram

For reasons of clarity, I left out the connections of the outputs to the anodes of the LEDs. Please connect the LEDs from left to right to Q0 To Q7

I would explicitly want to go to the green line from the left output Q again7S indicates DS to the right input. The cascadation takes place here.

 // This sketch displays a given decimal number (integer)
 // in binary format in 16 LEDs Connected to Two Sn74HC595N
 
 // Arduino-Pin Connected to SHCP of 74HC595
 intimately shiftpine = 8;
 // Arduino-Pin Connected to STCP of 74HC595
 intimately storepin = 9;
 // Arduino-pin connected to ds of 74hc595
 intimately datapine = 10;
 // This number should be read and output as a dual number
 intimately decimal;
 
 void set up() {
   // Initialize serial communication at 9600 Bits via second:
  Serial.Begin(9600);
  // define pins 8.9.10 AS outputs
  pin mode(storePin, OUTPUT);
  pinMode(shiftPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
 }
 
 void loop () {
 // important note: select "no line ending" = "kein Zeilenende"
 // at the bottom of the Serial Monitor.
 // Enter your number in the top line of the Serial Monitor
   if (Serial.available() > 0) {
     // read the incoming byte:
     decimal=Serial.parseInt();
 //   uncomment for display in the Serial Monitor
 //   Serial.print(decimal);
 //   Serial.println();
 //   for (int i=0; i<16;i++) {
 //     Serial.print(bitRead(decimal,i));
      }
 
   // storePin to LOW
   digitalWrite(storePin, LOW);
 
   for (int i=0; i<16; i++) {
   // start with all 3 Pins LOW
   // data transfer (1 bit) at the change from LOW to HIGH (positive-edge triggered)
   digitalWrite(shiftPin, LOW);
   // write value of the respective bit to DS pin
   digitalWrite(dataPin, bitRead(decimal,i));
  // ShiftPin SHCP from LOW to HIGH for data transfer to the sift register
  digitalWrite(shiftPin, HIGH);
  }
 
  // When all 16 bis are in shift registers put the StorePin STCP
  // from LOW to HIGH, in order to copy all 18 bits
  // from the shift registers to the storage registers for output
 
  digitalWrite(storePin, HIGH);
 }

Enter the decimal number you want to be displayed as a dual number in the top line of the serial monitor and press Enter or click on Send. After a short pause you will see the display on the LEDs. Try negative numbers as well.

This IC or functionally equivalent integrated circuits are often used when data is to be transmitted serially and output in parallel (simultaneously), e.g. in the LED or LCD displays. In addition, the SN74HC595N is used with the motor controller shield to operate two motor ICs L293D and thus four motors simultaneously despite the limited number of outputs of the microcontroller.

Another IC that is good for practicing reading, writing, and manipulating register contents is the port expander MCP23017, about which I had written a three-part blog in the summer of 2020
(
Part 1, Part 2, Part 3).

Projekte für anfänger

4 comments

Andreas Wolter

Andreas Wolter

@Bernd-Steffen Großmann: vielen Dank für diesen Hinweis. Wir haben die Abbildung des Schaltplans ausgetauscht.

Grüße,
Andreas Wolter
AZ-Delivery Blog

Bernd-Steffen Großmann

Bernd-Steffen Großmann

Hallo Herr Wolter, Sie haben uns da eine ziemliche Nuss zu knacken gegeben! Der Versuchsaufbau machte alles möglichen, nur nicht was er sollte! Ich hab zuerst an mir und dann am IC 74HC595 gezweifelt bzw. am Fernost-Lieferanten. Sie haben im Sketch den Schiebe-Takt (SHCP=Shift Register Clock Pin) an Pin 8 und Dateneingang an Pin 10 vom Arduino festgelegt. In der Schaltung ist es genau anders herum gezeichnet! Böse Falle…
Viele Grüße, Bernd-Steffen Großmann

Andreas Wolter

Andreas Wolter

@Frant: das ist kein Fehler, denn die Wertigkeit steigt von rechts nach links pro Ziffer um den Faktor 2, also jedes mal verdoppelt. Faktor 10 wäre pro Stelle mit 10 multipliziert, was eine Folge von 1, 10, 100, 1000, … etc. ergeben würde. Ich habe die Wortwahl trotzdem etwas angepasst, um Missverständnisse zu vermeiden.

Grüße,
Andreas Wolter
AZ-Delivery Blog

Frant

Frant

!!! Fehler!!!
Wie wir gesehen haben, steigt bei den Dezimalzahlen die Wertigkeit einer Ziffer, je weiter links sie in der Ziffernfolge steht, um den Faktor 2, bei Hexadezimalzahlen um den Faktor 16.
richtig ( Faktor 10 statt 2)
Wie wir gesehen haben, steigt bei den Dezimalzahlen die Wertigkeit einer Ziffer, je weiter links sie in der Ziffernfolge steht, um den Faktor 10, bei Hexadezimalzahlen um den Faktor 16.

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery