Detailed explanation of GPIO in STM32 learning

Publisher:Ziran520Latest update time:2018-11-20 Source: eefocusKeywords:STM32  GPIO Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

GPIO:


STM32 has 3 (64-pin) IO ports, namely PA, PB, and PC.


The IO ports of STM32 can be configured into 8 modes by software:


1. Input floating


2. Input pull-up


3. Input drop-down


4. Analog Input


5. Open drain output


6. Push-pull output


7. Push-pull multiplexing function


8. Open-drain multiplexing function


Each IO port of STM32 is controlled by 7 registers. They are: 2 32-bit port configuration registers CRL and CRH in configuration mode; 2 32-bit data registers IDR and ODR; 1 32-bit set/reset register BSRR; 1 16-bit reset register BRR; 1 32-bit latch register LCKR; There are only 4 IO port registers we commonly use: CRL, CRH, IDR, ODR.


Note (When configuring STM32 peripherals, always enable the clock of the peripheral first! APB2ENR is the peripheral clock enable register on the APB2 bus)


1.CRL and CRH registers-->control whether the IO port is output or input.


The CRL of STM32 controls the mode of the lower 8 bits of each IO port (A~G) (for example, PA0-PA7). Each IO port occupies 4 bits of CRL, the upper two bits are CNF, and the lower two bits are MODE. Here we can remember several commonly used configurations, such as 0X4 for analog input mode (for ADC), 0X3 for push-pull output mode (for output port, 50M rate), 0X8 for pull-up/pull-down input mode (for input port), and 0XB for multiplexed output (using the second function of the IO port, 50M rate).


The CRH of STM32 controls the mode of the upper 8 bits of each IO port (A~G) (for example, PA8-PA15). Each IO port occupies 4 bits of CRH, the upper two bits are CNF, and the lower two bits are MODE.


2. IDR register and ODR register--"Read the content of the pin input (high or low) and control the content of your output (high or low)


IDR is a port input data register, which uses only the lower 16 bits. This register is a read-only register and can only be read in 16-bit form. The read value is the status of the corresponding IO port.


ODR is a port output data register, and only the lower 16 bits are used. Although this register is readable and writable, the data read from this register are all 0. Only writing is valid. Its function is to control the output of the port.


3. BSRR port bit set/clear register and BRR port reset register


I have used the BSRR register for a while, and I don't know how to use other registers. I just feel that it is very easy to use. It is very convenient to use. For example, if you have configured the port and want PA5 to output "1", then GPIOA->BSRR |= (1<<5); 5 is the corresponding register. It outputs "0", the same.


GPIO->BSRR |=(1<<(5+16)); You know why 16 is added.


The BRR and BSRR operations are the same.


When using ODR to operate the PC port, because the main program and interrupts operate the IO port at the same time (the main program sets PC3, and the interrupt sets PC6), using ODR to set it may cause unexpected situations. Online debugging shows that the values ​​of the GPIO registers are both correct, but the actual output is incorrect. If ODR is not used, and BRR and BSRR are used to implement the IO port settings, this problem will not occur and everything will be correct.


When referring to the manual describing BRR and BSRR, I found a few sentences (in the GPIO chapter):


"Each I/O port bit is freely programmable, however the I/O port registers must be accessed as 32-bit words (halfword or byte accesses are not allowed). The GPIOx_BSRR and GPIOx_BRR registers allow independent access to read/change any GPIO register; this way, there is no hazard if an IRQ is generated between read and change accesses."


"When programming individual bits of GPIOx_ODR, software does not need to disable interrupts: in a single APB2 write operation, only one or more bits can be changed. This is done by writing a '1' to the bits you want to change in the 'Set/Reset Register' (GPIOx_BSRR, for reset it is GPIOx_BRR). The bits not selected will not be changed."


The final conclusion is that if you need to set the IO port in the interrupt, it is best to use BSRR and BRR operations instead of ODR.


Keywords:STM32  GPIO Reference address:Detailed explanation of GPIO in STM32 learning

Previous article:What are AHB bus, APB2 bus and APB1 bus in STM32?
Next article:How to port uCGUI to STM32, see here!

Recommended ReadingLatest update time:2024-11-16 14:34

stm32 UCGUI perfect transplantation
        UCGUI is a graphics support system for embedded applications. It is designed to provide an efficient graphical user interface independent of the processor and LCD controller for any application using LCD graphics display. It is suitable for single-task or multi-task system environments, and for real or virtual
[Microcontroller]
My MCU Methodology 2: Special Edition of STM32 MCU
My MCU Methodology Part 2 STM32 MCU Special Edition Written by zzw YanJun.tech Because I was really busy during this period, and of course because of my procrastination, I have not started writing this STM32 microcontroller learning experience. On the afternoon of the third day of the Chinese New Year, I was a little
[Microcontroller]
STM32 - TIM2 timer timing
There are 11 timers in STM32, including 2 advanced control timers, 4 ordinary timers and 2 basic timers, as well as 2 watchdog timers and 1 system tick timer. The system tick timer is SysTick described in the previous article, and the watchdog timer will be studied in detail later. Today, we mainly study the remaining
[Microcontroller]
STM32 - TIM2 timer timing
STM32 USART configuration
//USART void UART2_Configuration(void) {     GPIO_InitTypeDef GPIO_InitStructure;     USART_InitTypeDef USART_InitStructure;     USART_ClockInitTypeDef USART_ClockInitStructure;      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;     GPIO_InitStructure.GPIO_Speed =
[Microcontroller]
New STMicroelectronics Demonstration Board Helps Advanced Industrial and Consumer Electronics Manufacturers Accelerate Dual-Motor Design
Highly integrated motor controller STSPIN32G4 relies on the STM32 ecosystem to accelerate product development cycle May 31, 2024, China - STMicroelectronics' EVSPIN32G4-DUAL demonstration board can control the operation of two motors with only one highly integrated motor driver STSPIN32G4, acceler
[Industrial Control]
New STMicroelectronics Demonstration Board Helps Advanced Industrial and Consumer Electronics Manufacturers Accelerate Dual-Motor Design
stm32 implements GPIO input key detection
1. Hardware design When the mechanical contact of the key is disconnected or closed, due to the elastic effect of the key contact, the key switch will not be immediately stably connected or disconnected. When the key is used, a ripple signal as shown in the figure below will be generated, which requires software de-
[Microcontroller]
stm32 implements GPIO input key detection
STM32 input capture pulse width and frequency calculation
Input capture mode can be used to measure pulse width or frequency. Except for TIM6 and TIM7, all other timers of STM32 have input capture function. The following is the calculation of pulse width and frequency. 1. Pulse width As shown in the figure below, to collect the width of the high-level pulse, you only need t
[Microcontroller]
STM32 input capture pulse width and frequency calculation
STM32 serial port (UART) and serial communication principle
1. Communication interface introduction     1. Two ways for the processor to communicate with external devices:               Parallel communication     - Transmission principle: Each bit of data is transmitted simultaneously.                 - Advantages: Fast speed                 - Disadvantages: occupies
[Microcontroller]
STM32 serial port (UART) and serial communication principle
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号