当前位置:网站首页>Introduction to GPIO

Introduction to GPIO

2022-07-06 23:55:00 Xiao Hou_ two thousand and twenty-two

GPIO brief introduction

1. Introduce

GPIO Is the abbreviation of general input and output port , In a nutshell STM32 Controllable pins ,STM32 Chip GPIO Pins are connected to external devices , So as to realize external communication 、 Control and data acquisition functions .STM32 Chip GPIO Divided into many groups , Each group has 16 One pin , If the model is STM32F103VET6 The models of chips have GPIOA、GPIOB、GPIOC to GPIOE common 5 Group GPIO, The chip is a total of 1O0 One pin , among GPIO It's a big part of it , be-all GPIO All pins have basic input and output functions .

The most basic output function is made up of STM32 Control pin output high 、 Low level , Switch control is realized , If you put GPIO Pin into LED The lamp , Then you can control LED The light goes on and off , Pin into relay or triode , Then you can control the on-off of external high-power circuits through relays or triodes .

The most basic input function is to detect the external input level , If you put GPIO Pin to key , Distinguish whether the key is pressed by the level .

1.1 Protection diode and upper 、 Pull down resistance

** The two protection diodes of the pin can prevent too high or too low voltage input outside the pin ,** When the pin voltage is higher than VDD when , The diode above is on , When the pin voltage is below Vss when , The diode below is on , Prevent abnormal voltage from being introduced into the chip and causing the chip to burn . Despite this protection , It doesn't mean that STM32 The pin can be directly connected to the high power driver , Such as direct drive motor , Forced drive or the motor doesn't work , Or cause the chip to burn out , It is necessary to increase the power and isolate the circuit drive .

1.2 P-MOS Tube and N-MOS tube

GPIO The pin line passes through two protection diodes , Up to “ The input mode ” structure , Down to “ The output mode ” structure . Let's look at the output mode section first , The line goes through a road made up of PMOS and N-MOS A cell circuit made up of tubes . This structure makes GPIO Possess “ Push pull output ” and “ Open drain output ” Two modes .

So-called Push pull output mode , It's based on these two MOS Named after the way the tube works .** In this structure, high level is input , After the reverse , At the top of the P-MOS Conduction , At the bottom of the N-MOS close , External output high level ; In this structure, low level is input , After the reverse ,N-MOS The pipe is open ,P-MOS close , External output low level .***** When the pin level switches between high and low , Two pipes turn on ,P The tube is responsible for filling the current ,N The tube is responsible for pulling the current , The load capacity and switching speed are greatly improved compared with ordinary methods .* The low level of push-pull output is 0 v , The high level is 3.3 v , Refer to figure push-pull equivalent circuit for details , It is the equivalent circuit of push-pull output mode .

And in the Open drain output mode when , At the top of the P-MOS It doesn't work at all . If we control the output to be 0, Low level , be PMOS The tube is closed ,N-MOS The pipe is open , Ground the output , If the control output is 1( It can't directly output high level ) when , be P-MOS Tube and N-MOS All the tubes are closed , So the pin neither outputs high level , It doesn't output low level either , It's a high resistance state .

Open drain output is generally used in I2C、SMBUS Communication, etc “ Line and ” Function of the bus circuit

typedef enum
 {
    
     GPIO_Mode_AIN = 0x0, //  Analog input 
     GPIO_Mode_IN_FLOATING = 0x04, //  Floating input 
     GPIO_Mode_IPD = 0x28, //  Drop down input 
     GPIO_Mode_IPU = 0x48, //  Pull up input 
     
     GPIO_Mode_Out_OD = 0x14, //  Open drain output 
     GPIO_Mode_Out_PP = 0x10, //  Push pull output 
     GPIO_Mode_AF_OD = 0x1C, //  Reuse open drain output 
     GPIO_Mode_AF_PP = 0x18 //  Multiplexing push pull output 
 } GPIOMode_TypeDef;

1.3 Lighten up LED The lamp

technological process

1) Can make IO Port clock , By calling :

​ RCC_ABP2PeriphColckCmd( )

  1. initialization IO Mouth mode , call :

​ GPIO_Init( )

  1. operation IO mouth , Output high and low level

​ GPIO_SetBits( )

​ GPIO_ResetBits( )

# In order to increase the portability of the code , Define hardware related macros in  bsp_LED.h  in 
#ifndef _BSP_LED_H
#define _BSP_LED_H

#define LED1_GPIO_CLK RCC_APB2Periph_GPIOA
#define LED1_GPIO_PORT GPIOA
#define LED1_GPIO_PIN GPIO_Pin_8

#define LED2_GPIO_CLK RCC_APB2Periph_GPIOD
#define LED2_GPIO_PORT GPIOD
#define LED2_GPIO_PIN GPIO_Pin_2

#include "stm32f10x.h"
void LED_GPIO_Config(void);
#endif/*_BSP_LED_H*/

LED Initialize configuration

#include "./LED/bsp_led.h"
void LED_GPIO_Config(void)
{
    
	GPIO_InitTypeDef GPIO_InitType_struct_A;
	GPIO_InitTypeDef GPIO_InitType_struct_D;
	
	/* Step 1 turn on the peripheral clock */
	RCC_APB2PeriphClockCmd(LED1_GPIO_CLK,ENABLE);
	RCC_APB2PeriphClockCmd(LED2_GPIO_CLK,ENABLE);
	/* The second step is to configure the peripheral initialization structure */
	GPIO_InitType_struct_A.GPIO_Pin = LED1_GPIO_PIN; 
	GPIO_InitType_struct_A.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitType_struct_A.GPIO_Speed = GPIO_Speed_10MHz;
	
	GPIO_InitType_struct_D.GPIO_Pin = LED2_GPIO_PIN;
	GPIO_InitType_struct_D.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitType_struct_D.GPIO_Speed = GPIO_Speed_10MHz;
	
	/* Step 3 call the initialization function , Write the configured structure members to the register */
	GPIO_Init(LED1_GPIO_PORT,&GPIO_InitType_struct_A);
	GPIO_Init(LED2_GPIO_PORT,&GPIO_InitType_struct_D);

The main program

#include "stm32f10x.h"
#include "./LED/bsp_led.h"

void delay(uint32_t count)
{
    
	for(;count!=0;count--);
}

int main(void)
{
    
	LED_GPIO_Config( );
	
	while(1)
	{
    
		GPIO_SetBits(LED2_GPIO_PORT,LED2_GPIO_PIN);
		GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
		delay(0xfffff);
		GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);
		GPIO_ResetBits(GPIOD,LED2_GPIO_PIN);
		delay(0xfffff);
		
	}
}

原网站

版权声明
本文为[Xiao Hou_ two thousand and twenty-two]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061632516566.html