A brief discussion on the port output mode of STM32

Publisher:心灵律动Latest update time:2017-02-19 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.


Keywords:STM32 Reference address:A brief discussion on the port output mode of STM32

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

How to solve the frequency of pwm generated by Stm32?
Question one: //TIM2 pre-scaling setting: 1MHZ, APB1 division factor 2, input to TIM3 clock is 36MHzx2 = 72MHz   TIM_PrescalerConfig(TIM2,71, TIM_PSCReloadMode_Immediate);   void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode) {   /* Check the parameters */   assert_param(IS_TIM_
[Microcontroller]
How to solve the frequency of pwm generated by Stm32?
STM32 new project steps
1. Create a new project and select the chip model. After selecting the chip, add the startup file .s file. 2. Add files to groups for classification: It includes ASM group, USR group, FWlib combined with CMSYS group. l The ASM group is a .s startup file; The USR group is the user program file group, which generall
[Microcontroller]
STM32 MCU Learning (4) Independent Watchdog Experiment
/* * * Software function: Independent watchdog experiment * */ #include "stm32f10x.h" #include "delay.h" /* 函数: void RCC_Configuration(void) Function: Configure system clock Parameters: None Returns: None / void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus; //Define the external high-speed crystal s
[Microcontroller]
Research on LF RFID Reader Based on STM32
    Radio Frequency Identification (RFID) is an automatic identification technology that has matured since the 1980s. RFID uses radio frequency for non-contact two-way communication to achieve identification purposes and exchange data. It mainly realizes contactless information transmission through spatial coupling (al
[Microcontroller]
Research on LF RFID Reader Based on STM32
Solution to the problem of STM32+ucosll program running away
The STM32 microcontroller transplanted the ucosll program and it ran fast after a few hours. ucsll creates 4 tasks, OSQPend() message queue task, 1s timing task, 2s timing task, and key scanning task. Among them, OSQPend() message queue task has the highest priority and is used to receive interrupt messages. Since the
[Microcontroller]
C source code for TMP101 temperature sensor based on STM32 I2C
       It took me almost a weekend to do this process. A small TMP101 really made me rack my brains. In the end, I even used an oscilloscope to directly observe the waveforms of SDA SCL. However, the use of the oscilloscope did correct a serious and low-level mistake of mine. During this period, I also searched for STM
[Microcontroller]
C source code for TMP101 temperature sensor based on STM32 I2C
STM32 input capture mode
Input capture mode can be used to measure pulse width or frequency. Except for TIM6 and TIM7, all other STM32 timers have input capture function. Simply put, STM32 input capture detects the edge signal on TIMx_CHx. When the edge signal changes (such as rising edge/falling edge), the current timer value (TIMx_CNT) is st
[Microcontroller]
STM32 IO port simulates serial communication
A while ago, I needed to use low baud rate serial communication (300 baud rate) when debugging a project. I found that under normal circumstances (PCLK1 clock frequency is 72M, PCLK2 clock frequency is 36M): the minimum baud rate of STM32's USART0 can only be set to 1200; and the minimum baud rate of USART1 can only be
[Microcontroller]
STM32 IO port simulates serial communication
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号