首页 > 学院 > 开发设计 > 正文

STM32F103程序设计-6-引脚输入功能-按键(查询)

2019-11-06 09:28:24
字体:
来源:转载
供稿:网友

检测单片机引脚上的电平,即使用单片机的输入功能。把上次的例程中初始化控制LED的GPIO口的部分拿出来放到一个函数LED_Iint( )中。注意,初始化时先在LED_Iint( )的最后点亮LED,目的是为了验证初始化部分是成功的,之后再改写程序为熄灭LED。用途:分隔故障。

KEY_Init( )初始化函数中注意如何把GPIO口配置为输入口,而且本例程中由于外部没有外接上拉电阻,所以还要配置为带内部上拉的输入口。

void KEY_Init()

{

/* GPIO Periph clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

/* Configure PC13 and PD3 in output pushpull mode */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOD, &GPIO_InitStructure);

}

Stm32f10x_gpio.c文件中有许多API,其中:

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);用于读取某个引脚电平的函数。

By:霜月孤鸟

2017.2.26

CSDN博客地址:http://blog.csdn.net/ourrtems

欢迎访问、关注单片机百宝箱!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表