1、定义一个PPP_InitTypeDef类型的
结构体变量
(PPP泛指某个外设,如ADC、GPIO)
-
ex:GPIO_InitTypeDef GPIO_InitStructure
在stm32f10x_gpio.h文件中可以找到对GPIO_InitTypeDef的结构体定义:
typedef struct{uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.This parameter can be any value of @ref GPIO_pins_define */GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.This parameter can be a value of @ref GPIOSpeed_TypeDef */GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.This parameter can be a value of @ref GPIOMode_TypeDef */}GPIO_InitTypeDef;
-
对GPIO_pins_define进行词汇搜索
#define GPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */#define GPIO_Pin_1 ((uint16_t)0x0002) /*!< Pin 1 selected */#define GPIO_Pin_2 ((uint16_t)0x0004) /*!< Pin 2 selected */#define GPIO_Pin_3 ((uint16_t)0x0008) /*!< Pin 3 selected */#define GPIO_Pin_4 ((uint16_t)0x0010) /*!< Pin 4 selected */#define GPIO_Pin_5 ((uint16_t)0x0020) /*!< Pin 5 selected */#define GPIO_Pin_6 ((uint16_t)0x0040) /*!< Pin 6 selected */#define GPIO_Pin_7 ((uint16_t)0x0080) /*!< Pin 7 selected */#define GPIO_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */#define GPIO_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */#define GPIO_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */#define GPIO_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */#define GPIO_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */#define GPIO_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */#define GPIO_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */#define GPIO_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */#define GPIO_Pin_All ((uint16_t)0xFFFF) /*!< All pins selected */
-
对GPIOMode_TypeDef进行词汇搜索
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;
-
对GPIOSpeed_TypeDef进行词汇搜索
typedef enum{GPIO_Speed_10MHz = 1,GPIO_Speed_2MHz,GPIO_Speed_50MHz}GPIOSpeed_TypeDef;
2、在配置一个外设之前必须用以下一种函数打开其时钟
-
RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState));
-
RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState));
-
RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState));
参数1:RCC_APB1/APB2/AHBPeriph_PPP
参数2:ENABLE(使能)、DISABLE(失能)
3、对结构体变量的成员进行赋值配置(数值参考步骤1)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;(输出模式)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_X;(输出引脚)
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;(输出速率)
4、调用PPP_Init()的函数用于初始化外设
/*** @brief Initializes the GPIOx peripheral according to the specified* parameters in the GPIO_InitStruct.* @param GPIOx: where x can be (A..G) to select the GPIO peripheral.* @param GPIO_InitStruct: pointer to a GPIO_InitTypeDef structure that* contains the configuration information for the specified GPIO peripheral.* @retval None*/
ex:GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
参数1:选择GPIO输出端口GPIOx
参数2:
取步骤1结构体变量的地址
——ex:&GPIO_InitStructure
5、调用PPP_Cmd()函数打开外设
ex:GPIO_Cmd();