1. GPIO Introduction
1) Introduction: GPIO: (General Purpose Input Output (General Purpose Input/Output) is referred to as GPIO)
basic structure:
The stm32f407VGT6 microcontroller has a total of 5×16 PA-PE multiplexed IO ports, totaling 80.
Each general purpose I/O port has four 32-bit configuration registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit set/reset register (GPIOx_BSRR), a 32-bit lock register (GPIOx_LCKR) and two 32-bit alternate function selection registers (GPIOx_AFRH, GPIOx_AFRL), as shown in the figure:
2) GPIO working mode:
2. Detailed explanation of the eight working modes of GPIO
Floating Input_IN_FLOATING
With pull-up input_IPU
With pull-down input_IPD
Analog Input_AIN
Open drain output_OUT_OD
Push-pull output_OUT_PP
Open drain multiplexed output_AF_OD
Push-pull multiplexed output_AF_PP
4 inputs + 2 outputs + 2 multiplexed outputs, a total of 8 modes. Here are the working principles of the eight modes:
GPIO floating input_IN_FLOATING mode working principle
The above screenshot is the schematic diagram of the floating input mode.
The shaded part in the figure is not working in the floating input mode, especially the output circuit in the lower part. In fact, the output circuit is isolated from the input port at this time.
The yellow highlighted part shows the data transmission channel. The external level signal enters the STM32 through the I/O port numbered 1 on the left, and is shaped by the Schmitt trigger numbered 2 and sent to the "input data register" numbered 3. At the other end of the "input data register" (numbered 4), the CPU can read the level change status of the I/O port at any time through the internal data bus.
GPIO with pull-up input_IPU mode working principle:
The above figure is the schematic diagram of STM32's GPIO with pull-up input mode.
Compared with the floating input mode introduced earlier, only a pull-up resistor is connected to the data channel. According to the STM32 data sheet, the resistance of this pull-up resistor is between 30K and 50K ohms.
Similarly, the CPU can read the state of the level change of the I/O port at any time at the other end of the "input data register" through the internal data bus.
GPIO with pull-down input_IPD mode working principle:
For the input in pull-down mode, a pull-down resistor is connected at the bottom of the data channel.
According to the STM32 data sheet, the resistance of this pull-down resistor is also between 30K and 50K ohms.
To add a pull-up or pull-down resistor:
1. When used as an input for the microcontroller, suppose we connect a button directly to the ground (or power supply) at the IO port.
Because the pins are floating when the button is pressed and not pressed, it is difficult for the microcontroller to detect whether the button is pressed.
Therefore, a pull-up (or pull-down) is manually connected to determine the state of the IO input level when it is not pressed.
2. Can improve the chip's anti-interference ability
3. When the IO port of the microcontroller is used for output, if no pull-up resistor is connected, it can only provide current sinking. It cannot output current to drive external devices. At this time, the pull-up resistor also needs to be considered. This will make the IO output high level.
GPIO analog input_AIN mode working principle:
If the STM32 is configured in analog input mode, the working principle is relatively simple. The signal enters the AD module of the STM32 microcontroller from the port numbered 1 on the left and directly from the end numbered 2 on the right.
Careful friends can see that the pull-up and pull-down resistors and Schmitt triggers in the data channel are all in the off state at this time, and the "input data register" cannot reflect the state of the level change on the IO port. In other words, in the analog input state, the CPU cannot read the changed data of the IO port through the "input data register".
The above analysis is the working principle of the input mode of the GPIO module IO pin. The following introduces the working principle of the GPIO output mode.
GPIO open-drain output_OUT_OD mode working principle
The above figure is the working principle diagram of GPIO open-drain output mode
When the CPU writes data to the number 1 terminal through the "bit set/clear register" or "output data register"
The data bit will be transmitted to the I/O port number 4 through the output control circuit number 2.
If the CPU writes a logic "1", the N-MOS tube number 3 will be in the off state.
At this time, the level of the I/O port will be determined by the external pull-up resistor
If the CPU writes a logic "0", the N-MOS tube number 3 will be turned on.
At this time, the level of the I/O port is pulled to the zero potential of "ground" by the N-MOS tube numbered 3.
In the upper part of the figure, the Schmitt trigger is in the on state
This means that the CPU can monitor the status of the I/O port at any time on the other side of the "input data register".
This feature also enables bidirectional communication of virtual I/O ports: if the CPU outputs a logic "1"
Since the N-MOS tube No. 3 is in the off state, the level of the I/O port will be completely determined by the external circuit
Therefore, the CPU can read the signal of the external circuit in the "input data register" instead of the logic "1" output by itself.
In the output mode of the GPIO port, there are 3 output speeds to choose from (2MHz, 10MHz and 50MHz)
This speed refers to the response speed of the GPIO port driver circuit, not the speed of the output signal.
The speed of the output signal is related to the program (the chip has multiple output drive circuits with different response speeds arranged in the output part of the I/O port
Users can choose the appropriate drive circuit according to their needs).
By selecting the speed to choose different output driver modules, the best noise control and power consumption reduction can be achieved.
High-frequency drive circuit, high noise
When our project does not require a relatively high output frequency, please choose a low-frequency drive circuit, which is very helpful in improving the EMI performance of the system.
Of course, if our project requires the output of a higher frequency signal, but a lower frequency driver module is selected, it is likely to get a more distorted output signal.
GPIO push-pull output_OUT_PP mode working principle
The push-pull output mode of GPIO is based on the open-drain output mode. After the "output control circuit", a P-MOS tube is added.
When the CPU outputs logic "1", the P-MOS tube at No. 3 is turned on, and the N-MOS tube below is turned off, achieving the purpose of outputting a high level.
When the CPU outputs logic "0", the P-MOS tube at No. 3 is turned off, and the N-MOS tube below is turned on, achieving the purpose of outputting a low level.
In this mode, the CPU can still read the voltage change signal of the IO port from the "input data register".
GPIO open-drain multiplexed output_AF_OD mode working principle
The working principle of GPIO open-drain multiplexing output mode is basically the same as that of open-drain output mode.
The difference is that the source of input number 2 is different, it is connected to the output of the multiplex function
At this time, the "output data register" is disconnected by the output channel.
From the above figure, we can also see that the CPU can also read the changing level signal of the external IO port from the "input data register".
GPIO push-pull multiplexing output_AF_PP mode working principle
Finally, let's introduce the working principle of GPIO push-pull multiplexing output mode.
The input of No. 2 "output control circuit" is connected to the output terminal of the multiplexing function
At this time, the "output data register" is disconnected from the output channel, and the output signal of the on-chip peripheral is directly connected to the input of the "output control circuit".
After we configure the GPIO to multiplex output function, if the corresponding peripheral module is not activated, the output of the IO port will be uncertain.
The other principles are the same as the previous mode, including the way to read the "input data register".
The marquee is connected to 4 PIN pins: PD12, PD13, PD14, PD15
3. Marquee library function programming
1) Required library function files:
Header file: stm32f4xx_gpio.h
Source file: stm32f4xx_gpio.c
2) Important functions
1 initialization function:
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef*GPIO_InitStruct);
2 functions to read input level:
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_tGPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
2 functions to read output level:
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
4 functions for setting output level:
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitActionBitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
3) GPIO_Init initialization example
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //Enable GPIOF clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13; //LED0 and LED1 correspond to IO ports
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //Normal output mode
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOF, &GPIO_InitStructure); //Initialize GPIOD12,13
4) Read input level function
2 functions to read input level:
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_tGPIO_Pin);
Function: Read the input level of a GPIO. The actual operation is on the GPIOx_IDR register.
For example:
GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5); //Read the input level of GPIOA.5
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
Function: Read the input level of a certain group of GPIO. The actual operation is on the GPIOx_IDR register.
For example:
GPIO_ReadInputData(GPIOA); //Read the input level of all IO ports in the GPIOA group
5) Read output level function
uint8_t GPIO_ReadOutputDataBit (GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin);
Function: Read the output level of a GPIO. The actual operation is on the GPIO_ODR register.
For example:
GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_5); //Read the output level of GPIOA.5
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
Function: Read the output level of a certain GPIO group. The actual operation is on the GPIO_ODR register.
For example:
GPIO_ReadOutputData(GPIOA); //Read the output level of all IO ports in the GPIOA group
6) 4 functions for setting output level
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
Function: Set the output of a certain IO port to high level (1). Actual operation BSRRL register
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_tGPIO_Pin);
Function: Set the output of a certain IO port to low level (0). The BSRRH register for actual operation.
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin,BitAction BitVal);
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
These two functions are not commonly used and are also used to set the output level of the IO port.
The above is the use of the entire GPIO library function. Let’s summarize the steps of the GPIO library function:
1) Enable the IO port clock. Call the function RCC_AHB1PeriphClockCmd();
Different peripherals may call different clock enable functions
As shown in the figure: GPIO is on the AHB1 bus
2) Initialize the IO port mode. Call the function GPIO_Init();
3) 3) Operate the IO port and output high and low levels.
GPIO_SetBits();
GPIO_ResetBits();
Source code:
Led.h
#ifndef_LED_H_H_H
#define_LED_H_H_H
#include"stm32f4xx_gpio.h"
#include"stm32f4xx_rcc.h"
#defineLED_GREEN GPIO_Pin_12
#defineLED_ORANGE GPIO_Pin_13
#defineLED_RED GPIO_Pin_14
#defineLED_BLUE GPIO_Pin_15
#defineLED_ON 1
#defineLED_OFF 0
voidLED_Init(void);
voidLED_Operate(uint16_t GPIO_Pin,int32_t operate);
#endif
Led.c
#include"led.h"
voidLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12 |GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD,&GPIO_InitStruct);
}
voidLED_Operate(uint16_t GPIO_Pin,int32_t operate)
{
if(LED_ON == operate)
{
GPIO_SetBits(GPIOD,GPIO_Pin);
}
else if(LED_OFF == operate)
{
GPIO_ResetBits(GPIOD,GPIO_Pin);
}
}
Main.c
#include"led.h"
voidUser_Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
intmain(void)
{
LED_Heat();
while(1)
{
LED_Operate(LED_GREEN,LED_ON);
User_Delay(0x3FFFFF);
LED_Operate(LED_GREEN,LED_OFF);
LED_Operate(LED_ORANGE,LED_ON);
User_Delay(0x3FFFFF);
LED_Operate(LED_ORANGE,LED_OFF);
LED_Operate(LED_RED,LED_ON);
User_Delay(0x3FFFFF);
LED_Operate(LED_RED,LED_OFF);
LED_Operate(LED_BLUE,LED_ON);
User_Delay(0x3FFFFF);
LED_Operate(LED_BLUE,LED_OFF);
User_Delay(0x3FFFFF);
}
}
Previous article:【stm32f407】Key application of round sequence method
Next article:【stm32f407】library function
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Unboxing the newly purchased PIC Kit4, looking forward to its improved functions
- EEWORLD University Hall----Live playback: AVNET uses on-chip CIP and intelligent analog to build complex embedded control functions
- Step 2: Power supply project establishment, DIY high-efficiency bidirectional DC-DC converter from scratch!
- TMC2300-LA dual-phase stepper motor chip can replace DRV8846
- Urgent! Urgent! Urgent! Can the teacher provide a 50A/1000W mature constant current source circuit. The adjustable constant current source can be controlled by a single chip microcomputer.
- 【K210 Series】2. Download tool and latest firmware for K210
- TI's Arm Cortex-A8 family with 3D graphics, industrial Ethernet and flexible peripherals
- DSP temperature control device
- C6455 EMIFA initialization error
- Small form factor Bluetooth Low Energy (BLE) reference design