[STM32] 有人能帮忙看看这个配置的代码哪里有问题吗,谢谢大佬们

[复制链接]
1494|3
 楼主| 892301816 发表于 2022-2-12 23:29 | 显示全部楼层 |阅读模式
代码, TI, rc, se, ni, ST
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2022 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 "usart.h"
  24. #include "gpio.h"

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

  27. /* USER CODE END Includes */

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

  30. /* USER CODE END PTD */

  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */

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

  36. /* USER CODE END PM */

  37. /* Private variables ---------------------------------------------------------*/

  38. /* USER CODE BEGIN PV */

  39. /* USER CODE END PV */

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

  43. /* USER CODE END PFP */

  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */
  46. __IO uint16_t ChannelPulse = 0.5/20.0*(period+1);
  47. uint32_t s;
  48. /* USER CODE END 0 */

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

  58.   /* MCU Configuration--------------------------------------------------------*/

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

  61.   /* USER CODE BEGIN Init */

  62.   /* USER CODE END Init */

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

  65.   /* USER CODE BEGIN SysInit */

  66.   /* USER CODE END SysInit */

  67.   /* Initialize all configured peripherals */
  68.   MX_GPIO_Init();
  69.   MX_TIM4_Init();
  70.   MX_USART1_UART_Init();
  71.   /* USER CODE BEGIN 2 */

  72. HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
  73. printf("start!");

  74.   /* USER CODE END 2 */

  75.   /* Infinite loop */
  76.   /* USER CODE BEGIN WHILE */
  77.   while (1)
  78.   {
  79.                 /* 处理数据 */
  80.     if (Key_Scan(KEY1_GPIO_Port, KEY1_Pin) == KEY_ON)
  81.     {
  82.       ChannelPulse -= 10;    // 减少占空比
  83.       
  84.       ChannelPulse = 0.5/20.0*period > ChannelPulse ? 0.5/20.0*period : ChannelPulse;    // 检查占空比的合法性
  85.       
  86.       __HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,ChannelPulse);  // 设置占空比
  87.                         s = __HAL_TIM_GET_COMPARE(&htim4, TIM_CHANNEL_1);
  88.                         printf("%d\n",s);
  89.     }
  90.    
  91.     /* 处理数据 */
  92.     if (Key_Scan(KEY2_GPIO_Port, KEY2_Pin) == KEY_ON)
  93.     {
  94.       ChannelPulse += 10;    // 增加占空比
  95.       
  96.       ChannelPulse = (2.5/20.0*period) < ChannelPulse ? (2.5/20.0*period) : ChannelPulse;    // 检查占空比的合法性
  97.       
  98.       __HAL_TIM_SET_COMPARE(&htim4,TIM_CHANNEL_1,ChannelPulse);  // 设置占空比
  99.                         s = __HAL_TIM_GET_COMPARE(&htim4, TIM_CHANNEL_1);
  100.                         printf("%d\n",s);
  101.     }
  102.                
  103.    
  104.     /* USER CODE END WHILE */

  105.     /* USER CODE BEGIN 3 */
  106.   }
  107.   /* USER CODE END 3 */
  108. }

  109. /**
  110.   * @brief System Clock Configuration
  111.   * @retval None
  112.   */
  113. void SystemClock_Config(void)
  114. {
  115.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  116.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  117.   /** Configure the main internal regulator output voltage
  118.   */
  119.   __HAL_RCC_PWR_CLK_ENABLE();
  120.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  121.   /** Initializes the RCC Oscillators according to the specified parameters
  122.   * in the RCC_OscInitTypeDef structure.
  123.   */
  124.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  125.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  126.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  127.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  128.   RCC_OscInitStruct.PLL.PLLM = 25;
  129.   RCC_OscInitStruct.PLL.PLLN = 336;
  130.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  131.   RCC_OscInitStruct.PLL.PLLQ = 4;
  132.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  133.   {
  134.     Error_Handler();
  135.   }
  136.   /** Initializes the CPU, AHB and APB buses clocks
  137.   */
  138.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  139.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  140.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  141.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  142.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  143.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  144.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  145.   {
  146.     Error_Handler();
  147.   }
  148. }

  149. /* USER CODE BEGIN 4 */

  150. /* USER CODE END 4 */

  151. /**
  152.   * @brief  This function is executed in case of error occurrence.
  153.   * @retval None
  154.   */
  155. void Error_Handler(void)
  156. {
  157.   /* USER CODE BEGIN Error_Handler_Debug */
  158.   /* User can add his own implementation to report the HAL error return state */
  159.   __disable_irq();
  160.   while (1)
  161.   {
  162.   }
  163.   /* USER CODE END Error_Handler_Debug */
  164. }

  165. #ifdef  USE_FULL_ASSERT
  166. /**
  167.   * @brief  Reports the name of the source file and the source line number
  168.   *         where the assert_param error has occurred.
  169.   * @param  file: pointer to the source file name
  170.   * @param  line: assert_param error line source number
  171.   * @retval None
  172.   */
  173. void assert_failed(uint8_t *file, uint32_t line)
  174. {
  175.   /* USER CODE BEGIN 6 */
  176.   /* User can add his own implementation to report the file name and line number,
  177.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  178.   /* USER CODE END 6 */
  179. }
  180. #endif /* USE_FULL_ASSERT */

  181. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  1. /**
  2.   ******************************************************************************
  3.   * @file    tim.c
  4.   * @brief   This file provides code for the configuration
  5.   *          of the TIM instances.
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2022 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 htim4;

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

  30.   htim4.Instance = TIM4;
  31.   htim4.Init.Prescaler = prescaler;
  32.   htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  33.   htim4.Init.Period = period;
  34.   htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  35.   htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  36.   if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  37.   {
  38.     Error_Handler();
  39.   }
  40.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  41.   if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  42.   {
  43.     Error_Handler();
  44.   }
  45.   if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
  46.   {
  47.     Error_Handler();
  48.   }
  49.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  50.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  51.   if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &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(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  60.   {
  61.     Error_Handler();
  62.   }
  63.   HAL_TIM_MspPostInit(&htim4);

  64. }

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

  67.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  68.   if(tim_baseHandle->Instance==TIM4)
  69.   {
  70.   /* USER CODE BEGIN TIM4_MspInit 0 */

  71.   /* USER CODE END TIM4_MspInit 0 */
  72.     /* TIM4 clock enable */
  73.     __HAL_RCC_TIM4_CLK_ENABLE();

  74.     __HAL_RCC_GPIOD_CLK_ENABLE();
  75.     /**TIM4 GPIO Configuration
  76.     PD13     ------> TIM4_CH2
  77.     */
  78.     GPIO_InitStruct.Pin = DUO2_Pin;
  79.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  80.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  81.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  82.     GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
  83.     HAL_GPIO_Init(DUO2_GPIO_Port, &GPIO_InitStruct);

  84.     /* TIM4 interrupt Init */
  85.     HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0);
  86.     HAL_NVIC_EnableIRQ(TIM4_IRQn);
  87.   /* USER CODE BEGIN TIM4_MspInit 1 */

  88.   /* USER CODE END TIM4_MspInit 1 */
  89.   }
  90. }
  91. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
  92. {

  93.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  94.   if(timHandle->Instance==TIM4)
  95.   {
  96.   /* USER CODE BEGIN TIM4_MspPostInit 0 */

  97.   /* USER CODE END TIM4_MspPostInit 0 */

  98.     __HAL_RCC_GPIOD_CLK_ENABLE();
  99.     /**TIM4 GPIO Configuration
  100.     PD12     ------> TIM4_CH1
  101.     */
  102.     GPIO_InitStruct.Pin = GPIO_PIN_12;
  103.     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  104.     GPIO_InitStruct.Pull = GPIO_NOPULL;
  105.     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  106.     GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
  107.     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  108.   /* USER CODE BEGIN TIM4_MspPostInit 1 */

  109.   /* USER CODE END TIM4_MspPostInit 1 */
  110.   }

  111. }

  112. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
  113. {

  114.   if(tim_baseHandle->Instance==TIM4)
  115.   {
  116.   /* USER CODE BEGIN TIM4_MspDeInit 0 */

  117.   /* USER CODE END TIM4_MspDeInit 0 */
  118.     /* Peripheral clock disable */
  119.     __HAL_RCC_TIM4_CLK_DISABLE();

  120.     /**TIM4 GPIO Configuration
  121.     PD12     ------> TIM4_CH1
  122.     PD13     ------> TIM4_CH2
  123.     */
  124.     HAL_GPIO_DeInit(GPIOD, GPIO_PIN_12|DUO2_Pin);

  125.     /* TIM4 interrupt Deinit */
  126.     HAL_NVIC_DisableIRQ(TIM4_IRQn);
  127.   /* USER CODE BEGIN TIM4_MspDeInit 1 */

  128.   /* USER CODE END TIM4_MspDeInit 1 */
  129.   }
  130. }

  131. /* USER CODE BEGIN 1 */

  132. /* USER CODE END 1 */

  133. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| 892301816 发表于 2022-2-12 23:33 | 显示全部楼层
就是简单的舵机控制,找的很多例子,都是这样配置,可我就不行
hello、C 发表于 2022-2-13 20:00 | 显示全部楼层
你得说说现象问题,具体点啊  才好分析呀  
 楼主| 892301816 发表于 2022-2-13 20:16 | 显示全部楼层
问题解决了,犯了致命的错误,没有看开发板舵机接口的原理图,设置为pwm2模式就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部