2020-03-10 01:13 AM
Hi,
I use HAL library to build code in CubeIDE. I use TIM1 channel 1 and 2 to output PWM.
Channel 2 is changed pulse value to change PWM duty. When I repeat to set up this value. Sometimes, the register CC2P will be changed. But, I don't change polarity value. Code of changing pulse value is as below:
void user_pwm_duty_ch2_setvalue(uint16_t value)
{
TIM_OC_InitTypeDef sConfigOC;
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = value;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
}
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = PreScaler-1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = SetPeriodARR-1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
HAL_TIM_MspPostInit(&htim1);
}
May I ask where do I set it not correctly?
Please help me.
Thank you~
Solved! Go to Solution.
2020-03-10 01:30 AM
At a guess, it is the line TIM_OC_InitTypeDef sConfigOC;
What you are telling the C compiler to do is to reserve some space on the stack for this temporary struct variable. And because you have not followed it by "={0}" you have not told the C compiler to set it to a specific value.
Now you do set some parts of this struct with your lines e.g. sConfigOC.Pulse = value; but the remainder of this struct is left as whatever was on the stack when you called user_pwm_duty_ch2_setvalue() - even if it makes no sense at all. Sometimes this tells HAL_TIM_PWM_ConfigChannel() to alter CC2P.
My suggestion:
Try changing line 3 to TIM_OC_InitTypeDef sConfigOC = {0};
Hope this helps,
Danish
2020-03-10 01:30 AM
At a guess, it is the line TIM_OC_InitTypeDef sConfigOC;
What you are telling the C compiler to do is to reserve some space on the stack for this temporary struct variable. And because you have not followed it by "={0}" you have not told the C compiler to set it to a specific value.
Now you do set some parts of this struct with your lines e.g. sConfigOC.Pulse = value; but the remainder of this struct is left as whatever was on the stack when you called user_pwm_duty_ch2_setvalue() - even if it makes no sense at all. Sometimes this tells HAL_TIM_PWM_ConfigChannel() to alter CC2P.
My suggestion:
Try changing line 3 to TIM_OC_InitTypeDef sConfigOC = {0};
Hope this helps,
Danish
2020-03-10 01:47 AM
Hi Danish,
It works normally, I must reset the value before I config it in structure.
Thank you for your remind and assistance.