Each of the upper 16 bits of GPIOx_BSRR corresponds to each bit of port x. If a certain position in the upper 16 bits is '1', the corresponding bit of port x is cleared to '0'; if the position in the register is '0', it has no effect on the corresponding bit. Each of the
lower 16 bits of GPIOx_BSRR also corresponds to each bit of port x. If a certain position in the lower 16 bits is '1', the corresponding port bit is set to '1'; if the position in the register is '0', it has no effect on the corresponding port.
Simply put, the upper 16 bits of GPIOx_BSRR are called clear registers, and the lower 16 bits of GPIOx_BSRR are called set registers. Another register GPIOx_BRR has only the lower 16 bits valid, and has the same function as the upper 16 bits of GPIOx_BSRR.
Give an example to illustrate how to use these two registers and the advantages they embody. For example, the 16 IOs of GPIOE are all set to output, and each operation only needs to change the lower 8 bits of data while keeping the upper 8 bits unchanged. Assuming that the new 8 bits of data are in the variable Newdata,
this requirement can be achieved by operating these two registers. There are two functions GPIO_SetBits() and GPIO_ResetBits() in the STM32 firmware library that use these two registers to operate the port.
The above requirements can be achieved as follows:
GPIO_SetBits(GPIOE, Newdata & 0xff);
GPIO_ResetBits(GPIOE, (~Newdata & 0xff));
You can also directly operate these two registers:
GPIOE->BSRR = Newdata & 0xff;
GPIOE->BRR = ~Newdata & 0xff;
Of course, you can also complete the operation of 8 bits at a time:
GPIOE->BSRR = (Newdata & 0xff) | (~Newdata & 0xff)<<16;
From the last operation, it can be seen that using the BSRR register, 8 port bits can be modified at the same time.
If the BRR and BSRR registers are not used, the above requirements need to be implemented as follows:
GPIOE->ODR = GPIOE->ODR & 0xff00 | Newdata;
Using the BRR and BSRR registers can easily and quickly implement operations on certain specific bits of the port without affecting the status of other bits.
For example, if you want to quickly flip bit 7 of GPIOE, you can do:
GPIOE->BSRR = 0x80; // set to '1'
GPIOE->BRR = 0x80; // set to '0'If
you use the conventional 'read-modify-write' method:
GPIOE->ODR = GPIOE->ODR | 0x80; // set to '1'
GPIOE->ODR = GPIOE->ODR & 0xFF7F; // set to '0'Some
people ask whether the upper 16 bits of BSRR are redundant. Please see the following example:
If you want to set bit 7 of GPIOE to '1' and bit 6 to '0' in one operation, it is very convenient to use BSRR:
GPIOE->BSRR = 0x4080;
If there is no upper 16 bits of BSRR, it will be divided into two operations, resulting in the changes of bit 7 and bit 6 being out of sync!
GPIOE->BSRR = 0x80;
GPIOE->BRR = 0x40;
Previous article:3D graphics acceleration system based on ARM+FPGA architecture
Next article:Input filtering mechanism of STM32 timer
Recommended ReadingLatest update time:2024-11-17 01:44
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- GD32E231 Learning 5: Color Sensor Module GY-31 Test
- Can modules be used in electric racing?
- Live broadcast has ended | ST wide bandgap high-performance silicon carbide and gallium nitride product technology and different application cases
- Huawei P50 series released: the world's first 3D nano-crystal mobile phone
- How to test the continuity of the communication harness?
- 422 to Ethernet
- Thermocouple Calibration Methods
- Please help me with a slightly more detailed analysis of this picture
- FAQ_How to choose TCXO for S2-lp
- [CB5654 Intelligent Voice Development Board Review] First Look at the Intelligent Voice Development Board