[STM32F1] stm32 检测到按键按下 灯亮 否则灯不亮

[复制链接]
1863|3
 楼主| 捉虫天师 发表于 2017-12-25 20:20 | 显示全部楼层 |阅读模式
  1. /****************************************************************************

  2. * File Name          : key_led.c.c
  3. * Author               : MCD Application Team
  4. * FW Version        : V2.0.1
  5. * Date                  : 06/11/2012

  6. * Description        : This file provides all the RCC firmware functions.

  7. *开发板                  :神舟三号

  8. * 现象                    :如程序执行前按住按键USER2 LED亮起,没按则灯不亮
  9. *****************************************************************************

  10. */



  11. #include "stm32f10x_lib.h"


  12. /////////////   define LED   ///////////////
  13. #define  RCC_GPIO_LED               RCC_APB2Periph_GPIOF
  14. #define  GPIO_LED                     GPIOF
  15. #define  GPIO_LED_PIN         GPIO_Pin_6



  16. /////////////   define KEY   ///////////////
  17. #define  RCC_GPIO_KEY       RCC_APB2Periph_GPIOD
  18. #define  GPIO_KEY      GPIOD
  19. #define  GPIO_KEY_PIN                    GPIO_Pin_3



  20. /////////////   define Beep  ///////////////
  21. #define  RCC_GPIO_BEEP                RCC_APB2Periph_GPIOB
  22. #define  GPIO_BEEP                    GPIOB
  23. #define  GPIO_BEEP_PIN                GPIO_Pin_10



  24. /////////////   config LED   ///////////////
  25. void LED_Config(void)
  26. {
  27. GPIO_InitTypeDef GPIO_InitStructure;

  28. // RCC_ABP2PeriphClockCmd(RCC_GPIO_LED,ENABLE);


  29. GPIO_InitStructure.GPIO_Pin =  GPIO_LED_PIN;

  30. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  31. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  32. // GPIO_SetBits(GPIO_LED,GPIO_LED_PIN); //初始化完成才能使用输出功能
  33. GPIO_Init(GPIO_LED, &GPIO_InitStructure);

  34. GPIO_SetBits(GPIO_LED,GPIO_LED_PIN);

  35. }

  36. void LED_Turn_on(void)
  37. {
  38. GPIO_ResetBits(GPIO_LED,GPIO_LED_PIN);
  39. }

  40. void LED_Turn_off(void)
  41. {
  42. GPIO_SetBits(GPIO_LED,GPIO_LED_PIN);
  43. }





  44. /////////////   config KEY   ///////////////
  45. void KEY_Config(void)
  46. {
  47. GPIO_InitTypeDef GPIO_InitStructure;

  48. // RCC_ABP2PeriphClockCmd(RCC_GPIO_KEY,ENABLE);

  49. GPIO_InitStructure.GPIO_Pin =  GPIO_KEY_PIN;
  50. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//需要配置速率

  51. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

  52. GPIO_Init(GPIO_KEY, &GPIO_InitStructure);
  53. }

  54. int Is_KeyDown(void)
  55. {
  56. if( !GPIO_ReadInputDataBit(GPIO_KEY,GPIO_KEY_PIN) )
  57. {
  58.   return  1 ;
  59. }
  60. else
  61.      return 0  ;
  62. }



  63. /////// RccConfig///////
  64. void RccConfiguration(void)
  65. {
  66.   ErrorStatus HSEStartUpStatus;

  67.   /* RCC system reset(for debug purpose) */
  68.   RCC_DeInit();

  69.   /* Enable HSE */
  70.   RCC_HSEConfig(RCC_HSE_ON);     //启动外部晶体震荡器

  71.   /* Wait till HSE is ready */
  72.   HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待启动完成

  73.   if(HSEStartUpStatus == SUCCESS)   //如果启动成功,继续配置系统频率
  74.   {
  75.     /* Enable Prefetch Buffer */
  76.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//选择flash预取址缓存模式

  77.     /* Flash 2 wait state */
  78.     FLASH_SetLatency(FLASH_Latency_2); //设置flash存储器延时时钟周期数

  79.     /* HCLK = SYSCLK */
  80.     RCC_HCLKConfig(RCC_SYSCLK_Div1);  //设置HCLK相对SYSCLK的分频比例  AHB时钟
  81.   
  82.     /* PCLK2 = HCLK */
  83.     RCC_PCLK2Config(RCC_HCLK_Div1);   //设置PCLK2相对SYSCLK的分频比例  设置高速AHB时钟

  84.     /* PCLK1 = HCLK/2 */
  85.     RCC_PCLK1Config(RCC_HCLK_Div2);   

  86.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  87.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  88.     /* Enable PLL */
  89.     RCC_PLLCmd(ENABLE);//启动PLL

  90.     /* Wait till PLL is ready */
  91.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  92.     {
  93.     }

  94.     /* Select PLL as system clock source */
  95.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  96.     /* Wait till PLL is used as system clock source */
  97.     while(RCC_GetSYSCLKSource() != 0x08)  
  98.     {
  99.     }
  100.   }
  101.    
  102.   RCC_APB2PeriphClockCmd(RCC_GPIO_LED,ENABLE);
  103.   RCC_APB2PeriphClockCmd(RCC_GPIO_KEY,ENABLE);
  104.   RCC_APB2PeriphClockCmd(RCC_GPIO_BEEP,ENABLE);
  105. }





  106. //////// NvicConfig//////////

  107. void NvicConfiguration(void)
  108. {
  109.   NVIC_InitTypeDef NVIC_InitStructure;

  110. #ifdef  VECT_TAB_RAM
  111.   /* Set the Vector Table base location at 0x20000000 */
  112.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  113. #else  /* VECT_TAB_FLASH  */
  114.   /* Set the Vector Table base location at 0x08000000 */
  115.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  116. #endif

  117. }

  118. void ConfigureInterfaces(void)
  119. {      
  120.     /* System Clocks Configuration */
  121.     RccConfiguration();          //配置系统时钟   
  122.       
  123.     /* NVIC configuration */
  124.     NvicConfiguration();   //设置向量入口

  125. }
  126.                   


  127. int main(void)
  128. {
  129. ConfigureInterfaces();

  130. LED_Config();
  131. KEY_Config();

  132. while(1)
  133. {
  134.     if(Is_KeyDown()==1)
  135.     {
  136.          LED_Turn_on();
  137.     }
  138.     else
  139.     {
  140.          LED_Turn_off();
  141.     }
  142. }
  143. }


 楼主| 捉虫天师 发表于 2017-12-25 20:20 | 显示全部楼层
这个例子是检测按键的,通过判断按键状态做一个触发。
wahahaheihei 发表于 2017-12-25 22:36 | 显示全部楼层
其实按键最好的操作,就是按下计时,松手停止计时读时间,这样操作更多余地,同时按下的时候可以设置一个状态,而这个状态在松手的时候必须做出反相操作。
dongnanxibei 发表于 2017-12-26 10:45 | 显示全部楼层
学习一下,APB是怎么用的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

213

主题

3276

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部