stm32GPIO学习

来源:互联网 发布:数据库视频 编辑:程序博客网 时间:2024/05/22 21:47
void LED_GPIO_Config(void)
24. {
25. /*定义一个GPIO_InitTypeDef类型的结构体*/
26. GPIO_InitTypeDef GPIO_InitStructure;
27.
28.
/*开启GPIOC的外设时钟*/
29. RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE);
30.
31.
/*选择要控制的GPIOC引脚
*/
32. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5
;
33.
34.
/*设置引脚模式为通用推挽输出*/
35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
36.
37.
/*设置引脚速率为50MHz */
38. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
39.
40.
/*调用库函数,初始化GPIOC*/

41. GPIO_Init(GPIOC, &GPIO_InitStructure);

42.}

25行定义了一个名为GPIO_InitStructure的GPIO_InitTypeDef 类型的结构体,结构体原型定义在stm32f10x_gpio.h,

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_SetBits()和GPIO_ResetBits()函数原型定义在stm32f10x_gpio.h,分别对应了一个GPIO寄存器,BSRR写1的位置1,BRR写1的位置0;这两个命令是不同的,一个用于清零,一个用于置1,源代码如下:


void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  
  GPIOx->BSRR = GPIO_Pin;
}

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
  /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  
  GPIOx->BRR = GPIO_Pin;
}



原创粉丝点击