新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > stm32學(xué)習(xí)之五

stm32學(xué)習(xí)之五

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

目標(biāo):通過按鍵的輸入,實現(xiàn)LED燈的反轉(zhuǎn)等操作。

要實現(xiàn)按鍵的輸入的讀取,必須要實現(xiàn)一個key.c和key.h的文件的編寫,這里,利用庫函數(shù)的編寫原則(仿照
庫函數(shù)的編寫方法),獲取按鍵的動作。
首先,編寫key.h函數(shù):
#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://butianyuan.cn/article/201612/325175.htm

#endif

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

然后,編寫:
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--);
}

這里,初始化了按鍵的操作,以及延時,防抖動的操作。
同時:
uint8_t key_Scan( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin )
是仿照庫函數(shù)的寫法:
uint8_t GPIO_ReadInputDataBit ( GPIO_TypeDef * GPIOx, uint16_t GPIO_Pin )
然后,就是led.c和led.h的編寫了
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
其實,是復(fù)制原來的代碼而已,也可以省略很多的東西的。
接著就是主函數(shù)的編寫了:
/******************** (C) COPYRIGHT 2013 **************************
* 文件名 :main.c
* 描述 :用3.5.0版本建的工程模板。
* 實驗平臺:野火STM32開發(fā)板
* 庫版本 :ST3.5.0
*
* 作者 :wit_yuan
* 版本 : v1.0
* 時間 : 2013年4月27日
**********************************************************************************/
#include "stm32f10x.h"
#include "led.h"
#include "SysTick.h"
#include "key.h"
/*
* 函數(shù)名:main
* 描述 : 主函數(shù)
* 輸入 :無
* 輸出 : 無
*/
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)));
}
}

}

由此,基本上完畢程序的編寫。



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

評論


技術(shù)專區(qū)

關(guān)閉