I cant seem to make a pwm work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 8:33 AM
I have a project where I want to add a simple PWM. I'm using the stm32h series. I have a scope on PA3, and I set up my timer2 for PWM on channel 4. I did this all in the IOC file as I want to understand how to create the PWM I need. I see it added my init code and I'm guessing I need to add a few lines to start it.
maybe a timer start
HAL_TIM_Base_Start_IT(&htim2);
and a pwm start
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
the lines it added were
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 83999999;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &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(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&htim2);
}
I assume it configured my pins for output somewhere? Is there something more I need to do here?
If I use my GPIO and in code bring the line up and down I see the result. So I'm pretty sure my scope is on the right pin.
Solved! Go to Solution.
- Labels:
-
STM32CubeMX
-
STM32H7 Series
-
TIM
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 8:48 AM
> sConfigOC.Pulse = 0;
This means PWM with 0 duty, i.e. output permanently at one level (low or high, depending on PWM type and polarity setting etc.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 8:40 AM
Only
HAL_TIM_Base_Start_IT(&htim2);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
and then , to modify the duty cycle you could do TIM2->CCR4=whatever;
https://www.youtube.com/watch?v=K0_hfbT7vWA
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 8:42 AM
Read out and check/post TIM and relevant GPIO registers content.
JW
[EDIT}
> sConfigOC.Pulse = 0;
This means PWM with 0 duty, i.e. output permanently at one level (low or high, depending on PWM type and polarity setting etc.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 8:45 AM
Thx, but still not seeing a PWM. I'm not sure what this tells me but if I tap a 1k pull up on the line with out enablindg the timer the lines will go high. if I tap the same 1k pull up when the timer is enabled it does not. It is like the PWM is holding the line low.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 8:48 AM
> sConfigOC.Pulse = 0;
This means PWM with 0 duty, i.e. output permanently at one level (low or high, depending on PWM type and polarity setting etc.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 9:18 AM
I put sConfigOC.Pulse = 32
and I got 1 pulse ever 300ms
not sure I understand yet but its a good sign, thx.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 9:28 AM
As I've said, read out TIM registers and check their content against the description in TIM chapter in Reference Manual.
In debugger, you can stop the program, and change the content of TIM registers directly in the debugger (e.g. here the respective TIMx_CCRx register) while observing the effect of doing this on the pin.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-16 9:31 AM
yeha I see that is changing the timing thx!
