硬件:STM32F103C8T6

  平台:ARM-MDk V5.11

  原理

  利用STM32F10x的定时器的捕获(Capture)单元测量输入信号的频率。

  基本原理是通过两次捕获达到的计数器的差值,来计算输入信号的频率。假如第一次捕获时计数器的值为Val1,第二次捕获计数器的值为Val2,

  定时器的时钟频率为ftimer,那么输入信号的频率finput

                    finput = ftimer / (Val2 - Val1)               (Val2 > Val1)

  或

                    finput = ftimer / (MaxVal - Val1 + Val2)          (Val2 ≤ Val1)

   其中MaxVal为定时器的最大计数值

  

  代码

  以TIM2 CH4为例,定时器配置代码如下:

void CaptureConfig(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure; TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM2, &TIM_ICInitStructure); /* TIM enable counter */
TIM_Cmd(TIM2, ENABLE); /* Enable the CC4 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC4, ENABLE);
}

  配置NVIC,使能TIM2的捕获中断:

void BSP_IntConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

  在TIM2中断处理函数中计算出输入信号的频率:

void TIM2_IRQHandler(void)
{
static U16 Capture, IC4ReadValue1, IC4ReadValue2;
static U8 CaptureNumber = 0; if(TIM_GetITStatus(TIM2, TIM_IT_CC4) == SET)
{
/* Clear TIM2 Capture compare interrupt pending bit */
TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);
if(CaptureNumber == 0)
{
/* Get the Input Capture value */
IC4ReadValue1 = TIM_GetCapture4(TIM2);
CaptureNumber = 1;
}
else if(CaptureNumber == 1)
{
/* Get the Input Capture value */
IC4ReadValue2 = TIM_GetCapture4(TIM2); /* Capture computation */
if (IC4ReadValue2 > IC4ReadValue1)
{
Capture = (IC4ReadValue2 - IC4ReadValue1);
}
else
{
Capture = ((0xFFFF - IC4ReadValue1) + IC4ReadValue2);
}
/* Frequency computation */
Freq = (U32) SystemCoreClock / Capture;
CaptureNumber = 0;
}
}
}

  注意:Freq是个全局变量。

/×××××××××××××××××××××××××××××××××××××××× THE END ×××××××××××××××××××××××××××××××××××××××××××××××××/

最新文章

  1. ActionMapping
  2. pl/sql配置连接远程数据库oracle,本地没有安装oracle数据库的情况下
  3. iOS 利用不等的constraint实现布局间隔调整
  4. 解决ScrollView嵌到listView冲突问题
  5. ie6下内容会撑开父级设置好的宽高
  6. 【Gym 100971J】Robots at Warehouse
  7. C#获取根目录的方法集合
  8. 登录远程SQL服务器
  9. ES6学习笔记:Module的基本用法
  10. C++设计模式-Visitor访问者模式
  11. 使用JS开发桌面端应用程序NW.js-3-开发问题小记
  12. 使用xcrun打包iOS应用
  13. SPOJ DIVCNT2 [我也不知道是什么分类了反正是数论]
  14. Azure Web连接到Azure MySql Db
  15. git温习
  16. element-ui的table动态生成表头和数据,且表中数据可编辑
  17. python常用模块之os模块
  18. TP5和TP3.2的区别
  19. 安装hyperledger fabric V1.0.1
  20. d3.js在vue项目中的安装及案例

热门文章

  1. CF1082A Vasya and Book 题解
  2. CF1497A Meximization 题解
  3. winpcap 静默安装
  4. Spring Boot应用程序启动器
  5. win10使用照片查看器查看图片
  6. 【LeetCode】434. Number of Segments in a String 解题报告(Python)
  7. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
  8. Codeforces 872B:Maximum of Maximums of Minimums(思维)
  9. C++判断月份天数(判断闰年)
  10. partial write bypass PIE