PWM配置
    image.png
    时钟配置
    image.png
    代码

    1. /* USER CODE BEGIN Header */
    2. /**
    3. ******************************************************************************
    4. * @file : main.c
    5. * @brief : Main program body
    6. ******************************************************************************
    7. * @attention
    8. *
    9. * Copyright (c) 2022 STMicroelectronics.
    10. * All rights reserved.
    11. *
    12. * This software is licensed under terms that can be found in the LICENSE file
    13. * in the root directory of this software component.
    14. * If no LICENSE file comes with this software, it is provided AS-IS.
    15. *
    16. ******************************************************************************
    17. */
    18. /* USER CODE END Header */
    19. /* Includes ------------------------------------------------------------------*/
    20. #include "main.h"
    21. #include "tim.h"
    22. #include "gpio.h"
    23. /* Private includes ----------------------------------------------------------*/
    24. /* USER CODE BEGIN Includes */
    25. /* USER CODE END Includes */
    26. /* Private typedef -----------------------------------------------------------*/
    27. /* USER CODE BEGIN PTD */
    28. /* USER CODE END PTD */
    29. /* Private define ------------------------------------------------------------*/
    30. /* USER CODE BEGIN PD */
    31. /* USER CODE END PD */
    32. /* Private macro -------------------------------------------------------------*/
    33. /* USER CODE BEGIN PM */
    34. /* USER CODE END PM */
    35. /* Private variables ---------------------------------------------------------*/
    36. /* USER CODE BEGIN PV */
    37. /* USER CODE END PV */
    38. /* Private function prototypes -----------------------------------------------*/
    39. void SystemClock_Config(void);
    40. /* USER CODE BEGIN PFP */
    41. /* USER CODE END PFP */
    42. /* Private user code ---------------------------------------------------------*/
    43. /* USER CODE BEGIN 0 */
    44. void Servo_SetAngle(float Angle);
    45. /* USER CODE END 0 */
    46. /**
    47. * @brief The application entry point.
    48. * @retval int
    49. */
    50. int main(void)
    51. {
    52. /* USER CODE BEGIN 1 */
    53. float Angle;
    54. /* USER CODE END 1 */
    55. /* MCU Configuration--------------------------------------------------------*/
    56. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    57. HAL_Init();
    58. /* USER CODE BEGIN Init */
    59. /* USER CODE END Init */
    60. /* Configure the system clock */
    61. SystemClock_Config();
    62. /* USER CODE BEGIN SysInit */
    63. /* USER CODE END SysInit */
    64. /* Initialize all configured peripherals */
    65. MX_GPIO_Init();
    66. MX_TIM3_Init();
    67. /* USER CODE BEGIN 2 */
    68. HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
    69. /* USER CODE END 2 */
    70. /* Infinite loop */
    71. /* USER CODE BEGIN WHILE */
    72. while (1)
    73. {
    74. /* USER CODE END WHILE */
    75. /* USER CODE BEGIN 3 */
    76. if (HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin) == 1)
    77. {
    78. HAL_Delay(100);
    79. if (HAL_GPIO_ReadPin(KEY_GPIO_Port,KEY_Pin) == 1)
    80. {
    81. Angle += 30;
    82. if (Angle > 180)
    83. {
    84. Angle = 0;
    85. }
    86. Servo_SetAngle(Angle);
    87. }
    88. }
    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 RCC Oscillators according to the specified parameters
    105. * in the RCC_OscInitTypeDef structure.
    106. */
    107. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    108. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    109. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    110. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    111. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
    112. RCC_OscInitStruct.PLL.PLLM = 16;
    113. RCC_OscInitStruct.PLL.PLLN = 144;
    114. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    115. RCC_OscInitStruct.PLL.PLLQ = 4;
    116. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    117. {
    118. Error_Handler();
    119. }
    120. /** Initializes the CPU, AHB and APB buses clocks
    121. */
    122. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
    123. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    124. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    125. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    126. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    127. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    128. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
    129. {
    130. Error_Handler();
    131. }
    132. }
    133. /* USER CODE BEGIN 4 */
    134. void Servo_SetAngle(float Angle) {
    135. __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1,Angle / 180 * 2000 + 500);
    136. }
    137. /* USER CODE END 4 */
    138. /**
    139. * @brief This function is executed in case of error occurrence.
    140. * @retval None
    141. */
    142. void Error_Handler(void)
    143. {
    144. /* USER CODE BEGIN Error_Handler_Debug */
    145. /* User can add his own implementation to report the HAL error return state */
    146. __disable_irq();
    147. while (1)
    148. {
    149. }
    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. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    165. /* USER CODE END 6 */
    166. }
    167. #endif /* USE_FULL_ASSERT */