找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2135|回复: 0
收起左侧

STM32按键执行程序

[复制链接]
ID:592674 发表于 2019-8-28 15:39 | 显示全部楼层 |阅读模式
自己编写的按键执行程序,实现上翻、下翻、功能、确认、返回的功能。新手编写,高手绕道!
单片机源程序如下:
  1. #include "stm32f10x.h"
  2. #include "stdio.h"
  3. #include "tm1620.h"
  4. #include "common.h"
  5. #include "KeyScan.h"
  6. #include "delay.h"
  7. #include "Keyexec.h"
  8. #include "tim.h"

  9. ErrorStatus HSEStartUpStatus;

  10. void RCC_Configuration(void);
  11. void NVIC_Configuration(void);
  12. #ifdef  DEBUG
  13. /*******************************************************************************
  14. * Function Name  : assert_failed
  15. * Description    : Reports the name of the source file and the source line number
  16. *                  where the assert error has occurred.
  17. * Input          : - file: pointer to the source file name
  18. *                  - line: assert error line source number
  19. * Output         : None
  20. * Return         : None
  21. *******************************************************************************/
  22. void assert_failed(u8* file, u32 line)
  23. {
  24.   /* User can add his own implementation to report the file name and line number */
  25.   printf("\n\r Wrong parameter value detected on\r\n");
  26.   printf("       file  %s\r\n", file);
  27.   printf("       line  %d\r\n", line);
  28.    
  29.   /* Infinite loop */
  30.   /* while (1)
  31.   {
  32.   } */
  33. }
  34. #endif

  35. void RCC_Configuration(void)
  36. {
  37.   /* RCC system reset(for debug purpose) */
  38.   RCC_DeInit();//复位RCC时钟

  39.   /* Enable HSE */
  40.   RCC_HSEConfig(RCC_HSE_ON);//RCC配置

  41.   /* Wait till HSE is ready */
  42.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  43.   if(HSEStartUpStatus == SUCCESS)
  44.   {
  45.     /* HCLK = SYSCLK */
  46.     RCC_HCLKConfig(RCC_SYSCLK_Div1);  
  47.   
  48.     /* PCLK2 = HCLK */
  49.     RCC_PCLK2Config(RCC_HCLK_Div1);//72Mhz

  50.     /* PCLK1 = HCLK/2 */
  51.     RCC_PCLK1Config(RCC_HCLK_Div2);
  52.                
  53.                 RCC_ADCCLKConfig(RCC_PCLK2_Div6);//ADC时钟   72/6=12M

  54.     /* Flash 2 wait state */
  55.     FLASH_SetLatency(FLASH_Latency_2);
  56.     /* Enable Prefetch Buffer */
  57.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

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

  60.     /* Enable PLL */
  61.     RCC_PLLCmd(ENABLE);

  62.     /* Wait till PLL is ready */
  63.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  64.     {
  65.     }

  66.     /* Select PLL as system clock source */
  67.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  68.     /* Wait till PLL is used as system clock source */
  69.     while(RCC_GetSYSCLKSource() != 0x08)
  70.     {
  71.     }
  72.   }
  73. }

  74. /*******************************************************************************
  75. * Function Name  : NVIC_Configuration
  76. * Description    : Configures Vector Table base location.
  77. * Input          : None
  78. * Output         : None
  79. * Return         : None
  80. *******************************************************************************/
  81. void NVIC_Configuration(void)
  82. {
  83. #ifdef  VECT_TAB_RAM  
  84.   /* Set the Vector Table base location at 0x20000000 */
  85.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  86. #else  /* VECT_TAB_FLASH  */
  87.   /* Set the Vector Table base location at 0x08000000 */
  88.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x9000);
  89. #endif
  90. }

  91. void IWDG_Configuration(void)
  92. {
  93.         IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//启动寄存器读写
  94.         IWDG_SetPrescaler(IWDG_Prescaler_32);//40K时钟32分频
  95.         IWDG_SetReload(0xfff);                 //计数器数值         819.2ms
  96.         IWDG_ReloadCounter();                //重启计数器
  97.         IWDG_Enable();                       //启动看门狗
  98. }



  99. u8 Value;
  100. u8 Value1;
  101. u8 Value2;
  102. u8 Value3;
  103. int main(void)
  104. {
  105.         RCC_Configuration();
  106.         //NVIC_Configuration();
  107.         __set_FAULTMASK(0);  //打开所有中断
  108.         IOOUT_Init();
  109.         TM1616_init();
  110.         KeyScan_Init();
  111.   Timer3_Init(49);
  112.   Timer2_Init(9999);
  113.         while(1)
  114.         {        
  115.                  key_function();
  116.                  key_execute();
  117.   }
  118.         
  119. }
复制代码

所有资料51hei提供下载:
按键执行程序.7z (182.27 KB, 下载次数: 23)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表