Geekly Articles each Day

In the last lesson we looked at where to start if you decided to study STM32 microcontrollers: how to set up an IDE, how to create a simple project, how to compile a program, and how to run a program. After admiring the winking of the LEDs on the Discovery board)

Starting a new article, I planned to immediately go to a detailed analysis of the listing of the program, which forced our LEDs to alternately wink at each other, but having already started writing, I suddenly realized that there were a large number of questions without an answer. prematurely. And for myself, I identified a whole list of such issues. :

- What is
? How to use them?*bitwise operations* - What are
and how are they related to bit operations?*registers* STM32F0xx-series, how is*What are the microcontrollers of the*performed and how is life inside the MC ensured?*clocking*- How does
occur, why do you need a*the initial MK initialization*, what does the*startup file*function*SystemInit*? Explanation on the fingers.*do* - What does
? How to navigate it? What useful can be extracted from it and how to use it?*the CMSIS library consist of*

It is with the consideration of these issues, I would like to continue the story about programming STM32.

List of articles:

- We start to learn STM32 or control the light rationally
- We start to study STM32: bit operations
- Starting to learn STM32: What are registers? How to work with them?

Just starting to study microcontrollers, the words “register” and “bit operations” seemed mysteriously mysterious to me and for a long time I did not want to move on to the consideration of this topic. But when I more or less figured out what it was, I realized that I wasn’t putting the study of such an important topic in the back box for nothing. Bit operations are probably the most common operations in the microcontroller and knowing how and why they can be used in our work will open up a huge potential for us to control everything and everything in our MC!

At school, we all learned computer science lessons about what digital technology is, why it is called that, what basic logic operations exist. All modern digital technologies are based on binary mathematics and logic circuits.

The microcontroller always operates with only two states: “zero” means no voltage, “unit” means voltage. Let's refresh in the head a little knowledge of the basic logical operations since they form the basis of all digital technology.

- denoted as*Conjunction*or*"Logical AND"*.*"Logical multiplication*In essence, the result from performing this logical operation of two for expressions A and B is similar to their multiplication. That is, the expression will take the value “1” in the case only if both A and B have the value “1”. In all other cases, the value will be "0". May be designated as*"**AND, &&, AND, &*- denoted as*Disjunction*or*"Logical OR"*.*"Logical addition*The result of performing this logical two operation for expressions A and B is similar to their addition. That is, the expression will take the value “1” in the event that at least one of the expressions A and B have the value “1”. May be designated as*"**OR, ||, OR, |.*- denoted as*Inversion*or*“Logical NOT”*. The result of performing this logical two operation for the expression A is the opposite. That is, the expression will take the value 1 if the expression A is 0 and vice versa. May be*“Denial”**NOT,!, NOT, ~.*- denoted as*Strict disjunction*or*“Exclusive OR”*. The result from the execution of this logical operation two for the expressions A and B will take the value 1 if A and B have different values. May be referred to as*“Logical addition, excluding OR”**Excl.**OR, XOR, ^.*

** Bit operations** are practically the same as logical operations with the only difference that they apply to bits and numbers of the binary system.

By the way, for ease of studying bit operations, I used the 32-bit ASM Calculator program from ManHunter . With this program you can check the results of bit operations, transfer numbers from one number system to another. The program has an intuitive interface and after meeting the program has become one of the main tools in my work with microcontrollers. A small explanation of the program interface is given in the image below:

**Bit operation "NOT" - "~"**

If the bit is equal to "1", then after the operation "NOT" it will be equal to "0", and vice versa. The operation is immediately performed on all bits of the binary number. For example, invert the number FF:

**Bit operation "And" - "&"**

If both bits in the bit are equal to "1", then after performing the operation "And" the result in the bit will be equal to "1", but if at least one of the bits is equal to "0" then the result will be equal to "0". The operation is also performed one by one. For example, “multiply” two numbers 0xFF0 and 0xF0F:

As a result, we will see that in those digits where there were ones in both numbers, the result was ones, in all other cases - zeros.

Consider the options for practical application:

- In a situation, if we need
we can use a mask. I think it will be clearer to show it with an example. Suppose we take the number and 0xF8F and we need the 7th bit to become zero instead of one. No problem, we put on the mask and remove the tick from the desired bit. Multiply the numbers and get the result:*to reset a specific bit or group of bits to zero,* - If we need
- we also use the mask to multiply. In the mask, we set the bit that we would like to check. If the required bit is "0" - then the result of the calculation will be "0", if "1" then, respectively, "1". If we want to find out if the 7th bit of the unit is equal, we make the corresponding mask and multiply our number by the mask. It's simple:*to check a specific bit in the number to 0 or 1*

If we need(meaning the ability of a number to divide by two) - then we check the 1st bit in the same way, if it is equal to "1" - then the number is odd, if there is "0" then the number is even. Try it yourself, in order to learn and develop skills, to do this test.*to check the parity of a number*

**Bit operation "OR" - "|"**

If one or both of the pair of bits equals "1", then the result will be "1", otherwise if both bits are equal to "0", then the result will be equal to "0". That is, roughly speaking, the addition of all units in the digits. For example, if we add two numbers 0xF8F and 0x7F, we get the following result:

Consider the option of practical application:

- If we need to
- we also use the mask to add. For example, to set the 15th bit in the number 0xFF0 you need to perform a logical addition operation and we will get the desired result:*set a specific bit in the number to 1*

Try yourself to play with different numbers and watch the results.

**Bit operation "EXCLUSIVE OR" - "^"**

If the bits in the bit are different and not equal then the result will be “1”, otherwise “0”. For example, if we make XOR numbers 0xF8F and 0x7F, then we will see that in the bits in which there are excellent bits, then the result is “1” and in places where the bits are the same, either “0” or “1” - we get “0 ", In the end we get the following result:

Consider the options for practical application:

- If we needed to
, we can easily do this using a mask using the XOR operation. Let's make the inversion of 6 and 7 digits in the number 0xF8 using the mask 0xC0. You can see the result on the image:*invert any bits in a number* - There are situations when it is necessary to
. In this case, we need register values to undergo XOR operations. If the result is “0”, then the registers are equal, otherwise they are not equal:*compare two registers and determine whether they are equal or not*

**Bit shift operations**

There are a number of interesting and sometimes extremely useful bit operations referred to as ** shift operations** . Discharges can be moved either to the right or to the left. During this operation, all digits of the binary number are shifted by the specified number of positions, while if the shift goes to the

**Shift left - "<<"**

You can see how the shift occurs in the image below. I think that everything is quite obvious:

With a binary shift, you can notice one interesting feature. Shift by one digit multiplies our number by 2. If we shift our number x by n digits, we get x * (2 * n). Try to track this pattern yourself through our scrap for counting. =)

**Shift to the right - ">>"**

The result of the shift to the right is quite clearly reflected in the image:

With a binary shift to the right, you can see that the situation is reversed to a shift to the left - the number is divided by 2 seconds when shifted by 1 bit and then by 2 * n, where n is the number of digits by which the shift was made. Also try to play with numbers yourself and which are obviously divided into 2 completely. And the question on filling - what result will be if you divide in this way an odd number?

To many beginners, this topic may seem wildly boring and you may get the feeling that well, it’s not at all clear where and how this knowledge can be applied. I hasten to encourage you, in situations when you need to raise one or another leg of the MK or write a parameter to a thread of a peripheral block or module - there will be knowledge of bit operations everywhere and everywhere. Since the article turned out to be quite voluminous, we will move the consideration of registers to the next lesson. Well, in the future, you can use this article as a cheat sheet.

As a ** home task,** try to independently disassemble the code of our program in the

List of articles:

Source: https://habr.com/ru/post/406889/