STM32_GPIO

来源:互联网 发布:漫画打印排版软件 编辑:程序博客网 时间:2024/05/20 05:29

一般使用配置

打开Portx时钟(RCC->APB2ENR)

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx | RCC_APB2Periph_GPIOx, ENABLE); 
  • 下面是用到的宏定义。
#define RCC_APB2Periph_GPIOA             ((uint32_t)0x00000004)#define RCC_APB2Periph_GPIOB             ((uint32_t)0x00000008)#define RCC_APB2Periph_GPIOC             ((uint32_t)0x00000010)#define RCC_APB2Periph_GPIOD             ((uint32_t)0x00000020)#define RCC_APB2Periph_GPIOE             ((uint32_t)0x00000040)#define RCC_APB2Periph_GPIOF             ((uint32_t)0x00000080)#define RCC_APB2Periph_GPIOG             ((uint32_t)0x00000100)
typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;

配置使用的引脚、模式和速率

GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_x;   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_x_x;  //如果引脚设置为输入模式则不需要配置速率GPIO_InitStructure.GPIO_Speed = GPIO_Speed_xMHz; 
  • 以下是用到的结构体、枚举变量和宏定义。
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;
#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 */
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;
typedef enum{   GPIO_Speed_10MHz = 1,  GPIO_Speed_2MHz,   GPIO_Speed_50MHz}GPIOSpeed_TypeDef;

初始化GPIOx(GPIOx->CRH、GPIOx->CRL)

GPIO_Init(GPIOx, &GPIO_InitStructure);  
  • 以下是用到的宏定义。
#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE)#define GPIOB               ((GPIO_TypeDef *) GPIOB_BASE)#define GPIOC               ((GPIO_TypeDef *) GPIOC_BASE)#define GPIOD               ((GPIO_TypeDef *) GPIOD_BASE)#define GPIOE               ((GPIO_TypeDef *) GPIOE_BASE)#define GPIOF               ((GPIO_TypeDef *) GPIOF_BASE)#define GPIOG               ((GPIO_TypeDef *) GPIOG_BASE)

置位与清零

置位(GPIOx->BSRR)

GPIO_SetBits(GPIOx, GPIO_Pin_x | GPIO_Pin_x);

清零(GPIOx->BRR)

GPIO_ResetBits(GPIOx, GPIO_Pin_x | GPIO_Pin_x);

获取单个引脚数据(GPIOx->IDR)

GPIO_ReadInputDataBit(GPIOx, GPIO_Pin_x);

关于volatile类型

core_cm3.h

#ifdef __cplusplus  #define     __I     volatile                /*!< defines 'read only' permissions      */#else  #define     __I     volatile const          /*!< defines 'read only' permissions      */#endif#define     __O     volatile                  /*!< defines 'write only' permissions     */#define     __IO    volatile                  /*!< defines 'read / write' permissions   */

__I是只读,作为输入口,程序不应该试图去修改它。如只读的状态寄存器。
__O是只写,作为输出口。
__IO是读写,作为输入输出口。

  • 实现一个volatile型参数的平方。如下是错误的。
long square(volatile int *ptr){       return *ptr * *ptr;  }  
  • 由于*ptr的值可能被意想不到地改变,所以两个*ptr可能不同。以下是正确代码。
long square(volatile int *ptr){       int a;       a = *ptr;      return a * a;   } 
0 0
原创粉丝点击