key.h
#ifndef _key_H
#define _key_H
#include "system.h"
#define KEY0_PIN GPIO_Pin_4
#define KEY1_PIN GPIO_Pin_3
#define KEY2_PIN GPIO_Pin_2
#define KEY_UP_PIN GPIO_Pin_0
#define KEY012_PORT GPIOE
#define KEY_UP_PORT GPIOA
#define KEY_UP PAin(0) //位帶引腳
#define KEY0 PEin(4)
#define KEY1 PEin(3)
#define KEY2 PEin(2)
#define KEY012_PORT_RCC RCC_APB2Periph_GPIOE //時(shí)鐘
#define KEY_UP_PORT_RCC RCC_APB2Periph_GPIOA //時(shí)鐘
#define KEY_UP_PRESS 1
#define KEY0_PRESS 2
#define KEY1_PRESS 3
#define KEY2_PRESS 4
void KEY_Init();
u8 KEY_Scan(u8 mode);
#endif
key.c
#include "key.h"
#include "SysTick.h"
void KEY_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(KEY012_PORT_RCC | KEY_UP_PORT_RCC, ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin=KEY_UP_PIN;
GPIO_Init(KEY_UP_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=KEY0_PIN | KEY1_PIN | KEY2_PIN;
GPIO_Init(KEY012_PORT,&GPIO_InitStructure);
}
u8 KEY_Scan(u8 mode)//mode=0,單次檢測(cè)按鍵 //mode=1, 連續(xù)檢測(cè)按鍵
{
static u8 key=1;
if(mode ==1 )key=1;
//剛開始key=1,按鍵啟用,key又為0,按鍵沒(méi)有啟用,key又變?yōu)?.
//若為連續(xù)按鍵,則key經(jīng)過(guò)第二個(gè)大if又返回后經(jīng)過(guò)第一個(gè)大if變?yōu)?,則變?yōu)檫B續(xù)按鍵
if(key==1&&(KEY_UP==1 || KEY0==0 || KEY1==0 || KEY2==0))
{
delay_ms(10);//消抖處理
key=0;
if(KEY_UP==1)
{
return KEY_UP_PRESS;
}
else if(KEY0==0)
{
return KEY0_PRESS;
}
else if(KEY1==0)
{
return KEY1_PRESS;
}
else if(KEY2==0)
{
return KEY2_PRESS;
}
}
else if(key==0&&(KEY_UP==0 && KEY0==1 && KEY1==1 && KEY2==1))
{
key=1;
}
return 0;
}
main.c
#include "stm32f10x.h"
#include "led.h"
#include "system.h"
#include "SysTick.h"
#include "beep.h"
#include "key.h"
int main()
{
u8 key=0;
u8 i=0;
SysTick_Init(72);
BEEP_Init();
LED_Init();
KEY_Init();
while(1)
{
key=KEY_Scan(0);//單次按鍵掃描
switch(key)
{
case KEY_UP_PRESS:LED2=0;break;
case KEY0_PRESS:BEEP=0;break;
case KEY1_PRESS:LED2=1;break;
case KEY2_PRESS:BEEP=1;break;
}
i++;
if(i%20==0)
LED1=!LED1;//指示燈亮提示機(jī)器正在運(yùn)行
delay_ms(10);
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧