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. :
It is with the consideration of these issues, I would like to continue the story about programming STM32.
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 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:
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:
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:
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:
Shift to the right - ">>"
The result of the shift to the right is quite clearly reflected in the image:
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!