Several five-usage timer, stm32 of

A commonly used timing functions

 (1) basic, general-purpose, high-level timing timer has the most basic features, functions, timing functions used are as follows:

HAL_TIM_Base_Init timing of initialization, including frequency, preloaded value or the like.

HAL_TIM_ConfigClockSource Select the timer clock source

HAL_TIM_Base_Start_IT start timer

After the timeout callback HAL_TIM_PeriodElapsedCallback

 

(2) Cubemx configuration, no matter what the timer, all this configuration, attention turned interruption, calculate the final timer clock

 

(3) Fragment

 1 void MX_TIM12_Init(void)
 2 {
 3   TIM_ClockConfigTypeDef sClockSourceConfig = {0};
 4 
 5   htim12.Instance = TIM12;
 6   htim12.Init.Prescaler = 199;
 7   htim12.Init.CounterMode = TIM_COUNTERMODE_UP;
 8   htim12.Init.Period = 999;
 9   htim12.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
10   htim12.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
11   if (HAL_TIM_Base_Init(&htim12) != HAL_OK)
12   {
13     Error_Handler();
14   }
15   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
16   if (HAL_TIM_ConfigClockSource(&htim12, &sClockSourceConfig) != HAL_OK)
17   {
18       Error_Handler();
19   }
20
21 }
22 
23 int main(void)
24 {
33   HAL_Init();
34 
35   /* USER CODE BEGIN Init */
36 
37   /* USER CODE END Init */
38 
39   /* Configure the system clock */
40   SystemClock_Config();
47   MX_GPIO_Init();
48   MX_TIM12_Init();
49   /* USER CODE BEGIN 2 */
50     HAL_TIM_Base_Start_IT(&htim12);
51   /* USER CODE END 2 */
52 
53   /* Infinite loop */
54   /* USER CODE BEGIN WHILE */
55   while (1)
56   {
57     /* USER CODE END WHILE */
58         HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_13);
59         HAL_Delay(1000);
60     /* USER CODE BEGIN 3 */
61   }
62   /* USER CODE END 3 */
63 }
64 
65 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
66 {
67         if(htim->Instance == TIM12)
68         {
69                 static uint16_t cnt = 0;    
70                 if(++cnt >= 1000 )
 71                  {
 72                          cnt = 0 ;
73                          HAL_GPIO_TogglePin (GPIOH, GPIO_PIN_9);
74                  }
 75          }
 76          
77 }

 

 

Second, the use of general-purpose timer or a timer to produce the PWM waveform Advanced

(1) using a general purpose timer generates a PWM channel

a. Use the timer API

HAL_TIM_PWM_Init

HAL_TIM_PWM_ConfigChannel

HAL_TIM_PWM_Start

__HAL_TIM_SET_COMPARE

 

b.Cubemx configuration

Always select internal, Channel 1 is PWM generation.

Depending on the configuration information, found that:

Timer prescaler is defined, the actual timer clock frequency: 200MHz / (GENERAL_TIMx_PRESCALER + 1)

 The actual clock frequency: 20MHz

 Definition of the timer period, the timer starts counting when the value is updated to GENERAL_TIMx_PERIOD timer interrupt and generates corresponding event and

 Timer Interrupt frequency: 20MHz / (999 + 1) = 20KHz, i.e. timing cycle 50us

GENERAL_TIM_CH1_PULSE/GENERAL_TIM_PERIOD*100%

Therefore, the above configuration of the generated PWM frequency is 20KHz, 50% duty cycle.


c. code implementation

 

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_TIM2_Init();

    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);

  while (1)
  {
  }
}

void MX_TIM2_Init(void)
{
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};

  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 9;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  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 = 500;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_TIM_MspPostInit(&htim2);

}

 

Guess you like

Origin www.cnblogs.com/lzh666/p/11355290.html