cancel
Showing results for 
Search instead for 
Did you mean: 

RESOLVED: TIM3 - 50 Hz pulse

selmesal
Associate II
Posted on October 13, 2016 at 00:41

Hello, 

Will the following generate a 50 Hz pulse?

(Since htim3.Init.Period = 19999 which I think equates to ~20ms)

 htim3.Instance = TIM3;

  htim3.Init.Prescaler = 83;

  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim3.Init.Period = 19999;

  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)

  {

    Error_Handler();

  }

Thanks,

Sel
5 REPLIES 5
selmesal
Associate II
Posted on October 13, 2016 at 03:34

I have an update. 

On the scope, I see 3.7 Hz. 

I would appreciate any feedback on how TIM works (and how to achieve 50 Hz)

Thanks,

Sel

selmesal
Associate II
Posted on October 13, 2016 at 04:00

Looks like I can play with htim3.Init.Prescalar, Period, and sConifigOC.Pulse to manipulate the waveform properties (Frequency and Duty cycle). 

At this point, I am just turning the knobs without a clear understanding all the dependencies and how to calculate the outcome. 

I tried a setting with Prescaler = 16, Period = 7499, and Pulse=800 and was able to see a 50 Hz waveform with close to 10% duty cycle. 

But, it appears it is not stable and the waveform keeps collapsing (and I have to reset the board). 

Walid FTITI_O
Senior II
Posted on October 13, 2016 at 11:11

Hi Sel, 

To let other users in Forum helping you , try to share details abbout your request: 

Which device you are using ?

Which board ?

Wich library you use ? which version ?

you shoud share code or part of it (as TIM configuration)

-Hannibal-

selmesal
Associate II
Posted on October 13, 2016 at 23:18

Hi Hannibal, 

Thanks. 

I am trying to vary PWM width (duty cycle) as shown in the while loop below. 

I am using STM32F407VGTx (on the STM32F4 Discovery board). 

Here is the version information:

@file    stm32f4xx_hal_tim.h

  * @author  MCD Application Team

  * @version V1.5.2

  * @date    22-September-2016

  * @brief   Header file of TIM HAL module.

Here is the code I have in main.c:

int main(void)

{

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_TIM3_Init();

  HAL_TIM_Base_Start(&htim3); 

  HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);

  MX_USART2_UART_Init();

  

//Trying to step through various duty-cycle values

  while (1)

  {

    HAL_Delay(1000);      

    sConfigOC.Pulse = 100;     

    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) 

      

    HAL_Delay(1000);      

    sConfigOC.Pulse = 400;     

    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) 

    HAL_Delay(1000);  

    sConfigOC.Pulse = 800;     

    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) 

  }

}

/* TIM3 init function */

static void MX_TIM3_Init(void)

{

  TIM_ClockConfigTypeDef sClockSourceConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_OC_InitTypeDef sConfigOC;

  htim3.Instance = TIM3;

  htim3.Init.Prescaler = 16;

  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim3.Init.Period = 7499;

  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)

  {

    Error_Handler();

  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

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

  {

    Error_Handler();

  }

  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)

  {

    Error_Handler();

  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

  {

    Error_Handler();

  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 800;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

  {

    Error_Handler();

  }

  HAL_TIM_MspPostInit(&htim3);

}

Walid FTITI_O
Senior II
Posted on October 17, 2016 at 19:29

Hi Sel, 

If you want to modify the  PWM on the fly, you ought to do thatin interruption. In case, you want that in a programmable delay, you should use the systick interruption or a timer channel interrupt. In case , you want to control the PWM signal and after a certain number of pulses you dou your modification, you ought to control it using another timer input.

-Hannibal-