Skip to main content
GAGGA.1
Associate III
October 19, 2020
Question

2 micro second period with pwm output in stm32f373RCT6

  • October 19, 2020
  • 4 replies
  • 2921 views

Hi!,

I am trying to generate pulse with PWM output with 2 micro second period and 50% duty cycle on STM32F373RCT6 on custom board. I have used Timer5.

APB1 frequency is 70MHz.

Prescalar value is 69 to generate counter clock frequency of 1MHz.

ARR value is 2 to give period value of 2 micro second.

Pulse width is 1 to generate 50% duty cycle.

But PWM Output frequency is coming 1.11khz (900 micro second).

Please help me in this matter. If any other information required please let me know.

Regards,

Gaurav

This topic has been closed for replies.

4 replies

waclawek.jan
Super User
October 19, 2020

Try to run it on the default HSI clock, to exclude clock setup problems.

ARR=2 means the frequency will be 1/3MHz.

JW

GAGGA.1
GAGGA.1Author
Associate III
October 20, 2020

Client requirement is to use external oscillator.

Tesla DeLorean
Guru
October 19, 2020

Set Prescaler = 0, Period = 140-1, Pulse 70

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
GAGGA.1
GAGGA.1Author
Associate III
October 20, 2020

I tried with these values but still I couldn't get desired values. Please find attached PWM Output waveform snap.

I configured Timer5 (PC0) for PWM output as per configuration given below.

TIM_ClockConfigTypeDef sClockSourceConfig = {0};

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 TIM_OC_InitTypeDef sConfigOC = {0};

 /* USER CODE BEGIN TIM5_Init 1 */

 /* USER CODE END TIM5_Init 1 */

 htim5.Instance = TIM5;

 htim5.Init.Prescaler = 0;//1;

 htim5.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim5.Init.Period = 140-1;//33;

 htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

htim5.Init.RepetitionCounter=0;

 if (HAL_TIM_Base_Init(&htim5) != HAL_OK)

 {

  Error_Handler();

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

 if (HAL_TIM_ConfigClockSource(&htim5, &sClockSourceConfig) != HAL_OK)

 {

  Error_Handler();

 }

 if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)

 {

  Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)

 {

  Error_Handler();

 }

 sConfigOC.OCMode = TIM_OCMODE_PWM1;

 sConfigOC.Pulse = 70;

 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;

sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;

 sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;

 if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

 {

  Error_Handler();

 }

 __HAL_TIM_DISABLE_OCxPRELOAD(&htim5, TIM_CHANNEL_1);

 /* USER CODE BEGIN TIM5_Init 2 */

 /* USER CODE END TIM5_Init 2 */

 HAL_TIM_MspPostInit(&htim5);

/*Generate PWM output on MCU_DO1 pin*/

HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_1);

If any mistake I did please let me know

Tesla DeLorean
Guru
October 21, 2020

>>If any mistake I did please let me know

52.63 KHz x 140 is approximately 8 MHz, not 70 MHz

Poor frequency suggests running from HSI not PLL

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
October 20, 2020

> Client requirement is to use external oscillator.

That's OK. Just make it as an experiment so that you are confident with the timer settings.

Also make sure you are not fooled by aliasing on the oscilloscope. Switch to higher horizontal, resolution, 1us/div or so.

JW

GAGGA.1
GAGGA.1Author
Associate III
October 20, 2020

I tried with internal oscillator with APB1 frequency 24Mhz.

To generate 36Khz, I gave Prescalar value =0. ARR-665-1, period =332 but it is coming around 35.2Khz.

Next to achieve 500Khz, I gave Prescalar = 0, ARR = 24-1, period = 12 then it is coming around 50Khz and proper square wave is not coming as well as duty cycle is around 65%.

waclawek.jan
Super User
October 20, 2020

> To generate 36Khz, I gave Prescalar value =0. ARR-665-1, period =332 but it is coming around 35.2Khz.

That's OK, the internal HSI oscillator is not precise.

> Next to achieve 500Khz, I gave Prescalar = 0, ARR = 24-1, period = 12 then it is coming around 50Khz and proper square wave is not coming as well as duty cycle is around 65%.

Have you tried to switch the oscilloscope to higher resolution?

JW

GAGGA.1
GAGGA.1Author
Associate III
October 21, 2020

I tried with oscilloscope with higher resolution.

I tried with ARR=7000000-1 and Pulse = 3500000 then output period is 880ms. (Expected period is 100ms).

other values tried is ARR=14000000-1 and pulse = 7000000 then output period is 1.76sec. (Expected is 200ms).

As APB1 clock is 70MHz. I did all calculations according to this frequency.

200ms period is coming on ARR=7954.

Does this seems configuration problem?

I have no idea what is wrong.