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

按键

2019-11-11 01:18:40
字体:
来源:转载
供稿:网友
                            按键

一、用到的知识

1.    位操作:直接访问一个位的地址。(可以在“sys.h”头文件中找到,并且使用之前要包含“sys.h”头文件)

2.    读取IO口电平函数(既可以使用位操作,又可以使用库函数操作)

二、操作流程

1.    使能相对应IO口的时钟(调用RCC_APB2PeriphClockCmd()函数)

2.    初始化IO口模式为上/下拉输入。(调用GPIO_Init()函数)

3.    扫描IO口的电平(调用GPIO_ReadInputDataBit()函数)

#include "key.h"#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)#define WAKE GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)void Key_Init(void){	GPIO_InitTypeDef GPIO_InitStructure;  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//PE3,PE4	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//PA0		GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;	GPIO_Init(GPIOE,&GPIO_InitStructure);//PE3ÉÏÀ­ÊäÈë	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;	GPIO_Init(GPIOE,&GPIO_InitStructure);//PE4ÉÏÀ­ÊäÈë		GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;	GPIO_Init(GPIOA,&GPIO_InitStructure);//PE3ÉÏÀ­ÊäÈë}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表