cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure Timer to generate 7K pulses without using interrupt

MBhir.11
Associate III

Hi,

I am using STM32cube IDE and trying to create project for STM32L151RCT6 MCU .

Requirement is like need to create 7Khz (means 143 microsec )pulse(i.e 71.4 micro sec ON and 71.4 microsec OFF) on Timer 4 channel 1 without using interrupt.

Not using PWM also, using counter method .

I checked some examples , all are interrupt based examples.

Please suggest me any link for reference

1 ACCEPTED SOLUTION

Accepted Solutions

You can set all in MX inspire here

static void MX_TIM2_Init(void)
{
 
  /* USER CODE BEGIN TIM2_Init 0 */
 
  /* USER CODE END TIM2_Init 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 = 22;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 8000;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  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 = 2000;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */
 
  /* USER CODE END TIM2_Init 2 */
  HAL_TIM_MspPostInit(&htim2);
 
}

View solution in original post

4 REPLIES 4
MM..1
Chief III

Your condition no PWM is ?? when you ask Timer 4 channel 1, you need define mode for this peripheral.

Datasheet say :  They feature four independent channels each for input capture/output compare, PWM or one-pulse mode output.

For any stable pwm you dont need interrupt or dma , only set registers for your desired mode and start HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);

Hi,

Thanks for your reply i configured like that only channel 1: PWM generation CH1 and other parameter setting to generate 7KHz 50% duty cycle pulse.

I will check in Eval board and update u

You can set all in MX inspire here

static void MX_TIM2_Init(void)
{
 
  /* USER CODE BEGIN TIM2_Init 0 */
 
  /* USER CODE END TIM2_Init 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 = 22;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 8000;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  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 = 2000;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */
 
  /* USER CODE END TIM2_Init 2 */
  HAL_TIM_MspPostInit(&htim2);
 
}

Hi,

I tried with timer PWM in STM32L151RCT6 in NUCLEO-L152RE EVAL board.

i am using PLL clock so final clock i will get 32MHZ clock, pre scalar divide by 1, counter period 4572 and pulse 2286.

i am getting 280 microsec pulse which has 140 micro second ON and 140 microsec OFF..

i changed the counter period to 2286 and pulse to 1143 it was not giving output.

again i reverted the previous settings counter period 4572 and pulse 2286 but i not got the previous result.

again i tried with 2286 counter period and 2286 pulse getting 140 microsec with 70 microsec on and 70 microsec OFF.

after doing some changes not proper output and after reverting it to previous configuration not same result..

why this kind of behaviour is happening ??

please help me to sort out it..

i need 7KHZ pulse with 50% duty cycle