- #include "stm32f10x.h"
- #include "pwm.h"
- #include "usart.h"
- __IO uint16_t IC1Value = 0;
- __IO uint16_t DutyCycle = 0;
- __IO uint32_t Frequency = 0;
- void Delay(u32 nCount)
- {
- for(; nCount!=0; nCount--);
- }
- int main(void)
- {
- USART1_Config();
- TIM3_PWMIC_Config();
-
- while(1)
- {
- Delay(0xffffff);
- printf("DutyCycle: %d%%\r\n", DutyCycle);
- printf("Frequency: %d\r\n", Frequency);
- }
- }
- void TIM3_IRQHandler(void)
- {
- /* Clear TIM3 Capture compare interrupt pending bit */
- TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
- /* Get the Input Capture value */
- IC1Value = TIM_GetCapture1(TIM3);
- if (IC1Value != 0)
- {
- /* Duty cycle computation */
- DutyCycle = (TIM_GetCapture2(TIM3) * 100) / IC1Value;//TIM_GetCapture2触发信号和下一个相反极性的边信号的计数个数
- /* Frequency computation */
- Frequency = SystemCoreClock / IC1Value;
- }
- else
- {
- DutyCycle = 0;
- Frequency = 0;
- }
- }
复制代码
原理图: 无
仿真: 无
代码:
stm32f1 PWM输入捕获.7z
(177.87 KB, 下载次数: 1)
|