RoboMaster

标题: 【分享帖】从STM32开始的RoboMaster生活:进阶篇 II [打印本页]

作者: Alchemic Ronin    时间: 2020-3-25 00:57
标题: 【分享帖】从STM32开始的RoboMaster生活:进阶篇 II
本文已经同步发布于作者部署的私人博客
为了更好的排版和观看体验
可以移步到 从STM32开始的RoboMaster生活:进阶篇 II [Interrupt]

从STM32开始的RoboMaster生活:进阶篇 II [Interrupt]

1.0 什么是Interrupt?
中断是指计算机运行过程中,出现某些意外情况需主机干预时,机器能自动停止正在运行的程序并转入处理新情况的程序,处理完毕后又返回原被暂停的程序继续运行。
2.0 为什么需要Interrupt?3.0 STM32内部实现线路 & NVIC

                               
登录/注册后可看帖子
NVIC全称为Nested Vectored Interrupt Controller,是STM32专门划分独立出来的模块,专门负责Interrupt事宜,其中技术细节和使用细节非常冗杂,但是实际在RoboMaster中能使用到的部分却是比较简单的,所以本文也不展开细说。
4.0 Interrupt的生命周期5.0 如何使用Interrupt
  1. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
  2.       ......
  3.   }
复制代码
在<项目>/Drivers/STM32F4xx_HAL_Driver/Src的stm32f4xx_hal_gpio.c文件中,我们可以看到其定义
  1. __weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  2.   {
  3.     /* Prevent unused argument(s) compilation warning */
  4.     UNUSED(GPIO_Pin);
  5.     /* NOTE: This function Should not be modified, when the callback is needed,
  6.              the HAL_GPIO_EXTI_Callback could be implemented in the user file
  7.      */
  8.   }
复制代码
这里的__weak指明了,该函数如果没被用户创建,就使用上面的代码被默认创建,而如果用户在main.c中自主创建该函数,则用户创建的函数覆盖默认定义的函数。
6.0 练习项目
6.1 项目简介6.2 芯片配置

                               
登录/注册后可看帖子

                               
登录/注册后可看帖子

                               
登录/注册后可看帖子
6.3 项目代码
  1. /* USER CODE BEGIN Header */
  2.   /**
  3.     ******************************************************************************
  4.     * @file           : main.c
  5.     * @brief          : Main program body
  6.     ******************************************************************************
  7.     * @attention
  8.     *
  9.     * <h2><center>© Copyright (c) 2020 STMicroelectronics.
  10.     * All rights reserved.</center></h2>
  11.     *
  12.     * This software component is licensed by ST under BSD 3-Clause license,
  13.     * the "License"; You may not use this file except in compliance with the
  14.     * License. You may obtain a copy of the License at:
  15.     *                        opensource.org/licenses/BSD-3-Clause
  16.     *
  17.     ******************************************************************************
  18.     */
  19.   /* USER CODE END Header */
  20.   ​
  21.   /* Includes ------------------------------------------------------------------*/
  22.   #include "main.h"
  23.   ​
  24.   /* Private includes ----------------------------------------------------------*/
  25.   /* USER CODE BEGIN Includes */
  26.   ​
  27.   /* USER CODE END Includes */
  28.   ​
  29.   /* Private typedef -----------------------------------------------------------*/
  30.   /* USER CODE BEGIN PTD */
  31.   ​
  32.   /* USER CODE END PTD */
  33.   ​
  34.   /* Private define ------------------------------------------------------------*/
  35.   /* USER CODE BEGIN PD */
  36.   /* USER CODE END PD */
  37.   ​
  38.   /* Private macro -------------------------------------------------------------*/
  39.   /* USER CODE BEGIN PM */
  40.   ​
  41.   /* USER CODE END PM */
  42.   ​
  43.   /* Private variables ---------------------------------------------------------*/
  44.   ​
  45.   /* USER CODE BEGIN PV */
  46.   ​
  47.   /* USER CODE END PV */
  48.   ​
  49.   /* Private function prototypes -----------------------------------------------*/
  50.   void SystemClock_Config(void);
  51.   static void MX_GPIO_Init(void);
  52.   /* USER CODE BEGIN PFP */
  53.   ​
  54.   /* USER CODE END PFP */
  55.   ​
  56.   /* Private user code ---------------------------------------------------------*/
  57.   /* USER CODE BEGIN 0 */
  58.   ​
  59.   /* USER CODE END 0 */
  60.   ​
  61.   /**
  62.     * @brief  The application entry point.
  63.     * @retval int
  64.     */
  65.   int main(void)
  66.   {
  67.     /* USER CODE BEGIN 1 */
  68.   ​
  69.     /* USER CODE END 1 */
  70.   ​
  71.     /* MCU Configuration--------------------------------------------------------*/
  72.   ​
  73.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  74.     HAL_Init();
  75.   ​
  76.     /* USER CODE BEGIN Init */
  77.   ​
  78.     /* USER CODE END Init */
  79.   ​
  80.     /* Configure the system clock */
  81.     SystemClock_Config();
  82.   ​
  83.     /* USER CODE BEGIN SysInit */
  84.   ​
  85.     /* USER CODE END SysInit */
  86.   ​
  87.     /* Initialize all configured peripherals */
  88.     MX_GPIO_Init();
  89.     /* USER CODE BEGIN 2 */
  90.   ​
  91.     /* USER CODE END 2 */
  92.   ​
  93.     /* Infinite loop */
  94.     /* USER CODE BEGIN WHILE */
  95.     while (1)
  96.     {
  97.       /* USER CODE END WHILE */
  98.   ​
  99.       /* USER CODE BEGIN 3 */
  100.     }
  101.     /* USER CODE END 3 */
  102.   }
  103.   ​
  104.   /**
  105.     * @brief System Clock Configuration
  106.     * @retval None
  107.     */
  108.   void SystemClock_Config(void)
  109.   {
  110.     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  111.     RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  112.   ​
  113.     /** Configure the main internal regulator output voltage
  114.     */
  115.     __HAL_RCC_PWR_CLK_ENABLE();
  116.     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  117.     /** Initializes the CPU, AHB and APB busses clocks
  118.     */
  119.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  120.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  121.     RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  122.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  123.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  124.     {
  125.       Error_Handler();
  126.     }
  127.     /** Initializes the CPU, AHB and APB busses clocks
  128.     */
  129.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  130.                                 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  131.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  132.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  133.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  134.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  135.   ​
  136.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  137.     {
  138.       Error_Handler();
  139.     }
  140.   }
  141.   ​
  142.   /**
  143.     * @brief GPIO Initialization Function
  144.     * @param None
  145.     * @retval None
  146.     */
  147.   static void MX_GPIO_Init(void)
  148.   {
  149.     GPIO_InitTypeDef GPIO_InitStruct = {0};
  150.   ​
  151.     /* GPIO Ports Clock Enable */
  152.     __HAL_RCC_GPIOG_CLK_ENABLE();
  153.     __HAL_RCC_GPIOB_CLK_ENABLE();
  154.     __HAL_RCC_GPIOE_CLK_ENABLE();
  155.     __HAL_RCC_GPIOF_CLK_ENABLE();
  156.   ​
  157.     /*Configure GPIO pin Output Level */
  158.     HAL_GPIO_WritePin(GPIOG, LD8_Pin|LD7_Pin|LD6_Pin|LD5_Pin
  159.                             |LD4_Pin|LD3_Pin|LD2_Pin|LD1_Pin, GPIO_PIN_RESET);
  160.   ​
  161.     /*Configure GPIO pin Output Level */
  162.     HAL_GPIO_WritePin(LD_RED_GPIO_Port, LD_RED_Pin, GPIO_PIN_RESET);
  163.   ​
  164.     /*Configure GPIO pin Output Level */
  165.     HAL_GPIO_WritePin(LD_GREEN_GPIO_Port, LD_GREEN_Pin, GPIO_PIN_RESET);
  166.   ​
  167.     /*Configure GPIO pins : LD8_Pin LD7_Pin LD6_Pin LD5_Pin
  168.                              LD4_Pin LD3_Pin LD2_Pin LD1_Pin */
  169.     GPIO_InitStruct.Pin = LD8_Pin|LD7_Pin|LD6_Pin|LD5_Pin
  170.                             |LD4_Pin|LD3_Pin|LD2_Pin|LD1_Pin;
  171.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  172.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  173.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  174.     HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  175.   ​
  176.     /*Configure GPIO pin : Button_Pin */
  177.     GPIO_InitStruct.Pin = Button_Pin;
  178.     GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  179.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  180.     HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
  181.   ​
  182.     /*Configure GPIO pin : LD_RED_Pin */
  183.     GPIO_InitStruct.Pin = LD_RED_Pin;
  184.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  185.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  186.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  187.     HAL_GPIO_Init(LD_RED_GPIO_Port, &GPIO_InitStruct);
  188.   ​
  189.     /*Configure GPIO pin : LD_GREEN_Pin */
  190.     GPIO_InitStruct.Pin = LD_GREEN_Pin;
  191.     GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  192.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  193.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  194.     HAL_GPIO_Init(LD_GREEN_GPIO_Port, &GPIO_InitStruct);
  195.   ​
  196.     /* EXTI interrupt init*/
  197.     HAL_NVIC_SetPriority(EXTI2_IRQn, 1, 0);
  198.     HAL_NVIC_EnableIRQ(EXTI2_IRQn);
  199.   ​
  200.   }
  201.   ​
  202.   /* USER CODE BEGIN 4 */
  203.   void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
  204.       if(GPIO_Pin == Button_Pin){
  205.           HAL_Delay(500);
  206.           HAL_GPIO_TogglePin(LD_RED_GPIO_Port,LD_RED_Pin);
  207.           HAL_Delay(100);
  208.           HAL_GPIO_TogglePin(LD_GREEN_GPIO_Port,LD_GREEN_Pin);
  209.           HAL_Delay(100);
  210.           HAL_GPIO_TogglePin(LD1_GPIO_Port,LD1_Pin);
  211.           HAL_Delay(100);
  212.           HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
  213.           HAL_Delay(100);
  214.           HAL_GPIO_TogglePin(LD3_GPIO_Port,LD3_Pin);
  215.           HAL_Delay(100);
  216.           HAL_GPIO_TogglePin(LD4_GPIO_Port,LD4_Pin);
  217.           HAL_Delay(100);
  218.           HAL_GPIO_TogglePin(LD5_GPIO_Port,LD5_Pin);
  219.           HAL_Delay(100);
  220.           HAL_GPIO_TogglePin(LD6_GPIO_Port,LD6_Pin);
  221.           HAL_Delay(100);
  222.           HAL_GPIO_TogglePin(LD7_GPIO_Port,LD7_Pin);
  223.           HAL_Delay(100);
  224.           HAL_GPIO_TogglePin(LD8_GPIO_Port,LD8_Pin);
  225.       }
  226.   }
  227.   /* USER CODE END 4 */
  228.   ​
  229.   /**
  230.     * @brief  This function is executed in case of error occurrence.
  231.     * @retval None
  232.     */
  233.   void Error_Handler(void)
  234.   {
  235.     /* USER CODE BEGIN Error_Handler_Debug */
  236.     /* User can add his own implementation to report the HAL error return state */
  237.   ​
  238.     /* USER CODE END Error_Handler_Debug */
  239.   }
  240.   ​
  241.   #ifdef  USE_FULL_ASSERT
  242.   /**
  243.     * @brief  Reports the name of the source file and the source line number
  244.     *         where the assert_param error has occurred.
  245.     * @param  file: pointer to the source file name
  246.     * @param  line: assert_param error line source number
  247.     * @retval None
  248.     */
  249.   void assert_failed(uint8_t *file, uint32_t line)
  250.   {
  251.     /* USER CODE BEGIN 6 */
  252.     /* User can add his own implementation to report the file name and line number,
  253.        tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  254.     /* USER CODE END 6 */
  255.   }
  256.   #endif /* USE_FULL_ASSERT */
  257.   ​
  258.   /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

6.4 效果展示
本文已经同步发布于作者部署的私人博客
为了更好的排版和观看体验
可以移步到 从STM32开始的RoboMaster生活:进阶篇 II [Interrupt]





欢迎光临 RoboMaster (https://bbs.robomaster.com/) Powered by Discuz! X3.2