Skip to main content
LACTIC
Associate III
April 9, 2019
Question

Wave generation of particular frequency.

  • April 9, 2019
  • 8 replies
  • 1642 views

By using timer I want to generate a wave of 27Mhz frequency. Present time I can generate only 13.48khz frequency wave. My timer settings are below please tell me how can I generate it.

static void MX_TIM2_Init(void)

{

 TIM_ClockConfigTypeDef sClockSourceConfig;

 TIM_MasterConfigTypeDef sMasterConfig;

 TIM_OC_InitTypeDef sConfigOC;

 htim2.Instance = TIM2;

 htim2.Init.Prescaler =27;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 1000;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;

 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

 if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 if (HAL_TIM_OC_Init(&htim2) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 sConfigOC.OCMode = TIM_OCMODE_TOGGLE;

 sConfigOC.Pulse = 250;

 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

 if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

 HAL_TIM_MspPostInit(&htim2);

}

This topic has been closed for replies.

8 replies

LACTIC
LACTICAuthor
Associate III
April 13, 2019

Please suggest how can in  generate a wave of 27Mhz frequency. Timer settings are mentioned in above massage.

RomainR.
ST Employee
April 13, 2019

Hello,

Can you please detail which STM32 device, and APB Clock frequency you have set ?

Change htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

Not TIM_CLOCKDIVISION_DIV4;

27MHz / 4 = 6.75 which is the half of 13.5MHz you measure.

BR

rrom

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
LACTIC
LACTICAuthor
Associate III
April 13, 2019

Hello,

Ok. I will cange it.

I am using STM32f207ZGT6.

LACTIC
LACTICAuthor
Associate III
April 13, 2019

Sorry it is "change" not cange.

S.Ma
Principal
April 13, 2019

if you core runs at 108 MHz and say this goes direct to feed timer clock, your timer can count only up to 4 as period.

Then set the toggle point to 2 and you get 50% duty cycle at 27MHz.

0123012301230123

__--__--__--__--__

Or more fun, if you use a SPI as master with DMA cyclical to a dummy buffer, its maximum clock SCK will run at SYSCLK/2 = 54 MHz (if fast SPI, STM32 family dependent)

LACTIC
LACTICAuthor
Associate III
April 13, 2019

System clock settings :-

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct;

 RCC_ClkInitTypeDef RCC_ClkInitStruct;

  /**Initializes the CPU, AHB and APB busses clocks 

  */

 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 = 216;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

 RCC_OscInitStruct.PLL.PLLQ = 4;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Initializes the CPU, AHB and APB busses clocks 

  */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|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_DIV8;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure the Systick interrupt time 

  */

 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  /**Configure the Systick 

  */

 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 /* SysTick_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

LACTIC
LACTICAuthor
Associate III
April 13, 2019

cubmex clock config window.

S.Ma
Principal
April 13, 2019

Also look at MCU output pin, which can output internal clock with simple prescale factor. Simple cheap, keeps more complex peripherals available.

==> Reference Manual