STM32 has two registers that can control IO output, one is the ODR register, which only uses the lower 16 bits. By writing data to this register, the output level of a pin can be controlled.
The BSRR register is a port bit set/clear register. This register has similar functions to the ODR register, and can be used to set the GPIO port output. This register is divided into the upper 16 bits and the lower 16 bits. Writing 1 to a bit in the upper 16 bits clears the corresponding ODR register bit (output 0), and writing 0 has no effect. Writing 1 to a bit in the lower 16 bits sets the corresponding ODR register bit (output 1), and writing 0 has no effect.
It can be seen that both registers can control IO output. From the above, we can know that writing BSRR can actually affect the value of ODR register. So what is the difference between using these two registers to control output?
There is such a description in the ST manual (RM0090 page 266):
Each I/O port bit is freely programmable, however the I/O port registers have to accessed as 32-bit words, half-words or bytes. The purpose of the GPIOx_BSRR register is to allow atomic read/modify accesses to any of the GPIO registers. In this way, There is no risk of an IRQ occurring between the read and the modify access.
Each I/O port bit can be freely programmed, however the I/O port registers have to be accessed as 32-bit words, half-words or bytes. GPIOx_BSRR has atomic read/modify accesses to any of the GPIO registers. In this way, there is no risk of an IRQ occurring between the read and the modify access.
Note: In the manual of the STM32F1 series, I/O registers are only allowed to be accessed in 32-bit word mode, and half-word and byte access are not allowed. However, in the manual of the F4 series, as mentioned above, it is not written that half-word or byte access is not allowed.
Therefore, it is better to operate the IO output through the BSRR register.
In the firmware library provided by ST, two functions are provided to operate a certain IO:
1 2 | void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_pin);void GPIO_ReSetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_pin); |
The implementation of these two functions is achieved by manipulating the BSRR register.
Here is a little talk about the port control method of STM32: Traditional 51, AVR and other single-chip microcomputers control the port output high/low by writing 1/0 to the I/O control register. Obviously, the control method of STM32 is different from that of traditional single-chip microcomputers. It controls the output high/low by writing 1 in two different registers (actually the high and low bits of one register, for comparison). Writing 0 is invalid.
What are the benefits of doing this? For example, if I want a certain port to output high, I just need to write 1 to the corresponding bit of the register that controls the output high level, and ignore the remaining bits. For traditional microcontrollers, we have to read back the port status first, then carefully change the corresponding bits, and then write them back. In comparison, the operation of STM32 is much simpler, and the probability of error is much smaller.
The full text is finished.
Previous article:STM32|4-20mA output circuit
Next article:A preliminary study on the STM32F4 clock system (Part 1)
Recommended ReadingLatest update time:2024-11-16 23:52
- 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!
- 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
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【Share】 RV-STAR development board transplants Chrome's offline game Little Dinosaur
- Thank you for having you + you who helped me and myself who struggled
- [Project source code] IIC interface initialization Verilog code based on FPGA OV5640/OV7670
- The Dragon Boat Festival Chip Coin Exchange Event has begun! Zongzi, multimeter, Huawei router... are waiting for you!
- @PCB Engineer, here are some practical tips for Allegro drawing board
- Means to Improve the Anti-interference Ability of Single Chip Microcomputer
- What brand of microphone is this? Where can I buy it?
- Mir MYC-YT507 development board review: Development environment part 1: Linux C button control LED
- Essential for Qorvo GaN Design
- Please let me know the banned words, thank you!