中级会员
 
- 积分
- 366
- 金钱
- 366
- 注册时间
- 2015-6-23
- 在线时间
- 111 小时
|

楼主 |
发表于 2016-2-16 21:35:26
|
显示全部楼层
//按键初始化函数
void KEY_Init()
{
GPIO_InitTypeDef GPIO_InitStrue; //定义端口设置结构体
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE, ENABLE); //使能PA、PE端口时钟
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_0; //选择端口0
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_IPD; //设置下拉输入
GPIO_Init(GPIOA, &GPIO_InitStrue); //使能端口
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_3 | GPIO_Pin_2; //选择端口2、3、4
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_IPU; //设置上拉输入
GPIO_Init(GPIOE, &GPIO_InitStrue); //使能端口
}
这是原教程的按键端口设置函数,虽然设置了上、下拉输入,这是高阻态,示波器测得的是干扰信号,我在相应的端口给了相应的电平,效果好很多了,但是PA0口虽然设置了低电平,而没有任何改善。
下面是我改进的函数:
void KEY_Init(void) //IO初始化
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE, ENABLE);//使能PORTA,PORTE时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4; //KEY0-KEY2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //设置成上拉输入
GPIO_Init(GPIOE, &GPIO_InitStructure); //初始化GPIOE2,3,4
GPIO_SetBits(GPIOE, GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4); //GPIOE2,3,4输出高电平
//初始化 WK_UP-->GPIOA.0 下拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0设置成输入,默认下拉
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOA.0
GPIO_ResetBits(GPIOA, GPIO_Pin_0); //GPIOA.0输出低电平
} |
|