2022-12-02 07:29 AM
I have this code that bring a line up for 5us and down for 20. I want to invert it. I thought the first part of the code was a time base loop and the second was also a timer base loop. but one setting effects the other. If I use htim4.Init.Period = 250; // for 100 kHz for the first I get 5us/5us but I'm not following setting the second .
static void MX_TIM4_Init(void)
{
/* USER CODE BEGIN TIM4_Init 0 */
/* USER CODE END TIM4_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM4_Init 1 */
/* USER CODE END TIM4_Init 1 */
htim4.Instance = TIM4;
htim4.Init.Prescaler = 10;
htim4.Init.CounterMode = TIM_COUNTERMODE_DOWN;
htim4.Init.Period = 500; // for 50 kHz
// htim4.Init.Period = 250; // for 100 kHz
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
// sConfigOC.Pulse = 250; // for 50 kHz
sConfigOC.Pulse = 125; // for 100 kHz
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_ENABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM4_Init 2 */
/*##-3- Start PWM signal generation in DMA mode ############################*/
if(HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2) != HAL_OK)
{
/* Starting PWM generation Error */
Error_Handler();
}
/* USER CODE END TIM4_Init 2 */
HAL_TIM_MspPostInit(&htim4);
}
2022-12-02 07:43 AM
ah, didnt realize there was a polarity. ---> sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
2022-12-02 08:12 AM
Always read entirely the peripheral chapter of the reference manual, it is a good return of time investment
...