找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1392|回复: 0
打印 上一主题 下一主题
收起左侧

stm32F407的按键FIFO应用 源程序

[复制链接]
跳转到指定楼层
楼主
ID:97544 发表于 2020-7-24 22:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
参考并理解安富莱的代码,能有按下,弹起和长按,并且长按可以连发,在程序里修改就可以

单片机源程序如下:
  1. /* USER CODE END Header */

  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"
  4. #include "usart.h"
  5. #include "gpio.h"

  6. /* Private includes ----------------------------------------------------------*/
  7. /* USER CODE BEGIN Includes */
  8. #include "KeyFIFO.h"
  9. /* USER CODE END Includes */

  10. /* Private typedef -----------------------------------------------------------*/
  11. /* USER CODE BEGIN PTD */

  12. /* USER CODE END PTD */

  13. /* Private define ------------------------------------------------------------*/
  14. /* USER CODE BEGIN PD */

  15. /* USER CODE END PD */

  16. /* Private macro -------------------------------------------------------------*/
  17. /* USER CODE BEGIN PM */

  18. /* USER CODE END PM */

  19. /* Private variables ---------------------------------------------------------*/

  20. /* USER CODE BEGIN PV */

  21. /* USER CODE END PV */

  22. /* Private function prototypes -----------------------------------------------*/
  23. void SystemClock_Config(void);
  24. /* USER CODE BEGIN PFP */
  25. static void vTimeBaseTask(void);
  26. /* USER CODE END PFP */

  27. /* Private user code ---------------------------------------------------------*/
  28. /* USER CODE BEGIN 0 */

  29. /* USER CODE END 0 */

  30. /**
  31.   * @brief  The application entry point.
  32.   * @retval int
  33.   */
  34. int main(void)
  35. {
  36.   /* USER CODE BEGIN 1 */

  37.   /* USER CODE END 1 */

  38.   /* MCU Configuration--------------------------------------------------------*/

  39.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  40.   HAL_Init();

  41.   /* USER CODE BEGIN Init */

  42.   /* USER CODE END Init */

  43.   /* Configure the system clock */
  44.   SystemClock_Config();

  45.   /* USER CODE BEGIN SysInit */

  46.   /* USER CODE END SysInit */

  47.   /* Initialize all configured peripherals */
  48.   MX_GPIO_Init();
  49.   MX_USART1_UART_Init();
  50.   /* USER CODE BEGIN 2 */

  51.   /* USER CODE END 2 */

  52.   /* Infinite loop */
  53.   /* USER CODE BEGIN WHILE */
  54.         bsp_InitKeyVar();
  55.         printf("start\r\n");
  56.   while (1)
  57.   {
  58.                 vTimeBaseTask();
  59.                 ProcessKey();
  60.     /* USER CODE END WHILE */

  61.     /* USER CODE BEGIN 3 */
  62.                
  63.   }
  64.   /* USER CODE END 3 */
  65. }

  66. /**
  67.   * @brief System Clock Configuration
  68.   * @retval None
  69.   */
  70. void SystemClock_Config(void)
  71. {
  72.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  73.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  74.   /**Configure the main internal regulator output voltage
  75.   */
  76.   __HAL_RCC_PWR_CLK_ENABLE();
  77.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  78.   /**Initializes the CPU, AHB and APB busses clocks
  79.   */
  80.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  81.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  82.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  83.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  84.   RCC_OscInitStruct.PLL.PLLM = 25;
  85.   RCC_OscInitStruct.PLL.PLLN = 336;
  86.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  87.   RCC_OscInitStruct.PLL.PLLQ = 4;
  88.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  89.   {
  90.     Error_Handler();
  91.   }
  92.   /**Initializes the CPU, AHB and APB busses clocks
  93.   */
  94.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  95.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  96.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  97.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  98.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  99.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  100.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  101.   {
  102.     Error_Handler();
  103.   }
  104. }

  105. /* USER CODE BEGIN 4 */
  106. /**********************************************************************************************************
  107. *        函 数 名: HAL_IncTick
  108. *        功能说明: 复写Systick中断执行函数
  109. *        形    参: 无
  110. *        返 回 值: 无
  111. **********************************************************************************************************/
  112. void HAL_IncTick(void)
  113. {
  114.         extern __IO uint32_t uwTick;
  115.         extern HAL_TickFreqTypeDef uwTickFreq;
  116.         static uint8_t KeyScanCycle = 0;
  117.        
  118.   uwTick += uwTickFreq;
  119.        
  120.         if(KeyScanCycle++ == KEY_TIMECYCLE)
  121.         {
  122.                 KeyScanCycle = 0;
  123.                 xtFlag.KeyButton = 1;
  124.         }
  125.        
  126. }

  127. /**********************************************************************************************************
  128. *        函 数 名: vTimeBaseTask
  129. *        功能说明: 定时执行的函数,放到主循环里执行
  130. *        形    参: 无
  131. *        返 回 值: 无
  132. **********************************************************************************************************/
  133. static void vTimeBaseTask(void)
  134. {
  135.         if(xtFlag.KeyButton)
  136.         {
  137.                 xtFlag.KeyButton = 0;
  138.                 bsp_KeyScan();
  139.         }
  140. }

  141. /* USER CODE END 4 */

  142. /**
  143.   * @brief  This function is executed in case of error occurrence.
  144.   * @retval None
  145.   */
  146. void Error_Handler(void)
  147. {
  148.   /* USER CODE BEGIN Error_Handler_Debug */
  149.   /* User can add his own implementation to report the HAL error return state */

  150.   /* USER CODE END Error_Handler_Debug */
  151. }

  152. #ifdef  USE_FULL_ASSERT
  153. /**
  154.   * @brief  Reports the name of the source file and the source line number
  155.   *         where the assert_param error has occurred.
  156.   * @param  file: pointer to the source file name
  157.   * @param  line: assert_param error line source number
  158.   * @retval None
  159.   */
  160. void assert_failed(uint8_t *file, uint32_t line)
  161. {
  162.   /* USER CODE BEGIN 6 */
  163.   /* User can add his own implementation to report the file name and line number,
  164.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  165.   /* USER CODE END 6 */
  166. }
  167. #endif /* USE_FULL_ASSERT */

  168. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
02_KeyFIFO.7z (312.48 KB, 下载次数: 18)

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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