Use BSRR and BRR registers to directly operate the STM32 I/O ports

Publisher:chunyingLatest update time:2015-04-15 Source: eechinaKeywords:BSRR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Each GPIO port of STM32 has two special registers, GPIOx_BSRR and GPIOx_BRR registers, through which the corresponding GPIOx port can be directly set to '1' or '0'.

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;
Keywords:BSRR Reference address:Use BSRR and BRR registers to directly operate the STM32 I/O ports

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

Several methods of measuring frequency and duty cycle using STM32
Platform used: official STM32F429DISCOVERY development board, 180MHz main frequency, timer frequency 90MHz. Related topics: (1) Measure the pulse signal frequency f_O in the frequency range of 10Hz to 2MHz, and the absolute value of the measurement error is no more than 0.1%. (15 points) (2) Measure the duty cycle D o
[Microcontroller]
Several methods of measuring frequency and duty cycle using STM32
STM32 Series No. 30--DHT11 Temperature and Humidity Sensor
Electrical characteristics: Working voltage: 3.3V-5.5V Working current: average 0.5mA Output: Single bus digital signal Measuring range: humidity 20~90%RH, temperature 0~50℃ Accuracy: humidity ±5%, temperature ±2℃ Resolution: humidity 1%, temperature 1℃ Its data packet consists of 5Byte (40Bit). The data is div
[Microcontroller]
STM32 Series No. 30--DHT11 Temperature and Humidity Sensor
Redirection problem of printf and scanf in STM32
Regarding the redirection problem of printf and scanf in STM32, here I only organize the situation where "USE MircoLIB" is not used (for Keil RVMDK development environment). ①: First, you need to include the "stdio.h" header file in usart.h ②: In usart.c, add the following code block to avoid using semihost mode and
[Microcontroller]
stm32 usb_istr.c file analysis
I recently worked on a project, in which a part of the internal part of the stm32 is used to store my own code, and a space is opened up to store the FPGA code. Every time the machine is turned on, the stm32 writes code to the FPGA. The method of writing code to a certain space of stm32: (1) ST_LINK Utility software
[Microcontroller]
stm32 usb_istr.c file analysis
How does STM32 enter the interrupt service function xxx_IRQHandler
Today I was looking at the interrupt of stm32. For a moment, I didn't understand how the stm32 main function entered the interrupt function. According to the understanding of C programming, there would be a specific entry or something like that, but I didn't find the entry during the demo. Taking the serial port int
[Microcontroller]
CAN network development notes based on STM32: filter configuration and ID setting
    I have read many articles about configuring filters and setting IDs (StdID ExtID), but found that they all have problems. After my own experimental tests, the results are as follows: (1) Set ID    If you want to use StdID, ExtID can be set at will, just configure StdID correctly. At the same time, you need to set
[Microcontroller]
CAN network development notes based on STM32: filter configuration and ID setting
STM32 single-line serial port control of bus servo
1 Introduction to bus servos Bus servo servos are serial bus intelligent servos, which can actually be understood as derivatives of digital servos . Compared with analog servos , digital servos are a subversion of control system design, while bus servo servos are a subversion of servos in terms of function and applic
[Microcontroller]
STM32 single-line serial port control of bus servo
STM32 serial port interrupt summary
This article takes USART1 as an example to describe the programming process of serial port interrupt. 1. Let's first talk about some library files involved in applying serial port interrupts. First of all, for the application programming of STM32 peripheral library files, misc.c and stm32f10x_rcc.c must be added. Next
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号