<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>

新聞中心

EEPW首頁(yè) > 嵌入式系統 > 設計應用 > stm32學(xué)習之五

stm32學(xué)習之五

作者: 時(shí)間:2016-12-03 來(lái)源:網(wǎng)絡(luò ) 收藏
按鍵輸入的學(xué)習(key_polling):

目標:通過(guò)按鍵的輸入,實(shí)現LED燈的反轉等操作。

要實(shí)現按鍵的輸入的讀取,必須要實(shí)現一個(gè)key.c和key.h的文件的編寫(xiě),這里,利用庫函數的編寫(xiě)原則(仿照
庫函數的編寫(xiě)方法),獲取按鍵的動(dòng)作。
首先,編寫(xiě)key.h函數:
#ifndef _KEY_H
#define _KEY_H
#include "stm32f10x.h"
#define KEY_ON 0
#define KEY_OFF 1
void key_Init(void);
uint8_t key_Scan( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin );
void delay(uint32_t count);

本文引用地址:http://dyxdggzs.com/article/201612/325175.htm

#endif

這里:
定義了KEY_ON和KEY_OFF的宏定義,主要是由于按鍵按下的時(shí)候,會(huì )導致讀取的數據為0,因此,就按上面的
定義規范了。

然后,編寫(xiě):
key.c文件:
#include "stm32f10x.h"
#include "key.h"
void key_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOE,&GPIO_InitStructure);

GPIO_SetBits(GPIOC,GPIO_Pin_5);
}

uint8_t key_Scan( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin )
{
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin_5)==KEY_ON)
{
delay(5000);
if(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin_5)==KEY_ON)
{
while(GPIO_ReadInputDataBit(GPIOx,GPIO_Pin_5)==KEY_ON);
return KEY_ON;
}
else
{
return KEY_OFF;
}
}
else
{
return KEY_OFF;
}

}

void delay(uint32_t count)
{
for(;count>0;count--);
}

這里,初始化了按鍵的操作,以及延時(shí),防抖動(dòng)的操作。
同時(shí):
uint8_t key_Scan( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin )
是仿照庫函數的寫(xiě)法:
uint8_t GPIO_ReadInputDataBit ( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin )
然后,就是led.c和led.h的編寫(xiě)了
led.c代碼如下:
#include "led.h"
void LED_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOC,&GPIO_InitStructure);

GPIO_SetBits(GPIOC,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
}
led.h代碼如下:
#ifndef _LED_H
#define _LED_H
#include "stm32f10x.h"

#define ON 1
#define OFF 0

#define LED1(a) if (a)
GPIO_ResetBits(GPIOC,GPIO_Pin_3);
else
GPIO_SetBits(GPIOC,GPIO_Pin_3)
#define LED2(a) if (a)
GPIO_ResetBits(GPIOC,GPIO_Pin_4);
else
GPIO_SetBits(GPIOC,GPIO_Pin_4)
#define LED3(a) if (a)
GPIO_ResetBits(GPIOC,GPIO_Pin_5);
else
GPIO_SetBits(GPIOC,GPIO_Pin_5)
void LED_GPIO_Config(void);


#endif
其實(shí),是復制原來(lái)的代碼而已,也可以省略很多的東西的。
接著(zhù)就是主函數的編寫(xiě)了:
/******************** (C) COPYRIGHT 2013 **************************
* 文件名 :main.c
* 描述 :用3.5.0版本建的工程模板。
* 實(shí)驗平臺:野火STM32開(kāi)發(fā)板
* 庫版本 :ST3.5.0
*
* 作者 :wit_yuan
* 版本 : v1.0
* 時(shí)間 : 2013年4月27日
**********************************************************************************/
#include "stm32f10x.h"
#include "led.h"
#include "SysTick.h"
#include "key.h"
/*
* 函數名:main
* 描述 : 主函數
* 輸入 :無(wú)
* 輸出 : 無(wú)
*/
int main(void)
{
LED_GPIO_Config();
//SysTick_Init();
key_Init();

while(1)
{
if(key_Scan( GPIOE, GPIO_Pin_5 )==KEY_ON)
{
GPIO_WriteBit(GPIOC,GPIO_Pin_3,(BitAction)
(1-GPIO_ReadOutputDataBit (GPIOC,GPIO_Pin_3)));
}
}

}

由此,基本上完畢程序的編寫(xiě)。



關(guān)鍵詞: stm32按鍵輸

評論


技術(shù)專(zhuān)區

關(guān)閉
国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放
<dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><small id="yhprb"></small><dfn id="yhprb"></dfn><small id="yhprb"><delect id="yhprb"></delect></small><small id="yhprb"></small><small id="yhprb"></small> <delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"></dfn><dfn id="yhprb"></dfn><s id="yhprb"><noframes id="yhprb"><small id="yhprb"><dfn id="yhprb"></dfn></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><small id="yhprb"></small><dfn id="yhprb"><delect id="yhprb"></delect></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn> <small id="yhprb"></small><delect id="yhprb"><strike id="yhprb"></strike></delect><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn><dfn id="yhprb"><s id="yhprb"><strike id="yhprb"></strike></s></dfn><dfn id="yhprb"><s id="yhprb"></s></dfn>