Experience and summary of register operation methods

The STM32 function library that has been in contact for a while is very convenient to use, but few processors have function libraries. In most cases, you have to operate the registers yourself, so you should not alienate the operation of the registers.

The operation of the register sometimes takes into account the different order of its different bits, because this may not achieve the expected effect, this is not a good summary, but the method of register operation is fixed.

Before that, we must first understand the difference between the logical operator (! && ||) and the bitwise operator (<< >> ~ | ^&). The operation of the register uses the bit operator, which is generally used for logical operators. Used in the judgment logic in the program.
Experience and summary of register operation methods

For example, define an 8-bit register (0xf0 is the address of the register):

#define REG 0xFF

1) Assign a single bit

(1) Set the 5th position of the register REG to "1"

REG |= (1 << 5);

(2) Clear bit 5 of register REG

REG &= ~(1 << 5);

(3) Set the 3rd and 5th positions of the register REG to "1"

REG |= (1 << 5) | (1 << 3);

(4) Clear bits 3 and 5 of register REG

REG &= ~( (1 << 5) | (1 << 3) );

The paragraph is summarized as follows:

Set a position to 1 and shift the bit to use the bitwise operation "|"

After a position 0 is shifted, the bitwise operation "&" is used.

2) Direct assignment

(1) Position 1, 2, 3, 5, 7 of register REG "1"

REG = 0x5E;

(ie assign the register REG1 to 1010 1110, this method is mostly used in the initialization)

(2) Position 1, 3, 5, and 7 of register REG are set to "1", and 0, 2 are set to "0".

U8 temp;

Tmep = REG;

Temp &= ~0x01; //Equivalent to temp &=~(1<<0) Clear bit 0

Temp |= (1 << 1);

Temp &= ~(1 << 2);

Temp |= (1 << 3);

Temp |= (1 << 5);

Temp |= (1 << 7);

REG = temp;


Green Light Film

Eye protection:The principle of chlorophyll defocusing is adopted to prevent the eyeball from focusing for a long time, effectively alleviate eye fatigue, and achieve better eyesight protection.

Anti-blue light, ultraviolet light:The Green Light Film can actively filter the target wavelength while providing a band in the range of 380-400nm. It can effectively block 100% harmful light.

Edge coverage:The soft material fully covers the edges of any device, so it can be affixed to curved screen and round edges, 100% provides excellent edge coverage, and there is no gap between the edges of the device.

Oleophobic and waterproof:The oleophobic coating surface of the Green Light Screen Protector can provide your phone with oleophobic and waterproof properties, which can prevent sweat, grease residue, and fingerprints.

Sensitive touch:The 0.14mm ultra-thin film does not interfere with the touch response, almost as if it does not exist. When using a mobile phone, the fingertips can be easily swiped, bringing you a comfortable gaming experience.

Green Light Film,Green Light Screen Protector,Anti Green Light Screen Protector,Anti-green Light Screen Protector

Shenzhen Jianjiantong Technology Co., Ltd. , https://www.jjthydrogelprotector.com