你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

CubeMX+HAL 输出PWM

[复制链接]
STMCU-管管 发布时间:2020-9-17 10:31
1. 配置STM32CubeMX

前面的一些基础步骤可参见:【STM32】CubeMX+HAL 点亮LED 的【1.1】~【1.6】步骤。


核心配置:


这里我使用的是 TIM2 定时器,当然使用其他的也可以,但要注意相关配置。

1.1 TIM2 的 Mode 配置


20200917102048.207791af7b7088c121454b83728cf4a3.png


1.2 TIM2 的 Configuration 配置



20200917102113.a4a43d76e665e94e9036b9ef26f2277b.png



1.3 其余 GPIO 配置

PA2 的 PWM 输出作为 PA6 的输入,PA6 连接的是一个 LED ,观察是否出现呼吸灯现象。




20200917102124.145e513fdd0517e21a65988873a2b8c9.png



余下步骤可参见:【STM32】CubeMX+HAL 点亮LED 的【1.10】~【1.13】步骤。


2. 添加代码


20200917102724.60b1e7cdc12376e48c20d5bfcd47e9f2.png

20200917102738.82f803f0929d7a5f053493b9009afa72.png

下面贴出主要代码:



2.1 gpio.c


  1. /**
  2.   ******************************************************************************
  3.   * File Name          : gpio.c
  4.   * Description        : This file provides code for the configuration
  5.   *                      of all used GPIO pins.
  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. /* Includes ------------------------------------------------------------------*/
  20. #include "gpio.h"
  21. /* USER CODE BEGIN 0 */

  22. /* USER CODE END 0 */

  23. /*----------------------------------------------------------------------------*/
  24. /* Configure GPIO                                                             */
  25. /*----------------------------------------------------------------------------*/
  26. /* USER CODE BEGIN 1 */

  27. /* USER CODE END 1 */

  28. /** Configure pins as
  29.         * Analog
  30.         * Input
  31.         * Output
  32.         * EVENT_OUT
  33.         * EXTI
  34. */
  35. void MX_GPIO_Init(void)
  36. {

  37.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  38.   /* GPIO Ports Clock Enable */
  39.   __HAL_RCC_GPIOC_CLK_ENABLE();
  40.   __HAL_RCC_GPIOH_CLK_ENABLE();
  41.   __HAL_RCC_GPIOA_CLK_ENABLE();

  42.   /*Configure GPIO pin : PA6 */
  43.   GPIO_InitStruct.Pin = GPIO_PIN_6;
  44.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  45.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  46.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  47. }

  48. /* USER CODE BEGIN 2 */

  49. /* USER CODE END 2 */

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


2.2 time.c


  1. /**
  2.   ******************************************************************************
  3.   * File Name          : TIM.c
  4.   * Description        : This file provides code for the configuration
  5.   *                      of the TIM instances.
  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. /* Includes ------------------------------------------------------------------*/
  20. #include "tim.h"

  21. /* USER CODE BEGIN 0 */

  22. /* USER CODE END 0 */

  23. TIM_HandleTypeDef htim2;

  24. /* TIM2 init function */
  25. void MX_TIM2_Init(void)
  26. {
  27.   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  28.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  29.   TIM_OC_InitTypeDef sConfigOC = {0};

  30.   htim2.Instance = TIM2;
  31.   htim2.Init.Prescaler = 80-1;
  32.   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  33.   htim2.Init.Period = 100;
  34.   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  35.   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  36.   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  37.   {
  38.     Error_Handler();
  39.   }
  40.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  41.   if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  42.   {
  43.     Error_Handler();
  44.   }
  45.   if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
  46.   {
  47.     Error_Handler();
  48.   }
  49.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  50.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  51.   if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  52.   {
  53.     Error_Handler();
  54.   }
  55.   sConfigOC.OCMode = TIM_OCMODE_PWM1;
  56.   sConfigOC.Pulse = 0;
  57.   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  58.   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  59.   if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  60.   {
  61.     Error_Handler();
  62.   }
  63.   HAL_TIM_MspPostInit(&htim2);

  64. }

  65. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
  66. {

  67.   if(tim_baseHandle->Instance==TIM2)
  68.   {
  69.   /* USER CODE BEGIN TIM2_MspInit 0 */

  70.   /* USER CODE END TIM2_MspInit 0 */
  71.     /* TIM2 clock enable */
  72.     __HAL_RCC_TIM2_CLK_ENABLE();
  73.   /* USER CODE BEGIN TIM2_MspInit 1 */

  74.   /* USER CODE END TIM2_MspInit 1 */
  75.   }
  76. }
  77. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
  78. {

  79.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  80.   if(timHandle->Instance==TIM2)
  81.   {
  82.   /* USER CODE BEGIN TIM2_MspPostInit 0 */

  83.   /* USER CODE END TIM2_MspPostInit 0 */
  84.   
  85.     __HAL_RCC_GPIOA_CLK_ENABLE();
  86.     /**TIM2 GPIO Configuration   
  87.     PA2     ------> TIM2_CH3
  88.     */
  89.     GPIO_InitStruct.Pin = GPIO_PIN_2;
  90.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  91.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  92.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  93.     GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
  94.     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  95.   /* USER CODE BEGIN TIM2_MspPostInit 1 */

  96.   /* USER CODE END TIM2_MspPostInit 1 */
  97.   }

  98. }

  99. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
  100. {

  101.   if(tim_baseHandle->Instance==TIM2)
  102.   {
  103.   /* USER CODE BEGIN TIM2_MspDeInit 0 */

  104.   /* USER CODE END TIM2_MspDeInit 0 */
  105.     /* Peripheral clock disable */
  106.     __HAL_RCC_TIM2_CLK_DISABLE();
  107.   /* USER CODE BEGIN TIM2_MspDeInit 1 */

  108.   /* USER CODE END TIM2_MspDeInit 1 */
  109.   }
  110. }

  111. /* USER CODE BEGIN 1 */

  112. /* USER CODE END 1 */

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



2.3 main.c



  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. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "tim.h"
  23. #include "gpio.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */

  26. /* USER CODE END Includes */

  27. /* Private typedef -----------------------------------------------------------*/
  28. /* USER CODE BEGIN PTD */

  29. /* USER CODE END PTD */

  30. /* Private define ------------------------------------------------------------*/
  31. /* USER CODE BEGIN PD */

  32. /* USER CODE END PD */

  33. /* Private macro -------------------------------------------------------------*/
  34. /* USER CODE BEGIN PM */

  35. /* USER CODE END PM */

  36. /* Private variables ---------------------------------------------------------*/
  37. static unsigned char counter=0;
  38. static unsigned char flag=0;
  39. /* USER CODE BEGIN PV */

  40. /* USER CODE END PV */

  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. /* USER CODE BEGIN PFP */

  44. /* USER CODE END PFP */

  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */

  47. /* USER CODE END 0 */

  48. /**
  49.   * @brief  The application entry point.
  50.   * @retval int
  51.   */
  52. int main(void)
  53. {
  54.   /* USER CODE BEGIN 1 */

  55.   /* USER CODE END 1 */
  56.   

  57.   /* MCU Configuration--------------------------------------------------------*/

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

  60.   /* USER CODE BEGIN Init */

  61.   /* USER CODE END Init */

  62.   /* Configure the system clock */
  63.   SystemClock_Config();

  64.   /* USER CODE BEGIN SysInit */

  65.   /* USER CODE END SysInit */

  66.   /* Initialize all configured peripherals */
  67.   MX_GPIO_Init();
  68.   MX_TIM2_Init();
  69.   /* USER CODE BEGIN 2 */
  70.         HAL_TIM_Base_Start(&htim2);
  71.         HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);
  72.   /* USER CODE END 2 */

  73.   /* Infinite loop */
  74.   /* USER CODE BEGIN WHILE */
  75.   while (1)
  76.   {
  77.     /* USER CODE END WHILE */
  78.                 if(counter==0)
  79.                         flag=0;
  80.                 else if(counter==100)
  81.                         flag=1;
  82.                 if(flag)
  83.                         counter--;
  84.                 else
  85.                         counter++;
  86.                 TIM2->CCR3=counter;
  87.                 HAL_Delay(10);
  88.     /* USER CODE BEGIN 3 */
  89.   }
  90.   /* USER CODE END 3 */
  91. }

  92. /**
  93.   * @brief System Clock Configuration
  94.   * @retval None
  95.   */
  96. void SystemClock_Config(void)
  97. {
  98.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  99.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  100.   /** Configure the main internal regulator output voltage
  101.   */
  102.   __HAL_RCC_PWR_CLK_ENABLE();
  103.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  104.   /** Initializes the CPU, AHB and APB busses clocks
  105.   */
  106.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  107.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  108.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  109.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  110.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  111.   {
  112.     Error_Handler();
  113.   }
  114.   /** Initializes the CPU, AHB and APB busses clocks
  115.   */
  116.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  117.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  118.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  119.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  120.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  121.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  122.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  123.   {
  124.     Error_Handler();
  125.   }
  126. }

  127. /* USER CODE BEGIN 4 */

  128. /* USER CODE END 4 */

  129. /**
  130.   * @brief  This function is executed in case of error occurrence.
  131.   * @retval None
  132.   */
  133. void Error_Handler(void)
  134. {
  135.   /* USER CODE BEGIN Error_Handler_Debug */
  136.   /* User can add his own implementation to report the HAL error return state */

  137.   /* USER CODE END Error_Handler_Debug */
  138. }

  139. #ifdef  USE_FULL_ASSERT
  140. /**
  141.   * @brief  Reports the name of the source file and the source line number
  142.   *         where the assert_param error has occurred.
  143.   * @param  file: pointer to the source file name
  144.   * @param  line: assert_param error line source number
  145.   * @retval None
  146.   */
  147. void assert_failed(uint8_t *file, uint32_t line)
  148. {
  149.   /* USER CODE BEGIN 6 */
  150.   /* User can add his own implementation to report the file name and line number,
  151.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  152.   /* USER CODE END 6 */
  153. }
  154. #endif /* USE_FULL_ASSERT */

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


3. 总结
  • TIM 的配置是本工程的核心
  • 通过观察 LED 判断是否输出成功,当然有条件的可以使用 示波器 看看波形
  • STM32 的基础配置用 CubeMX 很是方便



收藏 1 评论1 发布时间:2020-9-17 10:31

举报

1个回答
小歆-2051663 回答时间:2020-9-18 10:41:43
只是输出PWM,不用 HAL_TIM_Base_Start(&htim2); 也可以吧

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版