Skip to main content
deep_rune
Associate III
Sep 8, 2020 3:23 PM UTC
Solved

using PWM with STM32L412

I have PWM setup on my STM32L412, but there is no output on the pins. I move a value to CCR1, CCR2, CCR3 but there is no output. I'm using cubeIDE to generate the HAL setup, but I notice that the timers aren't enabled, but this isn't the solution - even if I set this bit there is still no output. Can anyone help? The HAL setup is below

static void MX_TIM1_Init(void)
{
 
 TIM_ClockConfigTypeDef sClockSourceConfig = {0};
 TIM_SlaveConfigTypeDef sSlaveConfig = {0};
 TIM_MasterConfigTypeDef sMasterConfig = {0};
 TIM_OC_InitTypeDef sConfigOC = {0};
 TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
 
 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 0;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = 0;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
 {
 Error_Handler();
 }
 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
 {
 Error_Handler();
 }
 sSlaveConfig.SlaveMode = TIM_SLAVEMODE_DISABLE;
 sSlaveConfig.InputTrigger = TIM_TS_ITR0;
 if (HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig) != 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();
 }
 if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != 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.Break2State = TIM_BREAK2_DISABLE;
 sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
 sBreakDeadTimeConfig.Break2Filter = 0;
 sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
 if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
 {
 Error_Handler();
 }
 HAL_TIM_MspPostInit(&htim1);
 
}

This topic has been closed for replies.
TDK
TDKBest answer
Sep 9, 2020 10:12 AM UTC

It depends on the frequency you want, but one possibility is:

> htim1.Init.Prescaler = 0;

> htim1.Init.Period = 255;

> sConfigOC.Pulse = 128;

After initializing, you will need to start the timer with

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

"If you feel a post has answered your question, please click ""Accept as Solution""."

3 replies

TDK
Sep 8, 2020 5:20 PM UTC

> htim1.Init.Prescaler = 0;

> htim1.Init.Period = 0;

> sConfigOC.Pulse = 0;

Your timer is set up to count from 0 to 0, and your output channels are enabled when the counter is 0 or above.

Given that configuration, the output is exactly what one would expect.

"If you feel a post has answered your question, please click ""Accept as Solution""."
deep_rune
deep_runeAuthor
Associate III
Sep 9, 2020 9:22 AM UTC

I want the output to be high until it reaches the value in CCR1, then go low until it reaches 256 and then reset / repeat. I tried changing the values of

> htim1.Init.Prescaler

> htim1.Init.Period

> sConfigOC.Pulse

But there is still no output. What should change these to in order to achieve my intended output?

TDK
TDKBest answer
Sep 9, 2020 10:12 AM UTC

It depends on the frequency you want, but one possibility is:

> htim1.Init.Prescaler = 0;

> htim1.Init.Period = 255;

> sConfigOC.Pulse = 128;

After initializing, you will need to start the timer with

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

"If you feel a post has answered your question, please click ""Accept as Solution""."