久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

專注電子技術(shù)學習與研究
當前位置:單片機教程網(wǎng) >> STM32 >> 瀏覽文章

STM32 按鍵檢測程序

作者:huqin   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2014年04月09日   【字體:

/*  ------------------------------------------------------- ————————————
注意事項: PA13 PA15 是JTAG的引腳。 所以JTAG 插上 模擬時候,不準去的。
只有調(diào)到SWD 模式 PA15 才能用。 PA13是SWDIO  PA14 SWCLK  復用時候一定要注意
 
實驗結(jié)果: DS0 交替閃爍   當按下KEY1 時候 DS1亮。 松手滅。
 
問題: 按鍵如何去抖。。這樣延時等待 占用系統(tǒng)資源。 請教高手按鍵消抖問題,有什么更好的方法解決。
---------------------------------------————————————————————---------------*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
GPIO_InitTypeDef GPIO_InitStructure;  //聲明結(jié)構(gòu)體
 
/**********************************************************************
聲明一個結(jié)構(gòu)體,名字是GPIO_InitStructure,結(jié)構(gòu)體原型由GPIO_InitTypeDef 確定,
stm32里面初始化GPIO用的吧。。設(shè)置完了GPIO_InitStructure里面的內(nèi)容后
在GPIO_Init (GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)里面調(diào)用,
比如初始化pa口,就是
GPIO_Init (GPIOA, &GPIO_InitStructure),括號里后面那個就是你問題里面聲明的那個結(jié)構(gòu)體
***************************************************************************/
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 nCount);
/* Private functions ---------------------------------------------------------*/

void LED_GPIO_init()
{
   //初始化 GPIOA 的時鐘  庫函數(shù)208頁
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD,ENABLE);
 //初始化GPIO 見庫函數(shù) P125頁    LED 兩只接在 PA8  PD2
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;    //要設(shè)置的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;    //要設(shè)置的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 的時鐘  庫函數(shù)208頁
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_15;    //要設(shè)置的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 函數(shù)
 
  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****/

關(guān)閉窗口

相關(guān)文章

主站蜘蛛池模板: 欧洲一区在线观看 | 免费在线播放黄色 | 视频国产一区 | 中文字幕91 | 国产精品久久久久久福利一牛影视 | 全免费a级毛片免费看视频免 | 亚洲高清视频在线观看 | 国产成人a亚洲精品 | 精品美女在线观看视频在线观看 | 91影院在线观看 | 成人午夜精品 | 欧美极品在线视频 | 午夜午夜精品一区二区三区文 | 九九热免费在线观看 | 成人午夜影院 | 欧美中文字幕一区二区三区 | 中文字幕欧美日韩 | 亚洲人的av| 成人小视频在线观看 | 亚洲第一在线 | 在线成人av| 91天堂网 | 国产精品a久久久久 | 亚洲自拍偷拍欧美 | 成人精品国产一区二区4080 | 国产免费av在线 | 国产成人综合在线 | 成人三级av | 欧美日韩亚洲三区 | 日韩在线国产 | 天天草天天爱 | 亚洲一区二区三区在线视频 | 日韩欧美在线一区 | 伊人网站 | 日本成人在线网址 | 日韩精品免费在线观看 | 精品综合视频 | 美女中文字幕视频 | 青青青伊人 | 欧美 视频 | 97伦理|