Associate III
April 9, 2021
Solved
Need help to get PWM running with NUCLEO-H743ZI2 board
- April 9, 2021
- 2 replies
- 2092 views
Hello,
I am trying to generate a PWM signal with the help of several guides from internet that ultimately contain the same thing, but it does not work, I'm doing something wrong, but I cannot see what. As a result, I only see a zero line in the display of a digital analyzer.
I have included the most important settings in the appendix below.
With the values for Prescaler = 2399 I would like to achieve a timer clock of 100 kHz and for Period = 1999 a PWM frequency of 50 Hz. However, it is not entirely clear to me what the change in Pulse does. Is it the duty cycle?
The code is very simple:
...
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t br;
/* USER CODE END 1 */
...
MX_TIM3_Init();
...
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
br = 1000;
pwm_setValue(br);
HAL_Delay(100);
}
/* USER CODE END 3 */
}
...
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 2399;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 1999;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
HAL_TIM_MspPostInit(&htim3);
}
...
/* USER CODE BEGIN 4 */
void pwm_setValue(uint32_t br)
{
TIM_OC_InitTypeDef sConfigOC;
bzero(&sConfigOC, sizeof(TIM_OC_InitTypeDef));
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = br;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) == HAL_OK)
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
}
/* USER CODE END 4 */Can someone help me get PWM working? Where is my mistake?
Thanks in Advance,
Michael