We start to study STM32: bit operations

A small digression ...

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. :

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

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

List of articles:
  1. We start to learn STM32 or control the light rationally
  2. We start to study STM32: bit operations
  3. Starting to learn STM32: What are registers? How to work with them?

Basic logical operations

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.

Bit operations

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:

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:

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:

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 left - the most significant bit (leftmost) is lost, and the least significant (rightmost) is written "0". With a logical shift to the right, the opposite situation occurs - the low bit (the rightmost one) is lost, and “0” is written to the high bit. Additionally, I would like to note that in the case of 32-bit words, all 32 bits are shifted entirely. Consider shift operations in more detail.

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?

Important note . If you make a shift for a variable with a negative sign (signed), the vacant positions will be filled with ones.

As a conclusion ...

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 while (1) {...} block and understand how we turn on and off our LEDs using bit operations . Well, in the next lesson I will tell you how it really happens!

List of articles:
  1. We start to learn STM32 or control the light rationally
  2. We start to study STM32: bit operations
  3. Starting to learn STM32: What are registers? How to work with them?

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

All Articles