2661 views|0 replies

11

Posts

0

Resources
The OP
 

STM32CubeMX uses LL library to set timer to control gpio flip [Copy link]

This post was last edited by qq825117996 on 2020-9-3 16:59

Cube automatically generated code

void MX_TIM2_Init(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};

  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);

  /* TIM2 interrupt Init */
  NVIC_SetPriority(TIM2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
  NVIC_EnableIRQ(TIM2_IRQn);

  TIM_InitStruct.Prescaler = 0;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload =100-1;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM2, &TIM_InitStruct);
  LL_TIM_DisableARRPreload(TIM2);
  LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
  LL_TIM_DisableMasterSlaveMode(TIM2);

}
void MX_GPIO_Init(void)
{

 LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */
 LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
 LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_0);



 InitStruct.Pin = LL_GPIO_PIN_0;
 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
 LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

Open TIM2 in the main function

	LL_TIM_EnableIT_UPDATE(TIM2);
	LL_TIM_EnableCounter(TIM2);

Set PA0 to flip in TIM2 interrupt function

void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
  if(LL_TIM_IsActiveFlag_UPDATE(TIM2))
    {
        LL_GPIO_TogglePin(GPIOA, LL_GPIO_PIN_0);        
    }
    LL_TIM_ClearFlag_UPDATE(TIM2);
  /* USER CODE END TIM2_IRQn 0 */
  /* USER CODE BEGIN TIM2_IRQn 1 */

  /* USER CODE END TIM2_IRQn 1 */
}

It is worth noting that TIM2 generates 10KHz interrupts, but each interrupt flips the gpio once, so the square wave generated is 5KHz.

The gpio speed of L4 seems to be limited, and the maximum frequency of the square wave can only reach 12KHz at 1MHz main frequency.

These two parameters, which are automatically generated by the cube, sometimes have problems and need to be modified manually.

TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.Autoreload =100-1;

image.png (15.26 KB, downloads: 0)

image.png
This post is from stm32/stm8
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list