2015-08-22 04:26 AM
Hello,
I have a problem with my initalisiation code for an STM32F205RB Microcontroller. I want to use four PWM Output Channels on the TIM1, but only the Channel 4 work properly. The other channels do not seem to be properly connected. E.g the channel 3 is switching but only with an peak-to-peak of 0.3 V. I think the PA10 is not set as Pull-Push, more like an high impendance state. When I set the Pins PA8 to PA11 as an GPIO then they work correctly. I use the STM32CubeF2 and the Microcontroller work with 3.3 V and 100 MHz. I hope anyone can help me or can reproduce that behavior.
#include ''stm32f2xx_hal.h''
TIM_HandleTypeDef htim1;
uint32_t foo=0;
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 200;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 4;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
__GPIOC_CLK_ENABLE();
__GPIOH_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
__GPIOD_CLK_ENABLE();
}
int main()
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
TIM_OC_InitTypeDef sConfigOC;
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1;
htim1.Init.Period = 4000;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV2;
htim1.Init.RepetitionCounter = 0;
HAL_TIM_PWM_Init(&htim1);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 2000;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_4);
while(1)
{
foo++;
}
}
void SysTick_Handler()
{
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_base)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(htim_base->Instance==TIM1)
{
/* Peripheral clock enable */
__TIM1_CLK_ENABLE();
/**TIM1 GPIO Configuration
PA8 ------> TIM1_CH1
PA9 ------> TIM1_CH2
PA10 ------> TIM1_CH3
PA11 ------> TIM1_CH4
*/
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
#stm32f205-tim1
2015-08-28 06:02 AM
Hi hebbeler.tim,
When I set the Pins PA8 to PA11 as an GPIO then they work correctly.!!! GPIOs must be set as alternate function to work correctly,Try the PWM output example under STM32Cube F2 package and compare the TIM configuration with yours.STM32Cube_FW_F2_V1.1.0\Projects\STM322xG_EVAL\Examples\TIM\TIM_PWMOutput, then display the different PWM waveforms.Hope this can help.-Syrine-2015-09-03 06:21 AM
Thanks for the help. I found the bug in the code. The uninitilized variable ''sConfigOC.OCNIdleState'' and ''sConfigOC.OCNPolarity'' was the problem here.
With the additional codesConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
the timer works well.
regards
Tim
2015-09-03 07:43 AM
Hi Tim,
It is good to hear that it was solved, thank you for posting your findings and how you fixed the issue.-Syrine –