PUYA TIM — TIM1_InputCapture_XORCh1Ch2Ch3

PUYA PY32F403_Firmware V1.4.5 TIM TIM1_InputCapture_XORCh1Ch2Ch3 示例教学

TIM — TIM1_InputCapture_XORCh1Ch2Ch3

固件库: PY32L020_Firmware V1.2.1
芯片: PUYA
源文件: PUYA/PY32L020_Firmware_V1.2.1/Projects/PY32L020-STK/Example/TIM/TIM1_InputCapture_XORCh1Ch2Ch3/Src/main.c


功能简介

本示例演示定时器输入捕获功能。当外部信号边沿到来时,定时器自动记录当前计数值,可以精确测量信号的脉宽和频率。

完整代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "main.h"

int main(void)
{
  /* Reset of all peripherals, Initializes the Systick. */ 
  HAL_Init();
  
  /* Initialize LED */
  BSP_LED_Init(LED_GREEN);
    
  TimHandle.Instance = TIM1;                                            /* TIM1 */
  TimHandle.Init.Period            = 8000 - 1;                          /* Period = 8000 - 1 */
  TimHandle.Init.Prescaler         = 3000 - 1;                          /* Prescaler = 3000 - 1 */
  TimHandle.Init.ClockDivision     = TIM_CLOCKDIVISION_DIV1;            /* ClockDivision = 0  */
  TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;                /* Counter direction = Up */
  TimHandle.Init.RepetitionCounter = 1 - 1;                             /* Repetition = 0 */
  TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;    /* Auto-reload register not buffered */
  if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK)                          /* Initialize TIM1 */
  {
    APP_ErrorHandler();
  }

  HAL_TIM_ConfigTI1Input(&TimHandle, TIM_TI1SELECTION_XORCOMBINATION);  /* input signal Select XOR combination with CH1/CH2/CH3 */
  
  /* Select the edge of the active transition on the TI1 channel: rising edge or falling edge */
  sICConfig.ICPolarity  = TIM_ICPOLARITY_BOTHEDGE;                               
  /* Select the active input: IC1 = TI1FP1 */
  sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;                             
  /* Set input prescaler: prescaler is disabled */
  sICConfig.ICPrescaler = TIM_ICPSC_DIV1;                                      
  /* Configure the input filter duration: no filter needed */
  sICConfig.ICFilter    = 0;                                                    
  /* Channel 1 Input Capture Configure */
  if (HAL_TIM_IC_ConfigChannel(&TimHandle, &sICConfig, TIM_CHANNEL_1) != HAL_OK) 
  {
    APP_ErrorHandler();
  }
  /* Start Channel 1 Input Capture in interrupt mode */
  if (HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_1) != HAL_OK)                    
  {
    APP_ErrorHandler();
  }
  while (1)
  {

  }
}

代码讲解

板级初始化:初始化开发板上的 LED、按键等基础外设。

定时器配置:设置预分频器(PSC)和重装载值(ARR),定时时间 = (PSC+1) × (ARR+1) / 时钟频率。

世界是你们
使用 Hugo 构建
主题 StackJimmy 设计