1. 程式人生 > >stm32-獨立按鍵

stm32-獨立按鍵

rcc its 其他 是把 clu bit pin 下拉 data

時間有點倉促,寫的比較粗糙 先寫點上去吧

前面講過了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;
}

stm32-獨立按鍵