Skip to main content
shose
Associate
Feb 17, 2020 7:43 AM UTC
Solved

STM32F051C8 TIMER1 PWM NOT WORKING?

HI

stm32f051 config with stmcubemx

but, timer1 and timer2 and timer3and16and17 not working for pwm generation

only timer15 good work

why????

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
Feb 28, 2020 1:44 AM UTC

You enable only the "inverted" of the complementary channels (TIMx_CHyN), look up the CCER register's description in RM:

0690X00000DXUgPQAX.jpg

TIM2 and TIM3 don't have complementary channels, that's why CCER there is completely zero.

However, in the GPIO AF, you have chosen the "direct" channels (without N at the end) - in all timers, except TIM15 - which you say does work.

0690X00000DXUgUQAX.jpg

You enable the "inverted" channels probably using HAL_TIMEx_PWMN_Start(); functions. You have to find some Cube/HAL function, probably similarly sounding, which enables the "direct" channel. I don't use Cube/HAL.

JW

11 replies

waclawek.jan
Super User
Feb 17, 2020 9:59 AM UTC

Read out and check the content of the timer's registers, also check the content of registers of GPIOs related to the pins you want to use for PWM.

JW

shose
shoseAuthor
Associate
Feb 17, 2020 11:09 AM UTC
shose
shoseAuthor
Associate
Feb 24, 2020 7:02 AM UTC

Nobody knows??!!

berendi
Principal
Feb 24, 2020 10:55 AM UTC

Nobody knows as long as you don't post the contents of the relevant timer and gpio registers.

Nobody is going to build a board just to test your code. Nobody knows the pinout of your system except you.

shose
shoseAuthor
Associate
Feb 24, 2020 11:01 AM UTC
int main(void)
{
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */
 
 
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* USER CODE BEGIN Init */
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_TIM1_Init();
 MX_TIM16_Init();
 MX_TIM17_Init();
 MX_TIM2_Init();
 MX_TIM3_Init();
 MX_TIM15_Init();
 /* USER CODE BEGIN 2 */
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_3);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_4);
	
	HAL_TIMEx_PWMN_Start(&htim2,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim2,TIM_CHANNEL_2);
	
	HAL_TIMEx_PWMN_Start(&htim3,TIM_CHANNEL_3);
	HAL_TIMEx_PWMN_Start(&htim3,TIM_CHANNEL_4);
	
	HAL_TIMEx_PWMN_Start(&htim15,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim16,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim17,TIM_CHANNEL_1);
	
	//__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,pwm);
	//TIM1->CR1 |= 1;
	//TIM16->CR1 |= 1;
	//TIM17->CR1 |= 1;
 /* USER CODE END 2 */
 
 
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
 }
 /* USER CODE END 3 */
}
 
/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
 /** Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
 RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 /** Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
 {
 Error_Handler();
 }
}
 
/**
 * @brief TIM1 Initialization Function
 * @param None
 * @retval None
 */
static void MX_TIM1_Init(void)
{
 
 /* USER CODE BEGIN TIM1_Init 0 */
 
 /* USER CODE END TIM1_Init 0 */
 
 TIM_ClockConfigTypeDef sClockSourceConfig = {0};
 TIM_MasterConfigTypeDef sMasterConfig = {0};
 TIM_OC_InitTypeDef sConfigOC = {0};
 TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
 
 /* USER CODE BEGIN TIM1_Init 1 */
 
 /* USER CODE END TIM1_Init 1 */
 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 0;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = 8191;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
 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();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sConfigOC.OCMode = TIM_OCMODE_PWM1;
 sConfigOC.Pulse = 4096;
 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();
 }
 if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
 {
 Error_Handler();
 }
 sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
 sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
 sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
 sBreakDeadTimeConfig.DeadTime = 0;
 sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
 sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
 sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;
 if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN TIM1_Init 2 */
 
 /* USER CODE END TIM1_Init 2 */
 HAL_TIM_MspPostInit(&htim1);
 
}
 
 

shose
shoseAuthor
Associate
Feb 26, 2020 10:10 AM UTC

HI

:worried_face:

berendi
Principal
Feb 26, 2020 10:20 AM UTC

I'd be very glad to help as soon as you've posted the pinout and the GPIO and timer register values.

waclawek.jan
Super User
Feb 26, 2020 10:16 AM UTC

We told you to:

> Read out and check the content of the timer's registers, also check the content of registers of GPIOs related to the pins you want to use for PWM.

JW

shose
shoseAuthor
Associate
Feb 26, 2020 10:53 AM UTC

GPIOA0690X00000DXL2BQAX.png

GPIOB0690X00000DXL2LQAX.pngTIM10690X00000DXL2fQAH.pngTIM20690X00000DXL39QAH.png

TIM30690X00000DXL3JQAX.png

TIM150690X00000DXL3iQAH.png

TIM160690X00000DXL3xQAH.png

TIM170690X00000DXL4HQAX.png

Thanks for your help

waclawek.jan
waclawek.janBest answer
Super User
Feb 28, 2020 1:44 AM UTC

You enable only the "inverted" of the complementary channels (TIMx_CHyN), look up the CCER register's description in RM:

0690X00000DXUgPQAX.jpg

TIM2 and TIM3 don't have complementary channels, that's why CCER there is completely zero.

However, in the GPIO AF, you have chosen the "direct" channels (without N at the end) - in all timers, except TIM15 - which you say does work.

0690X00000DXUgUQAX.jpg

You enable the "inverted" channels probably using HAL_TIMEx_PWMN_Start(); functions. You have to find some Cube/HAL function, probably similarly sounding, which enables the "direct" channel. I don't use Cube/HAL.

JW

shose
shoseAuthor
Associate
Feb 29, 2020 9:24 AM UTC

HI

Thank you all

especially JW

The problem was solved

HAL_TIMEx_PWMN_Start() function is false

HAL_TIM_PWM_Start() function is true