The specific application of C language bit operation in STM32f1 microcontroller programming

The specific application of C language bit operation in STM32f1 microcontroller programming

Preface: Familiar with bit operation is a necessary technology for learning STM32 entry, bit operation is particularly important in MCU programming, below I will introduce in detail the specific usage of several bit operations in C language in MCU programming:

One: bitwise and & (AND)
only when both of the two bits are 1, the result is 1, otherwise both are 0.
For example: we want to change the state of GPIOA, use & to change the value of some bits in its CRL register

GPIOA->CRL&=0XFFFFFF0F; //将第 4-7 位清 0

Convert this hexadecimal number to binary and perform & operation, we can see that F is 1111, only the fourth to seventh digits are all 0 (binary starts from the 0th position), so you get the new The value of it.

Two: bitwise exclusive OR^ (XOR)
only when the two bits are different, its result is 1, otherwise it is 0.
For example, suppose we want to invert all bits of the register

GPIOA->CRL^=0XFFFFFFFF; //所有位刚好相反

When the original bit is 1, it becomes 0, and when the original bit is 0, it becomes 1.

Three: bitwise OR | (OR)
Why should I put it in the third to illustrate?
Because there is an implicit relationship here: |=&+^ When
performing OR operation, only two bits are 0 and the result is 0, otherwise both are 1, so we can get this relationship through simple mathematical logic. Let's take a look at the application of bitwise operation in detail!

GPIOA->CRL|=0X00000040; //设置相应位的值,不改变其他位的值

This is performed on the bitwise AND of Example 1. The fourth to seventh bits are all 0, and the remaining bits are all 1. The final result of the operation here is that the remaining bits remain unchanged, and only the fourth to seventh bits The sixth place becomes 1.

Four: shift: shift left << shift right>> The
biggest feature of the shift operator is to improve the readability of the code. If you want to assign a value, you can use the simplest and easy-to-understand method to directly use = to assign. But why do you want to set a fixed value by shifting to the left instead of directly? In fact, this is to improve the readability and reusability of the code.
For example: if we set the pipipink bit (bit 4, 5) of the BSSR register to 1, we can directly

GPIOx->BSRR =0x00000030;//第4,5位为1

But in this case, it is very troublesome to change the value.
Might as well do

GPIOx->BSRR = (((uint32_t)0x01) << pipipink);//把指定位设置为1

In this way, the readability of the program is improved, and it is easy to manipulate bits.

Five:
Negation ~ means to complement its operand, that is, 0 becomes 1, and 1 becomes 0.
In the microcontroller programming, if we want to set a bit to 1, and the other bits to 0, we can also use the direct assignment method:
For example, the 0th bit of the 16-bit register SR is 0, and the remaining bits are 1:

TIMx->SR=0xFFFE;//第0位为0。

But this approach is also ugly and poorly readable. Take a look at the method of negation operation:

#define TIM_FLAG_Update ((uint16_t)0x0001)//宏定义
TIMx->SR = (uint16_t)~TIM_FLAG_Update;

The macro definition is used here. The 0th bit of the initial value of TIM_FLAG_Update is set to 1, and then the reverse operation is used, which is very readable.

Proficiency in bit operation is a necessary condition for learning MCU well, so this is the writing.
Follow-up will continue to update the blog about MCU programming, so stay tuned!

Guess you like

Origin blog.csdn.net/qq_51959247/article/details/113079862