硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3281|回复: 3
收起左侧

[TIMER] STM32H7定时器输入捕获功能实现

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107031
QQ
发表于 2018-8-20 11:29:38 | 显示全部楼层 |阅读模式
说明:

通过输入捕获功能可以测量脉宽和信号周期,占空比等信息。

测试的官方例子:TIM_InputCapture



使用的定时器1的CC2。

最新分辨率是200 MHz / 65535 ≈ 3051Hz

代码如下:
  1. int main(void)
  2. {
  3.   /* Enable the CPU Cache */
  4.   CPU_CACHE_Enable();

  5.   /* STM32H7xx HAL library initialization:
  6.        - Systick timer is configured by default as source of time base, but user
  7.          can eventually implement his proper time base source (a general purpose
  8.          timer for example or other time source), keeping in mind that Time base
  9.          duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
  10.          handled in milliseconds basis.
  11.        - Set NVIC Group Priority to 4
  12.        - Low Level Initialization
  13.      */
  14.   HAL_Init();

  15.   /* Configure the system clock to 400 MHz */
  16.   SystemClock_Config();

  17.   /*##-1- Configure the TIM peripheral #######################################*/
  18.   /* TIM1 configuration: Input Capture mode ---------------------
  19.      The external signal is connected to TIM1 CH2 pin (PE.11)  
  20.      The Rising edge is used as active edge,
  21.      The TIM1 CCR2 is used to compute the frequency value
  22.   ------------------------------------------------------------ */

  23.   /* Set TIMx instance */
  24.   TimHandle.Instance = TIMx;

  25.   /* Initialize TIMx peripheral as follows:
  26.        + Period = 0xFFFF
  27.        + Prescaler = 0
  28.        + ClockDivision = 0
  29.        + Counter direction = Up
  30.   */
  31.   TimHandle.Init.Period            = 0xFFFF;
  32.   TimHandle.Init.Prescaler         = 0;
  33.   TimHandle.Init.ClockDivision     = 0;
  34.   TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  35.   TimHandle.Init.RepetitionCounter = 0;
  36.   if(HAL_TIM_IC_Init(&TimHandle) != HAL_OK)
  37.   {
  38.     /* Initialization Error */
  39.     Error_Handler();
  40.   }

  41.   /*##-2- Configure the Input Capture channel ################################*/
  42.   /* Configure the Input Capture of channel 2 */
  43.   sICConfig.ICPolarity  = TIM_ICPOLARITY_RISING;
  44.   sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
  45.   sICConfig.ICPrescaler = TIM_ICPSC_DIV1;
  46.   sICConfig.ICFilter    = 0;   
  47.   if(HAL_TIM_IC_ConfigChannel(&TimHandle, &sICConfig, TIM_CHANNEL_2) != HAL_OK)
  48.   {
  49.     /* Configuration Error */
  50.     Error_Handler();
  51.   }
  52.   
  53.   /*##-3- Start the Input Capture in interrupt mode ##########################*/
  54.   if(HAL_TIM_IC_Start_IT(&TimHandle, TIM_CHANNEL_2) != HAL_OK)
  55.   {
  56.     /* Starting Error */
  57.     Error_Handler();
  58.   }

  59.   while (1)
  60.   {
  61.   }
  62. }

  63. /**
  64.   * @brief  Conversion complete callback in non blocking mode
  65.   * @param  htim : hadc handle
  66.   * @retval None
  67.   */
  68. void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
  69. {
  70.   if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
  71.   {
  72.     if(uhCaptureIndex == 0)
  73.     {
  74.       /* Get the 1st Input Capture value */
  75.       uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
  76.       uhCaptureIndex = 1;
  77.     }
  78.     else if(uhCaptureIndex == 1)
  79.     {
  80.       /* Get the 2nd Input Capture value */
  81.       uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

  82.       /* Capture computation */
  83.       if (uwIC2Value2 > uwIC2Value1)
  84.       {
  85.         uwDiffCapture = (uwIC2Value2 - uwIC2Value1);
  86.       }
  87.       else if (uwIC2Value2 < uwIC2Value1)
  88.       {
  89.         /* 0xFFFF is max TIM1_CCRx value */
  90.         uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2) + 1;
  91.       }
  92.       else
  93.       {
  94.         /* If capture values are equal, we have reached the limit of frequency
  95.            measures */
  96.         Error_Handler();
  97.       }
  98.       /* Frequency computation: for this example TIMx (TIM1) is clocked by
  99.          2*APB2Clk as APB2CLKDivider are set to RCC_APB2_DIV2 */      
  100.       uwFrequency = 2*HAL_RCC_GetPCLK2Freq() / uwDiffCapture;
  101.       uhCaptureIndex = 0;
  102.     }
  103.   }
  104. }
复制代码



回复

使用道具 举报

76

主题

208

回帖

436

积分

高级会员

积分
436
发表于 2020-12-2 08:46:18 | 显示全部楼层
硬汉兄,这种官方的例子在哪里的?怎么在固件库里没看到
回复

使用道具 举报

76

主题

208

回帖

436

积分

高级会员

积分
436
发表于 2020-12-2 08:59:21 | 显示全部楼层
看到了
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107031
QQ
 楼主| 发表于 2020-12-2 10:56:02 | 显示全部楼层
薪火相传 发表于 2020-12-2 08:46
硬汉兄,这种官方的例子在哪里的?怎么在固件库里没看到

TIM_InputCapture
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-14 16:30 , Processed in 0.168900 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表