找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1126|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

stm32f1 PWM輸入捕獲程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:82098 發(fā)表于 2024-10-8 16:11 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
  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觸發(fā)信號(hào)和下一個(gè)相反極性的邊信號(hào)的計(jì)數(shù)個(gè)數(shù)

  33.     /* Frequency computation */
  34.     Frequency = SystemCoreClock / IC1Value;
  35.   }
  36.   else
  37.   {
  38.     DutyCycle = 0;
  39.     Frequency = 0;
  40.   }
  41. }
復(fù)制代碼

原理圖: 無(wú)
仿真: 無(wú)
代碼: stm32f1 PWM輸入捕獲.7z (177.87 KB, 下載次數(shù): 1)
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表