标题: stm32f1 PWM输入捕获程序 [打印本页]

作者: zlkj    时间: 2024-10-8 16:11
标题: stm32f1 PWM输入捕获程序
  1. #include "stm32f10x.h"   
  2. #include "pwm.h"
  3. #include "usart.h"

  4. __IO uint16_t IC1Value = 0;
  5. __IO uint16_t DutyCycle = 0;
  6. __IO uint32_t Frequency = 0;

  7. void Delay(u32 nCount)
  8. {
  9.         for(; nCount!=0; nCount--);
  10. }

  11. int main(void)
  12. {
  13.         USART1_Config();
  14.         TIM3_PWMIC_Config();
  15.        
  16.         while(1)
  17.         {
  18.                 Delay(0xffffff);
  19.                 printf("DutyCycle: %d%%\r\n", DutyCycle);
  20.                 printf("Frequency: %d\r\n", Frequency);
  21.         }
  22. }

  23. void TIM3_IRQHandler(void)
  24. {
  25.   /* Clear TIM3 Capture compare interrupt pending bit */
  26.   TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);

  27.   /* Get the Input Capture value */
  28.   IC1Value = TIM_GetCapture1(TIM3);  

  29.   if (IC1Value != 0)
  30.   {
  31.     /* Duty cycle computation */
  32.     DutyCycle = (TIM_GetCapture2(TIM3) * 100) / IC1Value;//TIM_GetCapture2触发信号和下一个相反极性的边信号的计数个数

  33.     /* Frequency computation */
  34.     Frequency = SystemCoreClock / IC1Value;
  35.   }
  36.   else
  37.   {
  38.     DutyCycle = 0;
  39.     Frequency = 0;
  40.   }
  41. }
复制代码

原理图: 无
仿真: 无
代码: stm32f1 PWM输入捕获.7z (177.87 KB, 下载次数: 1)





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1