新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2020-7-6
- 在线时间
- 6 小时
|
1金钱
下面是key.c
#include"key.h"
#include"delay.h"
void KEY_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct ;
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOE,ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN ;
// GPIO_InitStruct.GPIO_OType=GPIO_OType_PP ;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP ;
// GPIO_InitStruct.GPIO_Speed=GPIO_Fast_Speed ;
GPIO_Init (GPIOE,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN ;
// GPIO_InitStruct.GPIO_OType=GPIO_OType_PP ;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_DOWN ;
// GPIO_InitStruct.GPIO_Speed=GPIO_Fast_Speed ;
GPIO_Init (GPIOA,&GPIO_InitStruct);
}
u8 KEY_Scan(void)
{
static u8 key_up=1;
if (key_up&&(KEY0==0||KEY1==0||KEY2==0||WK_UP==1))
{
delay_ms(10);
key_up=0;
if(KEY0==0)
{
return 1;
}
else if(KEY1==0)
{
return 2;
}
else if(KEY2==0)
{
return 3;
}
else if(WK_UP==1)
{
return 4;
}
}
else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)
{
key_up=1;
return 0;
}
// else
//
// return 0;
}
下面是key.h
#ifndef __KEY_H
#define __KEY_H
#include"stm32f4xx.h"
#define KEY0 GPIO_ReadInputDataBit (GPIOE,GPIO_Pin_4)
#define KEY1 GPIO_ReadInputDataBit (GPIOE,GPIO_Pin_3)
#define KEY2 GPIO_ReadInputDataBit (GPIOE,GPIO_Pin_2)
#define WK_UP GPIO_ReadInputDataBit (GPIOA,GPIO_Pin_0)
#define KEY0_PRES 1
#define KEY1_PRES 2
#define KEY2_PRES 3
#define WK_UP_PRES 4
void KEY_Init(void);
u8 KEY_Scan(void);
#endif
下面是main.c
#include"stm32f4xx.h"
#include"beep.h"
#include"delay.h"
#include"usart.h"
#include"led.h"
#include"key.h"
int main(void)
{
u8 key;
delay_init(168);
LED_Init();
BEEP_Init();
KEY_Init();
// u8 KEY_Scan();
// GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
while (1)
{
key=KEY_Scan();
if(key)
{
switch(key)
{
case KEY0_PRES:
// GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
GPIO_ResetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
break;
case KEY1_PRES:
GPIO_ResetBits(GPIOF,GPIO_Pin_8);
break;
case KEY2_PRES:
GPIO_SetBits(GPIOF,GPIO_Pin_8);
break;
case WK_UP_PRES:
// GPIO_ResetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
// delay_ms(500);
break;
}
}
else delay_ms(10);
}
}
|
|