2024-06-24 06:14 AM
I am trying to set up PWM on Channel 1, 2, 3 of TIM3. At first I set up only Channel 1 and the output voltage of PWM is 2.8V. But after I set up the other two channels, the output voltage of Channel 1 & 3 gets to 3.6V, while that of Channel 2 is still 2.8V. Here is the GPIO and TIM settings of TIM and PWM:
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
// HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8, GPIO_PIN_RESET);
/*Configure GPIO pins : PF6 PF7 PF8 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/*Configure GPIO pin : PA6 PA7 */
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PB0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
TIM_HandleTypeDef htim3;
static void TIM_Mode_Config() {
TIM_OC_InitTypeDef sConfigOC;
ADV_TIM_CLK_ENABLE();
// Set TIM3 for all channels.
htim3.Instance = ADV_TIM;
// TIM3 clock freq = 84MHz
// TIM3 actual freq = 84MHz / 84 = 1MHz
htim3.Init.Prescaler = ADV_TIM_PRESCALER - 1;
// PWM freq = 1MHz / 1000 = 1kHz
htim3.Init.Period = ADV_TIM_PERIOD - 1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
HAL_TIM_Base_Init(&htim3);
HAL_TIM_PWM_Init(&htim3);
// Set PWM
sConfigOC.OCMode = TIM_OCMODE_PWM1;
// Duty = 50%
sConfigOC.Pulse = ADV_TIM_DEFAULT_PULSE;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
}
Any help will be highly appreciated. Thank you all.
Solved! Go to Solution.
2024-08-01 05:58 AM
Hello @LittleYe233, sorry for the delayed answer,
Your code seems ok, is it a custom board? maybe it's a hardware connections issue
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-08-01 05:58 AM
Hello @LittleYe233, sorry for the delayed answer,
Your code seems ok, is it a custom board? maybe it's a hardware connections issue
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2024-08-28 12:12 AM
Thank you. It is a custom board indeed. I will try to find out what the hardware issue is.