打印
[STM32G4]

stm32g473vet6定时器2&5输出pwm有问题.

[复制链接]
695|21
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
cr315|  楼主 | 2024-6-24 09:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
对定时器2和5重新赋值后再启动定时器 pwm会在定时器启动后15秒左右才有输出,其余6个定时器的pwm输出正常工程使用CUBEMX创建

/ USER CODE BEGIN Header / /**

@file : main.c
@brief : Main program body
@attention
Copyright (c) 2024 STMicroelectronics.
All rights reserved.
This software is licensed under terms that can be found in the LICENSE file
in the root directory of this software component.
If no LICENSE file comes with this software, it is provided AS-IS.
/ / USER CODE END Header / / Includes ------------------------------------------------------------------*/

include "main.h"
include "tim.h"
include "usart.h"
include "gpio.h"
include "stdio.h"
/ Private includes ----------------------------------------------------------/ / USER CODE BEGIN Includes /

/ USER CODE END Includes /

/ Private typedef -----------------------------------------------------------/ / USER CODE BEGIN PTD /

/ USER CODE END PTD /

/ Private define ------------------------------------------------------------/ / USER CODE BEGIN PD /

/ USER CODE END PD /

/ Private macro -------------------------------------------------------------/ / USER CODE BEGIN PM /

/ USER CODE END PM /

/ Private variables ---------------------------------------------------------/

/ USER CODE BEGIN PV / / USER CODE END PV /

/ Private function prototypes -----------------------------------------------/ void SystemClock_Config(void); / USER CODE BEGIN PFP /

if _user_debug
void user_debug(void);

endif
/ USER CODE END PFP /

/ Private user code ---------------------------------------------------------/ / USER CODE BEGIN 0 /

/ USER CODE END 0 /

/**

@brief The application entry point.
@retval int / int main(void) { / USER CODE BEGIN 1 / / USER CODE END 1 */
/ MCU Configuration--------------------------------------------------------/

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

/ USER CODE BEGIN Init /

/ USER CODE END Init /

/ Configure the system clock / SystemClock_Config();

/ USER CODE BEGIN SysInit / __disable_irq(); / USER CODE END SysInit /

/ Initialize all configured peripherals / MX_GPIO_Init(); MX_TIM15_Init(); MX_TIM16_Init(); MX_TIM17_Init(); MX_TIM1_Init(); MX_TIM2_Init(); MX_TIM3_Init(); MX_TIM5_Init(); MX_TIM20_Init(); MX_USART1_UART_Init();

/ USER CODE BEGIN 2 /

// HAL_Delay(1000);

if _user_debug
user_debug();

endif
TIM15->PSC = 1 - 1; TIM15->ARR = 42500 - 1; TIM15->CCR1 = 42500 / 2;

TIM3->PSC = 1 - 1; TIM3->ARR = 42500 - 1; TIM3->CCR2 = 42500 / 2;

TIM16->PSC = 1 - 1; TIM16->ARR = 42500 - 1; TIM16->CCR1 = 42500 / 2;

TIM17->PSC = 1 - 1; TIM17->ARR = 42500 - 1; TIM17->CCR1 = 42500 / 2;

TIM20->PSC = 1 - 1; TIM20->ARR = 42500 - 1; TIM20->CCR1 = 42500 / 2;

TIM5->PSC = 1 - 1; TIM5->ARR = 42500 - 1; TIM5->CCR3 = 42500 / 2;

TIM1->PSC = 1 - 1; TIM1->ARR = 42500 - 1; TIM1->CCR2 = 42500 / 2;

TIM2->PSC = 1 - 1; TIM2->ARR = 42500 - 1; TIM2->CCR3 = 42500 / 2;

HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim20, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);

HAL_GPIO_WritePin(Relay_1_GPIO_Port, Relay_1_Pin, GPIO_PIN_SET); user_debug(); // HAL_Delay(1000);

HAL_TIM_PWM_Stop(&htim15, TIM_CHANNEL_1); HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2); HAL_TIM_PWM_Stop(&htim16, TIM_CHANNEL_1); HAL_TIM_PWM_Stop(&htim17, TIM_CHANNEL_1); HAL_TIM_PWM_Stop(&htim20, TIM_CHANNEL_1); HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_3); HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2); HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);

HAL_GPIO_WritePin(Relay_1_GPIO_Port, Relay_1_Pin, GPIO_PIN_RESET); // HAL_Delay(1000); user_debug(); / USER CODE END 2 /

/ Infinite loop / / USER CODE BEGIN WHILE / HAL_GPIO_WritePin(Relay_1_GPIO_Port, Relay_1_Pin, GPIO_PIN_SET); TIM5->PSC = 1 - 1; TIM5->ARR = 11333 - 1; TIM5->CCR3 = 11333 / 2;

TIM2->PSC = 1 - 1; TIM2->ARR = 11333 - 1; TIM2->CCR3 = 11333 / 2; /TIM2和TIM5重新赋值后 启动8路PWM输出 TIM2和TIM5大约会延时15s后再输出 其他6路正常/ HAL_TIM_PWM_Start(&htim15, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim16, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim20, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3); while (1) {

if 0
/**/ HAL_GPIO_WritePin(Relay_1_GPIO_Port, Relay_1_Pin, GPIO_PIN_RESET);

endif
/ USER CODE END WHILE /

/ USER CODE BEGIN 3 / } / USER CODE END 3 / }

/**

@brief System Clock Configuration
@retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/* Configure the main internal regulator output voltage / HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);

/** Initializes the RCC Oscillators according to the specified parameters

in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV6; RCC_OscInitStruct.PLL.PLLN = 85; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); }
/* Initializes the CPU, AHB and APB buses clocks / RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) { Error_Handler(); } }

/ USER CODE BEGIN 4 / void HAL_IncTick(void) {

uwTick += uwTickFreq; } int fputc(int ch, FILE f) { HAL_UART_Transmit(&huart1, (uint8_t )&ch, 1, 1000);

return (ch); }

if _user_debug
void user_debug(void) {

uint32_t CPU_Sn0, CPU_Sn1, CPU_Sn2; CPU_Sn0 = (__IO uint32_t )(0x1FFF7590); CPU_Sn1 = (__IO uint32_t )(0x1FFF7590 + 4); CPU_Sn2 = (__IO uint32_t )(0x1FFF7590 + 8);

printf("CPU : STM32G473VET6, LQFP100, Fre: %dMHz\r\n", SystemCoreClock / 1000000); printf("UID = %08X %08X %08X\n\r", CPU_Sn2, CPU_Sn1, CPU_Sn0); }

endif
/ USER CODE END 4 /

/**

@brief This function is executed in case of error occurrence.
@retval None / void Error_Handler(void) { / USER CODE BEGIN Error_Handler_Debug / / User can add his own implementation to report the HAL error return state / __disable_irq(); while (1) { } / USER CODE END Error_Handler_Debug */ }
ifdef USE_FULL_ASSERT
/**

@brief Reports the name of the source file and the source line number
where the assert_param error has occurred.
@param file: pointer to the source file name
@param line: assert_param error line source number
@retval None / void assert_failed(uint8_t file, uint32_t line) { / USER CODE BEGIN 6 / / User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) / / USER CODE END 6 / }
endif / USE_FULL_ASSERT /

使用特权

评论回复
沙发
AdaMaYun| | 2024-6-24 22:35 | 只看该作者
HAL库写的有点乱

使用特权

评论回复
板凳
eleg34ance| | 2024-7-22 15:14 | 只看该作者
在使用STM32G473VET6微控制器时,如果你遇到定时器2和定时器5输出PWM有问题的情况,可以检查硬件连接,确保PWM输出的引脚正确连接到外部设备或负载。检查是否有短路或断路的情况

使用特权

评论回复
地板
pe66ak| | 2024-7-22 16:18 | 只看该作者
确认系统时钟和外设时钟配置正确,特别是定时器的时钟源和分频器设置。使用STM32CubeMX工具生成初始化代码时,检查时钟树配置是否正确

使用特权

评论回复
5
tpgf| | 2024-7-22 16:46 | 只看该作者
定时器的引脚会不会出现了功能复用的现象呢

使用特权

评论回复
6
suiziq| | 2024-7-22 17:20 | 只看该作者
使用STM32CubeMX或手动配置定时器,确保定时器的模式、预分频器、自动重装载值(ARR)和捕获/比较寄存器(CCR)设置正确。确认定时器是否配置为PWM模式,并且通道配置为PWM输出

使用特权

评论回复
7
wamed| | 2024-7-22 18:25 | 只看该作者
如果使用了中断,确保中断优先级和使能设置正确。检查中断服务例程(ISR)是否正确处理了PWM相关的逻辑

使用特权

评论回复
8
星星点点didi| | 2024-7-22 19:33 | 只看该作者
确认PWM输出引脚的GPIO模式设置为复用功能输出。检查GPIO引脚是否正确映射到定时器通道

使用特权

评论回复
9
teaccch| | 2024-7-22 20:42 | 只看该作者
在主循环或定时器中断中没有错误的逻辑导致PWM输出异常。确保在启动定时器之前,所有相关的配置都已经完成

使用特权

评论回复
10
一切D都好| | 2024-7-23 08:25 | 只看该作者
使用ST-Link或其他调试工具连接到STM32G473VET6,通过调试器观察变量值和程序流程。可以在关键点设置断点,逐步执行代码,观察定时器和GPIO寄存器的状态

使用特权

评论回复
11
ewyu| | 2024-7-23 10:00 | 只看该作者
查阅STM32G473VET6的参考手册和数据手册,了解定时器和PWM的详细配置和操作。

使用特权

评论回复
12
gra22ce| | 2024-7-23 11:04 | 只看该作者
参考STM32Cube库提供的示例代码,特别是与定时器和PWM相关的示例

使用特权

评论回复
13
清芯芯清| | 2024-7-23 12:00 | 只看该作者
确保使用的STM32Cube固件库是最新版本,有时候固件库的更新会修复已知的问题

使用特权

评论回复
14
大鹏2365| | 2024-7-28 22:37 | 只看该作者
你遇到的问题是定时器 TIM2 和 TIM5 在重新赋值后,PWM 输出需要大约 15 秒才能开始。

使用特权

评论回复
15
yutingwei| | 2024-7-29 00:10 | 只看该作者
有时更改定时器寄存器后,需要重新启动定时器,或者确认定时器的时钟源和配置是否正确。

使用特权

评论回复
16
初级工程渣| | 2024-7-29 00:43 | 只看该作者
你已经在代码中调用了 HAL_TIM_PWM_Start,但可能需要在赋值后重新调用它,确保配置生效

使用特权

评论回复
17
海滨消消| | 2024-7-29 10:46 | 只看该作者
时钟源是否已经正确配置

使用特权

评论回复
18
海滨消消| | 2024-7-29 10:48 | 只看该作者
检查定时器的初始化配置

使用特权

评论回复
19
laocuo1142| | 2024-7-29 10:49 | 只看该作者
GPIO配置, 通道配置检查一下是否有误

使用特权

评论回复
20
flycamelaaa| | 2024-7-29 10:49 | 只看该作者
如果你在使用中断或DMA来更新PWM参数,请确保中断和DMA通道已正确配置和使能。
检查中断优先级和嵌套向量中断控制器(NVIC)设置。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1327

主题

3832

帖子

0

粉丝