stm32-独立按键

来源:互联网 发布:钱夫人淘宝店天涯 编辑:程序博客网 时间:2024/05/21 10:40

时间有点仓促,写的比较粗糙   先写点上去吧

   前面讲过了io口的设置,按键不过是把io口设置成其他的模式,如果按键接的vcc就将相应io口设置成下拉输入模式,接地就上拉输入模式(没按下的时候就要默认高电平)

然后就和51的时候一样处理抖动,读取状态就行了,我也改成没使用商家给的函数,只使用库函数了

  以下为key驱动

 #include "key.h"
void delay_ms(u16 time)
{    
   u16 i=0;  
   while(time--)
   {
      i=8000; //////////....????
      while(i--) ;    
   }
}


void KEY_Init()
{
    GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//ÉÏÀ­ÊäÈë
GPIO_Init(GPIOA,&GPIO_InitStructure);

// GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;//ÉÏÀ­ÊäÈë
// GPIO_Init(GPIOC,&GPIO_InitStructure);
//
// GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
// GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;//ÏÂÀ­ÊäÈë
// GPIO_Init(GPIOA,&GPIO_InitStructure);

}


u8 KEY_Scan(u8 mode)
{
u8 temp;
    temp=GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15);
if(temp==0)
{
delay_ms(10);
temp=GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15);
if(temp==0)
{
   return 0;
}
}
return 1;
}


以下为main,led和上次一样

#include "led.h"
#include "key.h"
void delayms(u16 time)
{    
   u16 i=0;  
   while(time--)
   {
      i=8000; 
      while(i--) ;    
   }
}
int main()
{
    KEY_Init();
LED_Init();
while(1)
{
if(KEY_Scan(0))
{
   GPIO_SetBits(GPIOA,GPIO_Pin_8);
delayms(500);
}
   else 
{
   GPIO_ResetBits(GPIOA,GPIO_Pin_8);
delayms(500);
}
}
//return 0;
}

0 0
原创粉丝点击