GPIO pins of the STM32 notes

EDITORIAL:
This article aims to summarize the backup, to facilitate future inquiries, as is the personal summary, if wrong, please correct me; in addition, most of the content from the Internet, books, and various manuals, should please inform the infringement, immediately delete posts to apologize.

 

table of Contents

A, GPIO mode

Second, the peripheral I / O configuration mode selection

Three, GPIO configuration code to achieve

Fourth, the total project realization


 

A, GPIO mode

The STM32 GPIO mode are the following:

  •   GPIO_Mode_AIN - Analog Input
  •   GPIO_Mode_IN_FLOATING - floating input
  •   GPIO_Mode_IPD - Input drop-down
  •   GPIO_Mode_IPU - pull input
  •   GPIO_Mode_Out_OD - open drain output
  •   GPIO_Mode_Out_PP - pull output
  •   GPIO_Mode_AF_OD - open drain multiplexing function
  •   GPIO_Mode_AF_PP - push-pull multiplexing function
typedef enum
{ 
  GPIO_Mode_AIN = 0x0,
  GPIO_Mode_IN_FLOATING = 0x04,
  GPIO_Mode_IPD = 0x28,
  GPIO_Mode_IPU = 0x48,
  GPIO_Mode_Out_OD = 0x14,
  GPIO_Mode_Out_PP = 0x10,
  GPIO_Mode_AF_OD = 0x1C,
  GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;

For later reference code, in the above way GPIO paste pattern structure, you can also view the files stm32f10x_gpio.h

According to the above several models can be roughly divided into four modes: input, output, multiplexing, analog

Let's look at I / O port overall functional structure, and then subdivided

1, input

2, the output

3, reuse

4, simulation

 

Second, the peripheral I / O configuration mode selection

 

Three, GPIO configuration code to achieve

This example demonstrates only the input and output; further, later also eleven applications

1, the output of the push-pull output

/************************************************
函数名称 : LED_Config
功    能 : LED配置
参    数 : 无
返 回 值 : 无
*************************************************/
void LED_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	/* Configure PB.14 pin as output push pull */
	GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
}

2, an input on the input pull-

/************************************************
函数名称 : Key_Config
功    能 : 按键配置
参    数 : 无
返 回 值 : 无
*************************************************/
void Key_Config(void)
{
	EXTI_InitTypeDef EXTI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	/* Enable GPIOB clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	/* Enable AFIO clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	/* Configure PB.01 pin as input pull up */
	GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
	
	/* Connect EXTI1 Line to PB.01 pin */
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);

	/* Configure EXTI1 line */
	EXTI_InitStructure.EXTI_Line = EXTI_Line1;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	/* Enable the EXTI1 Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* Generate software interrupt: simulate a falling edge applied on EXTI1 line */
	EXTI_GenerateSWInterrupt(EXTI_Line1);
}

The above is some common I / O configuration; in the input, use the EXIT and NVIC two strange configuration, now you do not need to understand them in more detail later say

 

Fourth, the total project realization

bsp_gpio.c source file

#include "bsp_gpio.h"


/************************************************
函数名称 : LED_Config
功    能 : LED配置
参    数 : 无
返 回 值 : 无
*************************************************/
void LED_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	/* Configure PB.14 pin as output push pull */
	GPIO_InitStructure.GPIO_Pin = LED_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(LED_GPIO_PORT, &GPIO_InitStructure);
	
	LED_OFF;	
}

/************************************************
函数名称 : Key_Config
功    能 : 按键配置
参    数 : 无
返 回 值 : 无
*************************************************/
void Key_Config(void)
{
	EXTI_InitTypeDef EXTI_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	/* Enable GPIOB clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	
	/* Enable AFIO clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	
	/* Configure PB.01 pin as input pull up */
	GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
	
	/* Connect EXTI1 Line to PB.01 pin */
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);

	/* Configure EXTI1 line */
	EXTI_InitStructure.EXTI_Line = EXTI_Line1;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	/* Enable the EXTI1 Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	/* Generate software interrupt: simulate a falling edge applied on EXTI1 line */
	EXTI_GenerateSWInterrupt(EXTI_Line1);
}
	

/*---------------------------- END OF FILE ----------------------------*/


 

bsp_gpio.h header file

#ifndef __BSP_GPIO_H
#define __BSP_GPIO_H


#include "stm32f10x.h"

#define LED_GPIO_PORT			GPIOB
#define LED_GPIO_PIN			GPIO_Pin_14

#define LED_ON				GPIO_ResetBits(GPIOB, GPIO_Pin_14)
#define LED_OFF				GPIO_SetBits(GPIOB, GPIO_Pin_14)

#define KEY_GPIO_PORT			GPIOB
#define KEY_GPIO_PIN			GPIO_Pin_1

void LED_Config(void);
void Key_Config(void);


#endif	/* __BSP_GPIO_H */


/*---------------------------- END OF FILE ----------------------------*/


 

stm32f10x_it.c source file

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_it.h"
#include "bsp_gpio.h"


/******************************************************************************/
/*            STM32F10x Peripherals Interrupt Handlers                        */
/******************************************************************************/

/**
  * @brief  This function handles External line 1 interrupt request.
  * @param  None
  * @retval None
  */
void EXTI1_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line1) != RESET)
	{
		if(GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN) == RESET)
		{
			LED_OFF;
		}

		/* Clear the  EXTI line 1 pending bit */
		EXTI_ClearITPendingBit(EXTI_Line1);
	}
}

 

main.c main function

/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include <stdio.h>


/************************************************
函数名称 : main
功    能 : 主函数入口
参    数 : 无
返 回 值 : 无
*************************************************/
int main(void)
{	
	/* Initial Configuration */
	SystemInit();
	
	/* -------- End -------- */
	
	LED_ON;
	
	/* Infinite loop */
	while (1)
	{

	}
}

 

发布了40 篇原创文章 · 获赞 14 · 访问量 1万+

Guess you like

Origin blog.csdn.net/qq_42992084/article/details/104089363