84 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			84 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
 | 
						||
#include "board.h"
 | 
						||
#include "stm32f10x.h"
 | 
						||
#include "if_key.h"
 | 
						||
#include "core_delay.h"
 | 
						||
 | 
						||
 | 
						||
#define GPIO_Initer() {.GPIO_Speed=GPIO_Speed_50MHz,\
 | 
						||
  .GPIO_Mode=GPIO_Mode_IPU,\
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
typedef struct{
 | 
						||
  int key_old;
 | 
						||
  
 | 
						||
}self_def;
 | 
						||
 | 
						||
static self_def g_self;
 | 
						||
 | 
						||
static int init(void)
 | 
						||
{
 | 
						||
  GPIO_InitTypeDef init2=GPIO_Initer();
 | 
						||
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
 | 
						||
 | 
						||
  init2.GPIO_Pin=(1<<0);
 | 
						||
  GPIO_Init(GPIOB,&init2);
 | 
						||
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
static int scan(void)
 | 
						||
{
 | 
						||
  self_def *s=&g_self;
 | 
						||
  int key=0,key2=0;
 | 
						||
  key=PININ(B,0);
 | 
						||
  if(key!=s->key_old)
 | 
						||
  {
 | 
						||
    //连续采集5次,都为相反电平时才输出此电平
 | 
						||
    for(int i=0;i<2;i++)
 | 
						||
    {
 | 
						||
      delay_ms(20);
 | 
						||
      key2=PININ(B,0);
 | 
						||
      if(key!=key2) {
 | 
						||
        return key2;
 | 
						||
      }
 | 
						||
    }
 | 
						||
    return key;
 | 
						||
  }
 | 
						||
  return s->key_old;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
// 读取按键,1按下,0未按下
 | 
						||
static int read(void)
 | 
						||
{
 | 
						||
  self_def *s=&g_self;
 | 
						||
  int key=scan();
 | 
						||
  int key_ret=0;
 | 
						||
  if(key!=s->key_old)
 | 
						||
  {
 | 
						||
    if(key==0){
 | 
						||
      key_ret=1;
 | 
						||
    }else{
 | 
						||
      key_ret=0;
 | 
						||
    }
 | 
						||
    s->key_old=key;
 | 
						||
  }
 | 
						||
  return key_ret;
 | 
						||
}
 | 
						||
 | 
						||
 | 
						||
static key_def g_key={
 | 
						||
  .init=init,
 | 
						||
  .read=read,
 | 
						||
};
 | 
						||
 | 
						||
 | 
						||
key_def *key(void)
 | 
						||
{
 | 
						||
  return &g_key;
 | 
						||
}
 | 
						||
 |