专注电子技术学习与研究
当前位置:单片机教程网 >> STM32 >> 浏览文章

STM32 按键检测程序

作者:huqin   来源:本站原创   点击数:  更新时间:2014年04月09日   【字体:

/*  ------------------------------------------------------- ————————————
注意事项: PA13 PA15 是JTAG的引脚。 所以JTAG 插上 模拟时候,不准去的。
只有调到SWD 模式 PA15 才能用。 PA13是SWDIO  PA14 SWCLK  复用时候一定要注意
 
实验结果: DS0 交替闪烁   当按下KEY1 时候 DS1亮。 松手灭。
 
问题: 按键如何去抖。。这样延时等待 占用系统资源。 请教高手按键消抖问题,有什么更好的方法解决。
---------------------------------------————————————————————---------------*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;  //声明结构体
 
/**********************************************************************
声明一个结构体,名字是GPIO_InitStructure,结构体原型由GPIO_InitTypeDef 确定,
stm32里面初始化GPIO用的吧。。设置完了GPIO_InitStructure里面的内容后
在GPIO_Init (GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)里面调用,
比如初始化pa口,就是
GPIO_Init (GPIOA, &GPIO_InitStructure),括号里后面那个就是你问题里面声明的那个结构体
***************************************************************************/
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
/* Private functions ---------------------------------------------------------*/

void LED_GPIO_init()
{
   //初始化 GPIOA 的时钟  库函数208页
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);
 //初始化GPIO 见库函数 P125页    LED 两只接在 PA8  PD2
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;    //要设置的PIN
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度
 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化 IO
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;    //要设置的PIN
 GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化 IO
 GPIO_SetBits(GPIOD,GPIO_Pin_2);   //初始化为高
 GPIO_SetBits(GPIOA,GPIO_Pin_8);   //初始化为高
 
}
void key_init()  //按键初始化   PA13 PA15   KEY0 KEY1
{
   //初始化 GPIOA 的时钟  库函数208页
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_15;    //要设置的PIN
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; //上拉输入
 GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化 IO
// GPIO_SetBits(GPIOA,GPIO_Pin_13);   //初始化为高
// GPIO_SetBits(GPIOA,GPIO_Pin_15);   //初始化为高
}

/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
  u8 led_flag=0;
#ifdef DEBUG
  debug();
#endif
  /* System Clocks Configuration **********************************************/
  RCC_Configuration(); 
  /* NVIC Configuration *******************************************************/
  NVIC_Configuration();
  /* Configure all unused GPIO port pins in Analog Input mode (floating input
     trigger OFF), this will reduce the power consumption and increase the device
     immunity against EMI/EMC *************************************************/
   key_init();  //按键初始化   PA13 PA15   KEY0 KEY1
   LED_GPIO_init();  //初始化LED 函数
 
  while (1)
  {
   if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==0)    //如果按键按下
 {
  Delay(0xfff); //延时去抖
  if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==0) //在判断一次
  {
   GPIO_ResetBits(GPIOD,GPIO_Pin_2);   //置低 LED 亮 
  }
 }

 if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==1)    //如果松开 为高
 {
  Delay(0xffff); //延时去抖
  if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==1) //延时去抖在判断一次
  {
   GPIO_SetBits(GPIOD,GPIO_Pin_2);   //置高  LED 灭 
  }
 }
 led_flag=!led_flag;   //另一只灯 作为运行指示灯
 if(led_flag==1)
 {
  GPIO_SetBits(GPIOA,GPIO_Pin_8);   //灯灭 
 }
 else
 {
  GPIO_ResetBits(GPIOA,GPIO_Pin_8);   //灯亮
 }
 Delay(0xfffff);
 
  }
}
/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)

  /* RCC system reset(for debug purpose) */
  RCC_DeInit();
  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);
  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);
 
    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);
    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);
    /* PLLCLK = 8MHz * 9 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    /* Enable PLL */
    RCC_PLLCmd(ENABLE);
    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }
}
/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef  VECT_TAB_RAM
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); 
#endif
}
/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nCount: specifies the delay time length.
* Output         : None
* Return         : None
*******************************************************************************/
void Delay(vu32 nCount)
{
  for(; nCount != 0; nCount--);
}
#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

关闭窗口

相关文章